From 2a5f9a629d08ec994767d2cdd2579a1eeb89ae40 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 13 Apr 2023 13:58:20 +0600 Subject: [PATCH] fixes in TextSourceSerializer --- .../textsources/TextSourceSerializer.kt | 87 ++++++++++++++----- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/TextSourceSerializer.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/TextSourceSerializer.kt index e93d87f575..7ba5856e02 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/TextSourceSerializer.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/message/textsources/TextSourceSerializer.kt @@ -1,37 +1,78 @@ 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 kotlinx.serialization.KSerializer +import kotlinx.serialization.encoding.CompositeEncoder +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder -private val baseSerializers: Map> = 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(), -) +//private val baseSerializers: Map> = 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(), +//) + +object TextSourceSerializer : TypedSerializer(TextSource::class, emptyMap()) { + private val baseSerializers: Map> 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::class, baseSerializers) { override fun include(type: String, serializer: KSerializer) { - require(type !in baseSerializers.keys) + require(type !in serializers.keys) super.include(type, serializer) } override fun exclude(type: String) { - require(type !in baseSerializers.keys) + require(type !in serializers.keys) super.exclude(type) } }