mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-21 23:23:47 +00:00
improve registerConfig
This commit is contained in:
parent
c2d6afccc2
commit
a7fe1e3d82
@ -26,7 +26,7 @@ object HelloPlugin : Plugin {
|
|||||||
)
|
)
|
||||||
|
|
||||||
override fun Module.setupDI(config: JsonObject) {
|
override fun Module.setupDI(config: JsonObject) {
|
||||||
registerConfig<HelloPluginConfig>("helloPlugin")
|
registerConfig<HelloPluginConfig>("helloPlugin") { null }
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed interface InternalFSMState : State {
|
private sealed interface InternalFSMState : State {
|
||||||
|
@ -20,16 +20,18 @@ val Koin.database: Database
|
|||||||
/**
|
/**
|
||||||
* Using [single] to register `T` with serializer [configSerializer]
|
* 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
|
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
|
||||||
* (default), absence of [field] in config will lead to an 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 {
|
single {
|
||||||
val fieldValue = get<JsonObject>().let {
|
val fieldValue = get<JsonObject>().let {
|
||||||
if (field == null) {
|
if (field == null) {
|
||||||
it
|
it
|
||||||
} else {
|
} 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)
|
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]
|
* 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
|
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
|
||||||
* (default), absence of [field] in config will lead to an error
|
* will be thrown
|
||||||
*/
|
*/
|
||||||
@OptIn(InternalSerializationApi::class)
|
@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]
|
* 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
|
* @param default Will be used if [field] is absent as an alternative way of config allocation. If null passed, error
|
||||||
* (default), absence of [field] in config will lead to an 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> Scope.config() = get<T>()
|
||||||
inline fun <reified T : Any> Koin.config() = get<T>()
|
inline fun <reified T : Any> Koin.config() = get<T>()
|
||||||
|
Loading…
Reference in New Issue
Block a user