• Stars
    star
    275
  • Rank 149,796 (Top 3 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple config properties API for Kotlin

Konfig - A Type Safe Configuration API for Kotlin

Kotlin Build Status Maven Central

Konfig provides an extensible, type-safe API for configuration properties gathered from multiple sources — built in resources, system properties, property files, environment variables, command-line arguments, etc.

A secondary goal of Konfig is to make configuration "self explanatory”.

Misconfiguration errors are reported with the location and “true name” of the badly configured property. E.g. a program may look up a key defined as Key("http.port", intType). At runtime, it will be parsed from an environment variable named HTTP_PORT. So the error message reports the name of the environment variable, so that the user can easily find and fix the error.

Configuration can be inspected and listed. For example, it can be exposed by HTTP to a network management system to help site reliability engineers understand the current configuration of a running application.

Getting Started

To get started, add com.natpryce:konfig:<version> as a dependency, import com.natpryce.konfig.* and then:

  1. Define typed property keys

    val server_port = Key("server.port", intType)
    val server_host = Key("server.host", stringType)
  2. Build a Configuration object that loads properties:

    val config = systemProperties() overriding
                 EnvironmentVariables() overriding
                 ConfigurationProperties.fromFile(File("/etc/myservice.properties")) overriding
                 ConfigurationProperties.fromResource("defaults.properties")
  3. Define some properties. For example, in defaults.properties:

    server.port=8080
    server.host=0.0.0.0
  4. Look up properties by key. They are returned as typed values, not strings, and so can be used directly:

    val server = Server(config[server_port], config[server_host])
    server.start()

Konfig can load properties from:

  • Java property files and resources
  • Java system properties
  • Environment variables
  • Hard-coded maps (with convenient syntax)
  • Command-line parameters (with long and short option syntax)

Konfig can easily be extended with new property types and sources of configuration data.

Konfig can report where configuration properties are searched for and where they were found.

Naming of Properties

Konfig's Configuration objects expect property names to follow Java property name conventions: dots to represent hierarchy, lower-case identifiers within the hierarchy, hyphens to separate words in those identifiers.

For example: servers.file-storage.s3-api-key, servers.file-storage.s3-bucket.

Each Configuration implementation maps from that naming convention to the convention used by the underlying configuration store. E.g. the EnvironmentVariables implementation maps Java property name convention to the upper-case-and-underscores convention used for Unix environment variables.

Configuration is an interface and Key is a data class. This makes it straight forward to write an implementation of Configuration that translates the names of keys to different naming conventions, if your configuration follows an unusual convention.

Reflectomagic key definition

Konfig has a few ways to reduce boilerplate code when defining configuration keys.

  1. You can use Kotlin's delgated property protocol to name keys after the constants that hold them:

    val host by stringType // defines a key named "host"
    val port by intType    // defines a key named "port"
    
    ...
    
    val client = TcpClient(configuration[host], configuration[port])
    
  2. You can declare objects that extend PropertyGroup to define hierarchies of property keys that follow the Konfig naming conventions described above:

    object server : PropertyGroup() {
        val base_uri by uriType   // defines a key named "server.base-uri"
        val api_key by stringType // defines a key named "server.api-key"
    }
    
    ...
    
    val client = HttpClient(configuration[server.base_uri], configuration[server.api_key])
    

More Repositories

1

adr-tools

Command-line tools for working with Architecture Decision Records
Shell
4,051
star
2

hamkrest

Hamcrest for Kotlin
Kotlin
344
star
3

make-it-easy

A tiny framework that makes it easy to write Test Data Builders in Java
Java
156
star
4

snodge

Randomly mutate JSON, XML, HTML forms, text and binary data for fuzz testing
Kotlin
154
star
5

rusty-pi

Rust
144
star
6

pottery

It's like Twitter, for recording things that happen in your project, stored in it's version control repository.
Shell
93
star
7

goos-code-examples

Code examples from later chapters of Growing Object-Oriented Software, Guided by Tests
Java
70
star
8

maybe-java

A Maybe type for Java
Java
67
star
9

code-words

Extract individual (natural-language) words from source code
Shell
62
star
10

deft

Easy Distributed Feature Tracking
Python
60
star
11

higher-order-react-components-demo

A demonstration of higher-order React components
JavaScript
37
star
12

codea-controllers

A small library of composable multi-touch controllers for Codea that implement various game control mechanisms
Lua
26
star
13

krouton

Typesafe, compositional routing and reverse routing for web apps and HTTP microservices
Kotlin
25
star
14

python-parallelize

Simple fork/join parallelism with Python's for loop
Python
21
star
15

python-factcheck

A simple but extensible implementation of QuickCheck for Python 2.7 and Python 3 that works well with Pytest.
Python
17
star
16

learn-you-a-kotlin

Exercises for the tutorial "Learn You a Kotlin For All The Good It Will Do You"
Java
16
star
17

code-guide

Generate step-by-step explanations of code from special comments
JavaScript
16
star
18

codea-planet-battle

A simple multiplayer game for Codea
Lua
14
star
19

property-driven-diamond-kata

The "Diamond" kata, implemented by TDD with only property-based tests
Scala
12
star
20

worktorule

Manage test lifecycle by correlating failures with contents of an issue tracker.
Java
12
star
21

tdd-as-if-you-meant-it-at-agile-cambridge-2011

TDD as if You Meant it at Agile Cambridge 2011
Python
7
star
22

codea-fishing

A Simple Fishing Game
Lua
6
star
23

robots

A simple, cooperative game to teach the basics of programming
Kotlin
6
star
24

courseware

XML document type and tool chain for creating training courses
XSLT
6
star
25

codea-gui

A simple windowing system for writing GUIs in Codea
Lua
5
star
26

asciidoc-book-skeleton

A skeleton project for writing a book in Asciidoc format
Makefile
4
star
27

xsltest

XSLTest: XSLT easy stylesheet testing
4
star
28

calcpad

A utility for doing calculations as if with a pad and calculator: more useful than a calculator application, simpler than a spreadsheet
Python
4
star
29

courseware-html

Create elegant HTML presentations from Courseware documents
JavaScript
4
star
30

reservoir-rs

Reservoir sampling (Algorithm R) implemented in Rust
Rust
3
star
31

hamcrest-text-patterns

Automatically exported from code.google.com/p/hamcrest-text-patterns
Java
2
star
32

codea-bitmap-font

Bitmap Font Rendering for Codea
Lua
2
star
33

hamcrest-reflection

Automatically exported from code.google.com/p/hamcrest-reflection
Java
2
star
34

kotlin-lazy-seq

Kotlin
1
star
35

kotlinconf-2019-error-handling-examples

Kotlin
1
star
36

pottery-idea-plugin

A plugin for JetBrains IDEs for viewing and recording project history in Pottery format
Kotlin
1
star
37

spline-editor

Editor for Catmull-Rom splines, written in Kotlin with Jetpack Compose
Kotlin
1
star
38

budge

An old puzzle game written for Tcl/Tk.
JavaScript
1
star
39

jnirn

Generate C code that calls RegisterNatives for JNI code
Java
1
star