Konfig

Konfiguration for Cotlin... no, Configuration for Kotlin

License

License

GroupId

GroupId

com.natpryce
ArtifactId

ArtifactId

konfig
Last Version

Last Version

1.6.10.0
Release Date

Release Date

Type

Type

jar
Description

Description

Konfig
Konfiguration for Cotlin... no, Configuration for Kotlin
Project URL

Project URL

https://github.com/npryce/konfig
Source Code Management

Source Code Management

https://github.com/npryce/konfig

Download konfig

How to add to project

<!-- https://jarcasting.com/artifacts/com.natpryce/konfig/ -->
<dependency>
    <groupId>com.natpryce</groupId>
    <artifactId>konfig</artifactId>
    <version>1.6.10.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.natpryce/konfig/
implementation 'com.natpryce:konfig:1.6.10.0'
// https://jarcasting.com/artifacts/com.natpryce/konfig/
implementation ("com.natpryce:konfig:1.6.10.0")
'com.natpryce:konfig:jar:1.6.10.0'
<dependency org="com.natpryce" name="konfig" rev="1.6.10.0">
  <artifact name="konfig" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.natpryce', module='konfig', version='1.6.10.0')
)
libraryDependencies += "com.natpryce" % "konfig" % "1.6.10.0"
[com.natpryce/konfig "1.6.10.0"]

Dependencies

test (4)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-reflect jar 1.2.41
org.jetbrains.kotlin : kotlin-test jar 1.2.41
junit : junit jar 4.12
com.natpryce : hamkrest jar 1.4.0.0

Project Modules

There are no modules declared in this project.

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])
    

Versions

Version
1.6.10.0
1.6.9.0
1.6.8.0
1.6.7.0
1.6.6.0
1.6.5.0
1.6.4.0
1.6.3.0
1.6.1.0
1.6.0.0
1.5.0.0
1.4.2.0
1.4.1.0
1.4.0.0
1.3.0.0
1.2.0.0
1.1.0.0_patched
0.0.18.0
0.0.16.0
0.0.15.0
0.0.14.0
0.0.13.0
0.0.12.0
0.0.11.0
0.0.10.0
0.0.9.0
0.0.8.0
0.0.7.0
0.0.6.0
0.0.5.0
0.0.4.0
0.0.3.0
0.0.2.0
0.0.1.0
version=1.6.2.1