mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-12-22 05:57:11 +00:00
add koin extensions for plugins
This commit is contained in:
parent
d928db7e74
commit
c2d6afccc2
@ -26,9 +26,7 @@ object HelloPlugin : Plugin {
|
||||
)
|
||||
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
single {
|
||||
get<Json>().decodeFromJsonElement(HelloPluginConfig.serializer(), config["helloPlugin"] ?: return@single null)
|
||||
}
|
||||
registerConfig<HelloPluginConfig>("helloPlugin")
|
||||
}
|
||||
|
||||
private sealed interface InternalFSMState : State {
|
||||
@ -43,7 +41,7 @@ object HelloPlugin : Plugin {
|
||||
}
|
||||
|
||||
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.dS { getMe().toString() }
|
||||
onCommand("hello_world") {
|
||||
|
@ -1,11 +1,59 @@
|
||||
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.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.core.scope.Scope
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
val Scope.database: Database
|
||||
get() = get()
|
||||
|
||||
val Koin.database: Database
|
||||
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>()
|
||||
|
Loading…
Reference in New Issue
Block a user