A Kotlin wrapper for Typesafe Config
Config for Kotlin.
Config4k is a lightweight Typesafe Config wrapper for Kotlin and inspired by ficus, providing simple extension functions
Config.extractand
Any.toConfigto convert between
Configand Kotlin Objects.
Gradle:
repositories { mavenCentral() }dependencies { compile 'io.github.config4k:config4k:xxx' // See the
Download
badge }
By far the simplest way to use config4k is via Kotlin Delegated Properties:
val config = ConfigFactory.parseString(""" |stringValue = hello |booleanValue = true |""".trimMargin())val stringValue: String by config println(stringValue) // hello
val nullableStringValue: String? by config println(nullableStringValue) // null
val booleanValue: Boolean by config println(booleanValue) // true
Config.extractconverts
Configto
T.
Maps can be serialized with
Stringkeys
kotlin val config = ConfigFactory.parseString(""" |map { | foo = 5 | bar = 6 |}""".trimMargin()) val map: Map = config.extractor with arbitrary keys
kotlin val config = ConfigFactory.parseString(""" |map = [{ | key = 5 | value = "foo" |} |{ | key = 6 | value = "bar" |}]""".trimMargin()) val map: Map = config.extractTest Class: TestMap.kt
Config4k has no option to use different names between code and config file. ```kotlin data class Person(val name: String, val age: Int)
val config = ConfigFactory.parseString("""
|key {
| name = "foo"
| age = 20
|}""".trimMargin())
val person: Person = config.extract("key")
println(person.name == "foo") // true
println(person.age == 20) // true
```
For more details, please see TestArbitraryType.kt
Using
extractis the better way than
Config.hasPath().
extractreturns
Twhen the path exists and
nullwhen it does not exist.
kotlin val config = ConfigFactory.parseString("""key = 10""") val key = config.extract("key") val foo = config.extract("foo") println(key == 10) // true println(foo == null) // trueTest Class: TestNullable.kt
Config4k also supports Enum. Enum is converted to String of its name in the config file. ```kotlin enum class Size { SMALL, MEDIUM, LARGE }
val config = ConfigFactory.parseString("""key = SMALL""") val small = config.extract("key") println(small == Size.SMALL) // true ``` Test Class: TestEnum.kt
Any.toConfigconverts the receiver object to
Config.
You can use ConfigValue.render() to serialize
Config. Config4k helps getting
Configof the class you want to serialize.
kotlin data class Person(val name: String, val age: Int) val person = Person("foo", 20).toConfig("person") println(person.root().render())Output:
{ # hardcoded value "person" : { # hardcoded value "age" : 20, # hardcoded value "name" : "foo" } }Test Class: TestToConfigForArbitraryType.kt
Typesafe Config's class
ConfigRenderOptionsis the argument of
ConfigValue.render.
kotlin // If setJson(false) is called, ConfigValue.render returns HOCON data class Person(val name: String, val age: Int) val person = Person("foo", 20).toConfig("person") val options = ConfigRenderOptions.defaults().setJson(false) println(person.root().render(options))Output:
# hardcoded value person { # hardcoded value age=20 # hardcoded value name=foo }
// setOriginComments(false) removes comments data class Person(val name: String, val age: Int) val person = Person("foo", 20).toConfig("person") val options = ConfigRenderOptions.defaults() .setJson(false) .setOriginComments(false) println(person.root().render(options))
Output:
person { age=20 name=foo }
Property delegation,
extractand
toConfigsupport these types: - Primitive types -
Boolean-
Byte-
Int-
Long-
Float-
Double-
String-
import java.io.File-
import java.nio.file.Path-
java.time.Duration-
java.time.Period-
java.time.temporal.TemporalAmount-
kotlin.text.Regex- Collections -
List-
Set-
Map-
Array(You can use
Array, but can't use
Array>) - Nullable
T?- Typesafe Config classes(Calling
toConfigis meaningless) -
com.typesafe.config.Config-
com.typesafe.config.ConfigValue-
com.typesafe.config.ConfigMemorySize- Enum - Data classes
See SelectReader.kt for the exhaustive list.
All snapshot artifacts are available in the Sonatype snapshots repository.
Would you like to contribute to Config4k?
Take a look at CONTRIBUTING.md