Konstant

Global access to the loaded config. Load it once at startup with the generated init<Spec> function, then read any spec, root or nested, from anywhere:

// At startup
Konstant.initAppConfig {
sources {
+EnvSource()
+TomlSource.fromResource("config.toml")
}
}

// Anywhere else, including nested specs
val db = Konstant.get<DatabaseConfig>()
val port = Konstant[AppConfig::class].server.port

The configs are published once as an immutable snapshot, so reads are safe from any thread on every platform. Initializing twice fails; call reset between tests.

Properties

Link copied to clipboard

True once a config has been installed.

Functions

Link copied to clipboard
inline fun <T : Any> get(): T

The loaded config of type T, root or nested.

operator fun <T : Any> get(type: KClass<T>): T

The loaded config of type, root or nested, so Konstant[AppConfig::class] works.

Link copied to clipboard
inline fun <T : Any> getOrNull(): T?

The loaded config of type T, or null if there is none.

fun <T : Any> getOrNull(type: KClass<T>): T?

The loaded config of type, or null if there is none. Also null when type appears more than once among the nested specs, since it is ambiguous.

Link copied to clipboard
fun install(root: Any, nested: List<Any> = emptyList())

Publishes root and its nested specs. Called by the generated Konstant.init<Spec> functions, which pass every nested spec; prefer those.

Link copied to clipboard
fun reset()

Clears the loaded config. Meant for tests.