Nano-library which provides the ability to define typesafe (!) Configuration templates for applications.
A Configuration
is a set of named and typed Property
instances, which are defined using a ConfigurationTemplate
.
Each defined Property can be set with a default value, or be blank with a requirement to be overridden. At runtime, the template is reified
into a concrete Configuration
object, but if any properties are missing this process will throw a Misconfiguration
error.
Assuming that the reification process is successful, property values can be retrieved in an (actually) type-safe manner, and are applied in the following descending order of precedence:
- JVM system property
- Named environment property
- Default value
This is from the Kotlin version, but the Scala and Java APIs are broadly the same:
// simple typed values
val USER = Property.string("USER")
val AGE = Property.int("AGE")
// provide custom mapping functions to convert from strings to domain
val PATIENCE_LEVEL = Property("DURATION", { i: String -> Duration.parse(i) }, { it.describe() })
// build your template
val configTemplate = ConfigurationTemplate()
.requiring(USER) // will be supplied by the environment
.withProp(AGE, 2) // falls back to a default typed value
.withProp(PATIENCE_LEVEL, Duration(10)) // custom typed property with default
// attempting to build a configuration with missing values will fail with a Misconfiguration exception
val config = configTemplate.reify()
// retrieval of values in a typesafe way
val patience: Duration = config.valueOf[PATIENCE_LEVEL]
Currently, the library is published in Java, Kotlin and Scala versions in JCenter (and synced to Maven Central).
Java:
<dependency>
<groupId>io.github.daviddenton</groupId>
<artifactId>configur8</artifactId>
<version>1.7.0</version>
</dependency>
Kotlin:
<dependency>
<groupId>io.github.daviddenton</groupId>
<artifactId>konfigur8</artifactId>
<version>1.7.0</version>
</dependency>
libraryDependencies += "io.github.daviddenton" %% "configur8" % "1.7.0"