在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):config4k/config4k开源软件地址(OpenSource Url):https://github.com/config4k/config4k开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):Config4kConfig for Kotlin. Config4k is a lightweight Typesafe Config wrapper for Kotlin and inspired by ficus, providing simple extension functions Table of ContentsInstallationGradle: repositories {
mavenCentral()
}
dependencies {
compile 'io.github.config4k:config4k:xxx' // See the `Download` badge
} UsageDelegated PropertiesBy 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 Deserialization
MapMaps can be serialized with val config = ConfigFactory.parseString("""
|map {
| foo = 5
| bar = 6
|}""".trimMargin())
val map: Map<String, Int> = config.extract<Map<String, Int>>("map")
println(map["foo"] == 5) // true
println(map["bar"] == 6) // true or with arbitrary keys val config = ConfigFactory.parseString("""
|map = [{
| key = 5
| value = "foo"
|}
|{
| key = 6
| value = "bar"
|}]""".trimMargin())
val map: Map<Int, String> = config.extract<Map<Int, String>>("map")
println(map[5] == "foo") // true
println(map[6] == "bar") // true Test Class: TestMap.kt Data ClassesConfig4k has no option to use different names between code and config file. data class Person(val name: String, val age: Int)
val config = ConfigFactory.parseString("""
|key {
| name = "foo"
| age = 20
|}""".trimMargin())
val person: Person = config.extract<Person>("key")
println(person.name == "foo") // true
println(person.age == 20) // true For more details, please see TestArbitraryType.kt NullableUsing val config = ConfigFactory.parseString("""key = 10""")
val key = config.extract<Int?>("key")
val foo = config.extract<Int?>("foo")
println(key == 10) // true
println(foo == null) // true Test Class: TestNullable.kt EnumConfig4k also supports Enum. Enum is converted to String of its name in the config file. enum class Size {
SMALL,
MEDIUM,
LARGE
}
val config = ConfigFactory.parseString("""key = SMALL""")
val small = config.extract<Size>("key")
println(small == Size.SMALL) // true Test Class: TestEnum.kt Serialization
StringYou can use ConfigValue.render() to serialize data class Person(val name: String, val age: Int)
val person = Person("foo", 20).toConfig("person")
println(person.root().render()) Output:
Test Class: TestToConfigForArbitraryType.kt ConfigRenderOptionsTypesafe Config's class // 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:
// 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:
Supported typesProperty delegation,
See SelectReader.kt for the exhaustive list. SnapshotsAll snapshot artifacts are available in the Sonatype snapshots repository. ContributeWould you like to contribute to Config4k? |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论