mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-22 07:33:46 +00:00
fix of #9
This commit is contained in:
parent
cf6b06b41d
commit
58eaa86419
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
## 0.1.4
|
## 0.1.4
|
||||||
|
|
||||||
|
* `Versions`
|
||||||
|
* `sdi`: `0.4.0-rc2` -> `0.4.1`
|
||||||
|
* `tgbotapi`: `0.32.7` -> `0.32.8`
|
||||||
|
* `microutils`: `0.4.25` -> `0.4.26`
|
||||||
|
|
||||||
## 0.1.3
|
## 0.1.3
|
||||||
|
|
||||||
* `Versions`
|
* `Versions`
|
||||||
|
@ -25,6 +25,9 @@ dependencies {
|
|||||||
kapt "com.github.matfax.klassindex:processor:$klassindex_version"
|
kapt "com.github.matfax.klassindex:processor:$klassindex_version"
|
||||||
api "org.xerial:sqlite-jdbc:$sqlite_version"
|
api "org.xerial:sqlite-jdbc:$sqlite_version"
|
||||||
|
|
||||||
|
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
|
||||||
|
testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-jvm:$kotlin_version"
|
||||||
|
|
||||||
api project(":plagubot.plugin")
|
api project(":plagubot.plugin")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,10 @@ package dev.inmo.plagubot
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
||||||
import dev.inmo.plagubot.config.*
|
import dev.inmo.plagubot.config.*
|
||||||
import dev.inmo.plagubot.config.configSerialFormat
|
import dev.inmo.plagubot.config.configJsonFormat
|
||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
|
||||||
import dev.inmo.tgbotapi.types.botCommandsLimit
|
import dev.inmo.tgbotapi.types.botCommandsLimit
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.serialization.InternalSerializationApi
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
@ -42,7 +41,7 @@ suspend inline fun initPlaguBot(
|
|||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val (configPath) = args
|
val (configPath) = args
|
||||||
val file = File(configPath)
|
val file = File(configPath)
|
||||||
val config = configSerialFormat.decodeFromString(Config.serializer(), file.readText())
|
val config = configJsonFormat.decodeFromString(ConfigSerializer, file.readText())
|
||||||
|
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
val scope = CoroutineScope(Dispatchers.Default)
|
||||||
initPlaguBot(config, scope).join()
|
initPlaguBot(config, scope).join()
|
||||||
|
@ -2,9 +2,13 @@ package dev.inmo.plagubot.config
|
|||||||
|
|
||||||
import com.github.matfax.klassindex.KlassIndex
|
import com.github.matfax.klassindex.KlassIndex
|
||||||
import dev.inmo.plagubot.Plugin
|
import dev.inmo.plagubot.Plugin
|
||||||
import dev.inmo.sdi.Module
|
import dev.inmo.plagubot.PluginSerializer
|
||||||
|
import dev.inmo.sdi.*
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
|
import kotlinx.serialization.encoding.Decoder
|
||||||
|
import kotlinx.serialization.encoding.Encoder
|
||||||
|
import kotlinx.serialization.json.*
|
||||||
import kotlinx.serialization.modules.*
|
import kotlinx.serialization.modules.*
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
|
|
||||||
@ -12,7 +16,7 @@ import kotlin.reflect.KClass
|
|||||||
internal inline fun <T : Plugin> KClass<T>.includeIn(builder: PolymorphicModuleBuilder<Plugin>) = builder.subclass(this, serializer())
|
internal inline fun <T : Plugin> KClass<T>.includeIn(builder: PolymorphicModuleBuilder<Plugin>) = builder.subclass(this, serializer())
|
||||||
|
|
||||||
@InternalSerializationApi
|
@InternalSerializationApi
|
||||||
internal val configSerialFormat: StringFormat
|
internal val configJsonFormat: Json
|
||||||
get() = Json {
|
get() = Json {
|
||||||
ignoreUnknownKeys = true
|
ignoreUnknownKeys = true
|
||||||
serializersModule = SerializersModule {
|
serializersModule = SerializersModule {
|
||||||
@ -37,8 +41,126 @@ internal val configSerialFormat: StringFormat
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Config(
|
data class Config(
|
||||||
val plugins: List<Plugin>,
|
val plugins: List<@Contextual Plugin>,
|
||||||
val database: DatabaseConfig = DatabaseConfig(),
|
val database: DatabaseConfig = DatabaseConfig(),
|
||||||
val botToken: String,
|
val botToken: String,
|
||||||
|
@Contextual
|
||||||
val params: Module? = null
|
val params: Module? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@Serializer(Plugin::class)
|
||||||
|
private class InternalPluginSerializer(
|
||||||
|
private val params: Module
|
||||||
|
) : KSerializer<Plugin> {
|
||||||
|
override val descriptor: SerialDescriptor = PluginSerializer.descriptor
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun deserialize(decoder: Decoder): Plugin {
|
||||||
|
val asJson = JsonElement.serializer().deserialize(decoder)
|
||||||
|
|
||||||
|
return if (asJson is JsonPrimitive) {
|
||||||
|
params[asJson.jsonPrimitive.content] as Plugin
|
||||||
|
} else {
|
||||||
|
val jsonFormat = ((decoder as? JsonDecoder)?.json ?: configJsonFormat)
|
||||||
|
jsonFormat.decodeFromJsonElement(PluginSerializer, asJson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun serialize(encoder: Encoder, value: Plugin) {
|
||||||
|
params.keys.firstOrNull { params[it] === value } ?.also {
|
||||||
|
encoder.encodeString(it)
|
||||||
|
} ?: PluginSerializer.serialize(encoder, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val DefaultModuleSerializer = ModuleSerializer(emptyList()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializer(Module::class)
|
||||||
|
private class InternalModuleSerializer(
|
||||||
|
private val original: JsonElement?,
|
||||||
|
private val params: Module
|
||||||
|
) : KSerializer<Module> {
|
||||||
|
override val descriptor: SerialDescriptor = PluginSerializer.descriptor
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun deserialize(decoder: Decoder): Module {
|
||||||
|
val asJson = JsonElement.serializer().deserialize(decoder)
|
||||||
|
|
||||||
|
return if (asJson == original) {
|
||||||
|
params
|
||||||
|
} else {
|
||||||
|
configJsonFormat.decodeFromJsonElement(DefaultModuleSerializer, asJson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun serialize(encoder: Encoder, value: Module) = DefaultModuleSerializer.serialize(encoder, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun internalPluginSerializerSerializersModule(
|
||||||
|
internalPluginSerializer: InternalPluginSerializer,
|
||||||
|
internalModuleSerializer: InternalModuleSerializer?
|
||||||
|
) = SerializersModule {
|
||||||
|
contextual(internalPluginSerializer)
|
||||||
|
contextual(internalModuleSerializer ?: return@SerializersModule)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializer(Config::class)
|
||||||
|
internal object ConfigSerializer : KSerializer<Config> {
|
||||||
|
private val jsonSerializer = JsonObject.serializer()
|
||||||
|
private val moduleSerializer = ModuleSerializer()
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun deserialize(decoder: Decoder): Config {
|
||||||
|
val json = jsonSerializer.deserialize(decoder)
|
||||||
|
val jsonFormat = (decoder as? JsonDecoder) ?.json ?: configJsonFormat
|
||||||
|
val paramsRow = json["params"]
|
||||||
|
|
||||||
|
val resultJsonFormat = if (paramsRow != null && paramsRow != JsonNull) {
|
||||||
|
val params = jsonFormat.decodeFromJsonElement(
|
||||||
|
moduleSerializer,
|
||||||
|
paramsRow
|
||||||
|
)
|
||||||
|
|
||||||
|
val pluginsSerializer = InternalPluginSerializer(params)
|
||||||
|
val moduleSerializer = InternalModuleSerializer(paramsRow, params)
|
||||||
|
Json(jsonFormat) {
|
||||||
|
serializersModule = decoder.serializersModule.overwriteWith(
|
||||||
|
internalPluginSerializerSerializersModule(pluginsSerializer, moduleSerializer)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
jsonFormat
|
||||||
|
}
|
||||||
|
return resultJsonFormat.decodeFromJsonElement(
|
||||||
|
Config.serializer(),
|
||||||
|
json
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@InternalSerializationApi
|
||||||
|
override fun serialize(encoder: Encoder, value: Config) {
|
||||||
|
if (value.params != null) {
|
||||||
|
val pluginsSerializer = InternalPluginSerializer(value.params)
|
||||||
|
|
||||||
|
val jsonFormat = Json(configJsonFormat) {
|
||||||
|
serializersModule = encoder.serializersModule.overwriteWith(
|
||||||
|
internalPluginSerializerSerializersModule(pluginsSerializer, null)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonSerializer.serialize(
|
||||||
|
encoder,
|
||||||
|
jsonFormat.encodeToJsonElement(
|
||||||
|
Config.serializer(),
|
||||||
|
value
|
||||||
|
) as JsonObject
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Config.serializer().serialize(encoder, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
37
bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt
Normal file
37
bot/src/test/kotlin/dev/inmo/plagubot/config/ConfigTest.kt
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package dev.inmo.plagubot.config
|
||||||
|
|
||||||
|
import dev.inmo.plagubot.HelloPlugin
|
||||||
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class ConfigTest {
|
||||||
|
@InternalSerializationApi
|
||||||
|
@Test
|
||||||
|
fun testThatPluginPassedToParamsWillBeCorrectlyUsedInPlugins() {
|
||||||
|
val rawConfig = """
|
||||||
|
{
|
||||||
|
"database": {
|
||||||
|
},
|
||||||
|
"botToken": "",
|
||||||
|
"plugins": [
|
||||||
|
"helloPlugin"
|
||||||
|
],
|
||||||
|
"params": {
|
||||||
|
"helloPlugin": {"type": "dev.inmo.plagubot.HelloPlugin", "parameter": "Example"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
val config = configJsonFormat.decodeFromString(ConfigSerializer, rawConfig)
|
||||||
|
|
||||||
|
assert(config.plugins.size == 1)
|
||||||
|
assert(config.plugins.first() is HelloPlugin)
|
||||||
|
assert((config.plugins.first() as HelloPlugin).parameter == "Example")
|
||||||
|
|
||||||
|
val redecoded = configJsonFormat.decodeFromString(ConfigSerializer, configJsonFormat.encodeToString(ConfigSerializer, config))
|
||||||
|
assertEquals(config.database, redecoded.database)
|
||||||
|
assertEquals(config.plugins, redecoded.plugins)
|
||||||
|
assertEquals(config.botToken, redecoded.botToken)
|
||||||
|
assertEquals(config.params ?.toMap(), redecoded.params ?.toMap())
|
||||||
|
}
|
||||||
|
}
|
@ -9,9 +9,9 @@ kotlin_coroutines_version=1.4.2
|
|||||||
kotlin_serialisation_runtime_version=1.1.0-RC
|
kotlin_serialisation_runtime_version=1.1.0-RC
|
||||||
kotlin_exposed_version=0.29.1
|
kotlin_exposed_version=0.29.1
|
||||||
|
|
||||||
sdi_version=0.4.0-rc2
|
sdi_version=0.4.1
|
||||||
tgbotapi_version=0.32.7
|
tgbotapi_version=0.32.8
|
||||||
microutils_version=0.4.25
|
microutils_version=0.4.26
|
||||||
klassindex_version=4.1.0-rc.1
|
klassindex_version=4.1.0-rc.1
|
||||||
|
|
||||||
sqlite_version=3.30.1
|
sqlite_version=3.30.1
|
||||||
|
Loading…
Reference in New Issue
Block a user