From a7fe1e3d82ed7d19e467111ca22c0eea53caac05 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 22 Sep 2024 18:33:22 +0600 Subject: [PATCH] improve registerConfig --- .../kotlin/dev/inmo/plagubot/HelloPlugin.kt | 2 +- .../dev/inmo/plagubot/KoinExtensions.kt | 22 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt index c6a388b..7929336 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt @@ -26,7 +26,7 @@ object HelloPlugin : Plugin { ) override fun Module.setupDI(config: JsonObject) { - registerConfig("helloPlugin") + registerConfig("helloPlugin") { null } } private sealed interface InternalFSMState : State { diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt index c230e65..33a4cae 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt @@ -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 Module.registerConfig(configSerializer: KSerializer, field: String?, optional: Boolean = false) { +inline fun Module.registerConfig(configSerializer: KSerializer, field: String?, noinline default: (Scope.(JsonObject) -> T?)? = null) { single { val fieldValue = get().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().decodeFromJsonElement(configSerializer, fieldValue) @@ -39,19 +41,19 @@ inline fun Module.registerConfig(configSerializer: KSerializer, 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 Module.registerConfig(kClass: KClass, field: String?, optional: Boolean = false) = registerConfig(kClass.serializer(), field, optional) +inline fun Module.registerConfig(kClass: KClass, 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 Module.registerConfig(field: String?, optional: Boolean = false) = registerConfig(T::class, field, optional) +inline fun Module.registerConfig(field: String?, noinline default: (Scope.(JsonObject) -> T?)? = null) = registerConfig(T::class, field, default) inline fun Scope.config() = get() inline fun Koin.config() = get()