add koin extensions for plugins

This commit is contained in:
InsanusMokrassar 2024-09-22 18:12:46 +06:00
parent d928db7e74
commit c2d6afccc2
2 changed files with 50 additions and 4 deletions

View File

@ -26,9 +26,7 @@ object HelloPlugin : Plugin {
) )
override fun Module.setupDI(config: JsonObject) { override fun Module.setupDI(config: JsonObject) {
single { registerConfig<HelloPluginConfig>("helloPlugin")
get<Json>().decodeFromJsonElement(HelloPluginConfig.serializer(), config["helloPlugin"] ?: return@single null)
}
} }
private sealed interface InternalFSMState : State { private sealed interface InternalFSMState : State {
@ -43,7 +41,7 @@ object HelloPlugin : Plugin {
} }
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
val toPrint = koin.getOrNull<HelloPluginConfig>() ?.print ?: "Hello :)" val toPrint = koin.configOrNull<HelloPluginConfig>() ?.print ?: "Hello :)"
logger.d { toPrint } logger.d { toPrint }
logger.dS { getMe().toString() } logger.dS { getMe().toString() }
onCommand("hello_world") { onCommand("hello_world") {

View File

@ -1,11 +1,59 @@
package dev.inmo.plagubot package dev.inmo.plagubot
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.serializer
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.core.scope.Scope import org.koin.core.scope.Scope
import kotlin.reflect.KClass
val Scope.database: Database val Scope.database: Database
get() = get() get() = get()
val Koin.database: Database val Koin.database: Database
get() = get() get() = get()
/**
* 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
*/
inline fun <reified T> Module.registerConfig(configSerializer: KSerializer<T>, field: String?, optional: Boolean = false) {
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")
}
}
get<Json>().decodeFromJsonElement(configSerializer, fieldValue)
}
}
/**
* 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
*/
@OptIn(InternalSerializationApi::class)
inline fun <reified T : Any> Module.registerConfig(kClass: KClass<T>, field: String?, optional: Boolean = false) = registerConfig(kClass.serializer(), field, optional)
/**
* 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
*/
inline fun <reified T : Any> Module.registerConfig(field: String?, optional: Boolean = false) = registerConfig(T::class, field, optional)
inline fun <reified T : Any> Scope.config() = get<T>()
inline fun <reified T : Any> Koin.config() = get<T>()
inline fun <reified T : Any> Scope.configOrNull() = getOrNull<T>()
inline fun <reified T : Any> Koin.configOrNull() = getOrNull<T>()