All Projects → config4k → Config4k

config4k / Config4k

Licence: apache-2.0
A Kotlin wrapper for Typesafe Config

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Config4k

Gcfg
read INI-style configuration files into Go structs; supports user-defined types and subsections
Stars: ✭ 146 (-13.1%)
Mutual labels:  config
Game Server Configs
A repo of game server configuration files used by LinuxGSM
Stars: ✭ 157 (-6.55%)
Mutual labels:  config
Stylelint Config Primer
Sharable stylelint config used by GitHub's CSS
Stars: ✭ 165 (-1.79%)
Mutual labels:  config
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+1188.1%)
Mutual labels:  config
Py Kaldi Asr
Some simple wrappers around kaldi-asr intended to make using kaldi's (online) decoders as convenient as possible.
Stars: ✭ 156 (-7.14%)
Mutual labels:  wrapper
Zstd Rs
A rust binding for the zstd compression library.
Stars: ✭ 159 (-5.36%)
Mutual labels:  wrapper
Tsconfig
Shared TypeScript config for my projects
Stars: ✭ 146 (-13.1%)
Mutual labels:  config
Imageflow
A simple wrapper of TensorFlow for Converting, Importing (and Soon, Training) Images in tensorflow.
Stars: ✭ 166 (-1.19%)
Mutual labels:  wrapper
Magento2 Configurator
Magento 2 Configurator
Stars: ✭ 158 (-5.95%)
Mutual labels:  config
Mailchimp Api
Super-simple, minimum abstraction MailChimp API v3 wrapper, in PHP
Stars: ✭ 1,977 (+1076.79%)
Mutual labels:  wrapper
Bash Sensible
An attempt at saner Bash defaults
Stars: ✭ 1,861 (+1007.74%)
Mutual labels:  config
Luwra
Minimal-overhead C++ wrapper for Lua
Stars: ✭ 152 (-9.52%)
Mutual labels:  wrapper
T14m4t
Automated brute-forcing attack tool.
Stars: ✭ 160 (-4.76%)
Mutual labels:  wrapper
Vue Axios
A small wrapper for integrating axios to Vuejs
Stars: ✭ 1,887 (+1023.21%)
Mutual labels:  wrapper
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-1.79%)
Mutual labels:  config
Goconfig
goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.
Stars: ✭ 146 (-13.1%)
Mutual labels:  config
Desktop Google Keep Osx
A Super Simple Desktop Client for Mac OSX Built in Javascript and MacGap
Stars: ✭ 159 (-5.36%)
Mutual labels:  wrapper
React Openlayers
OpenLayer React Components
Stars: ✭ 169 (+0.6%)
Mutual labels:  wrapper
Spaces Api
An API wrapper for DigitalOcean's Spaces object storage designed for easy use.
Stars: ✭ 166 (-1.19%)
Mutual labels:  wrapper
Apriltag ros
A ROS wrapper of the AprilTag 3 visual fiducial detector
Stars: ✭ 160 (-4.76%)
Mutual labels:  wrapper

Config4k

Build Status codecov ktlint Maven Central Sonatype Nexus (Snapshots) javadoc GitHub

Config for Kotlin.

Config4k is a lightweight Typesafe Config wrapper for Kotlin and inspired by ficus, providing simple extension functions Config.extract<T> and Any.toConfig to convert between Config and Kotlin Objects.

example

Table of Contents

Installation

Gradle:

repositories {
    mavenCentral()
}


dependencies {
    compile 'io.github.config4k:config4k:xxx' // See the `Download` badge
}

Usage

Delegated Properties

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

Deserialization

Config.extract<T> converts Config to T.

Map

Maps can be serialized with String keys

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 Classes

Config4k 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

Nullable

Using extract<T?> is the better way than Config.hasPath(). extract<T?> returns T when the path exists and null when it does not exist.

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

Enum

Config4k 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

Any.toConfig converts the receiver object to Config.

String

You can use ConfigValue.render() to serialize Config. Config4k helps getting Config of the class you want to serialize.

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

ConfigRenderOptions

Typesafe Config's class ConfigRenderOptions is the argument of ConfigValue.render.

// 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
}

Supported types

Property delegation, extract and toConfig support 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<K, V>
    • Array<T> (You can use Array<Int>, but can't use Array<Array<Int>>)
  • Nullable T?
  • Typesafe Config classes(Calling toConfig is meaningless)
    • com.typesafe.config.Config
    • com.typesafe.config.ConfigValue
    • com.typesafe.config.ConfigMemorySize
  • Enum
  • Data classes

See SelectReader.kt for the exhaustive list.

Snapshots

All snapshot artifacts are available in the Sonatype snapshots repository.

Contribute

Would you like to contribute to Config4k?
Take a look at CONTRIBUTING.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].