improve registerConfig

This commit is contained in:
InsanusMokrassar 2024-09-22 18:33:22 +06:00
parent c2d6afccc2
commit a7fe1e3d82
2 changed files with 13 additions and 11 deletions

View File

@ -26,7 +26,7 @@ object HelloPlugin : Plugin {
)
override fun Module.setupDI(config: JsonObject) {
registerConfig<HelloPluginConfig>("helloPlugin")
registerConfig<HelloPluginConfig>("helloPlugin") { null }
}
private sealed interface InternalFSMState : State {

View File

@ -20,16 +20,18 @@ val Koin.database: Database
/**
* Using [single] to register `T` with serializer [configSerializer]
*
* @param optional If passed, absence of [field] in config will lead to null config value in koin. If passed as false
* (default), absence of [field] in config will lead to an error
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
* will be thrown
*/
inline fun <reified T> Module.registerConfig(configSerializer: KSerializer<T>, field: String?, optional: Boolean = false) {
inline fun <reified T> Module.registerConfig(configSerializer: KSerializer<T>, field: String?, noinline default: (Scope.(JsonObject) -> T?)? = null) {
single {
val fieldValue = get<JsonObject>().let {
if (field == null) {
it
} else {
it[field] ?: if (optional) return@single null else error("Unable to take field $field from config")
it[field] ?: default ?.let { _ ->
return@single default(it)
} ?: error("Unable to take field $field from config")
}
}
get<Json>().decodeFromJsonElement(configSerializer, fieldValue)
@ -39,19 +41,19 @@ inline fun <reified T> Module.registerConfig(configSerializer: KSerializer<T>, f
/**
* Using [single] to register config with getting of [serializer] from [kClass]
*
* @param optional If passed, absence of [field] in config will lead to null config value in koin. If passed as false
* (default), absence of [field] in config will lead to an error
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
* will be thrown
*/
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> Module.registerConfig(kClass: KClass<T>, field: String?, optional: Boolean = false) = registerConfig(kClass.serializer(), field, optional)
inline fun <reified T : Any> Module.registerConfig(kClass: KClass<T>, field: String?, noinline default: (Scope.(JsonObject) -> T?)? = null) = registerConfig(kClass.serializer(), field, default)
/**
* Using [single] to register config with getting of [serializer] from [kClass]
*
* @param optional If passed, absence of [field] in config will lead to null config value in koin. If passed as false
* (default), absence of [field] in config will lead to an error
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
* will be thrown
*/
inline fun <reified T : Any> Module.registerConfig(field: String?, optional: Boolean = false) = registerConfig(T::class, field, optional)
inline fun <reified T : Any> Module.registerConfig(field: String?, noinline default: (Scope.(JsonObject) -> T?)? = null) = registerConfig(T::class, field, default)
inline fun <reified T : Any> Scope.config() = get<T>()
inline fun <reified T : Any> Koin.config() = get<T>()