1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

fixes in TextSourceSerializer

This commit is contained in:
InsanusMokrassar 2023-04-13 13:58:20 +06:00
parent 75b27858dd
commit 2a5f9a629d

View File

@ -1,37 +1,78 @@
package dev.inmo.tgbotapi.types.message.textsources package dev.inmo.tgbotapi.types.message.textsources
import dev.inmo.micro_utils.serialization.mapper.MapperSerializer
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.encoding.CompositeEncoder
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
private val baseSerializers: Map<String, KSerializer<out TextSource>> = mapOf( //private val baseSerializers: Map<String, KSerializer<out TextSource>> = mapOf(
"regular" to RegularTextSource.serializer(), // "regular" to RegularTextSource.serializer(),
"text_link" to TextLinkTextSource.serializer(), // "text_link" to TextLinkTextSource.serializer(),
"code" to CodeTextSource.serializer(), // "code" to CodeTextSource.serializer(),
"url" to URLTextSource.serializer(), // "url" to URLTextSource.serializer(),
"pre" to PreTextSource.serializer(), // "pre" to PreTextSource.serializer(),
"bot_command" to BotCommandTextSource.serializer(), // "bot_command" to BotCommandTextSource.serializer(),
"strikethrough" to StrikethroughTextSource.serializer(), // "strikethrough" to StrikethroughTextSource.serializer(),
"italic" to ItalicTextSource.serializer(), // "italic" to ItalicTextSource.serializer(),
"bold" to BoldTextSource.serializer(), // "bold" to BoldTextSource.serializer(),
"email" to EMailTextSource.serializer(), // "email" to EMailTextSource.serializer(),
"underline" to UnderlineTextSource.serializer(), // "underline" to UnderlineTextSource.serializer(),
"mention" to MentionTextSource.serializer(), // "mention" to MentionTextSource.serializer(),
"phone_number" to PhoneNumberTextSource.serializer(), // "phone_number" to PhoneNumberTextSource.serializer(),
"text_mention" to TextMentionTextSource.serializer(), // "text_mention" to TextMentionTextSource.serializer(),
"hashtag" to HashTagTextSource.serializer(), // "hashtag" to HashTagTextSource.serializer(),
"cashtag" to CashTagTextSource.serializer(), // "cashtag" to CashTagTextSource.serializer(),
"spoiler" to SpoilerTextSource.serializer(), // "spoiler" to SpoilerTextSource.serializer(),
"custom_emoji" to CustomEmojiTextSource.serializer(), // "custom_emoji" to CustomEmojiTextSource.serializer(),
) //)
object TextSourceSerializer : TypedSerializer<TextSource>(TextSource::class, emptyMap()) {
private val baseSerializers: Map<String, KSerializer<out TextSource>> by lazy {
mapOf(
"regular" to RegularTextSource.serializer(),
"text_link" to TextLinkTextSource.serializer(),
"code" to CodeTextSource.serializer(),
"url" to URLTextSource.serializer(),
"pre" to PreTextSource.serializer(),
"bot_command" to BotCommandTextSource.serializer(),
"strikethrough" to StrikethroughTextSource.serializer(),
"italic" to ItalicTextSource.serializer(),
"bold" to BoldTextSource.serializer(),
"email" to EMailTextSource.serializer(),
"underline" to UnderlineTextSource.serializer(),
"mention" to MentionTextSource.serializer(),
"phone_number" to PhoneNumberTextSource.serializer(),
"text_mention" to TextMentionTextSource.serializer(),
"hashtag" to HashTagTextSource.serializer(),
"cashtag" to CashTagTextSource.serializer(),
"spoiler" to SpoilerTextSource.serializer(),
"custom_emoji" to CustomEmojiTextSource.serializer(),
).also {
it.forEach { (k, s) ->
include(k, s)
}
}
}
override fun serialize(encoder: Encoder, value: TextSource) {
baseSerializers // init base serializers
super.serialize(encoder, value)
}
override fun deserialize(decoder: Decoder): TextSource {
baseSerializers // init base serializers
return super.deserialize(decoder)
}
object TextSourceSerializer : TypedSerializer<TextSource>(TextSource::class, baseSerializers) {
override fun <T: TextSource> include(type: String, serializer: KSerializer<T>) { override fun <T: TextSource> include(type: String, serializer: KSerializer<T>) {
require(type !in baseSerializers.keys) require(type !in serializers.keys)
super.include(type, serializer) super.include(type, serializer)
} }
override fun exclude(type: String) { override fun exclude(type: String) {
require(type !in baseSerializers.keys) require(type !in serializers.keys)
super.exclude(type) super.exclude(type)
} }
} }