Skip to content

Commit

Permalink
improve sealed serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Dec 16, 2024
1 parent c2c2fcd commit 990bfd3
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.vendeli.tgbot.types.ReactionType
import eu.vendeli.tgbot.utils.builders.ListingBuilder
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer
import eu.vendeli.tgbot.utils.toJsonElement
import kotlinx.serialization.builtins.ListSerializer

Expand All @@ -24,7 +23,7 @@ class SetMessageReactionAction(

init {
parameters["message_id"] = messageId.toJsonElement()
if (reaction != null) parameters["reaction"] = reaction.encodeWith(ListSerializer(DynamicLookupSerializer))
if (reaction != null) parameters["reaction"] = reaction.encodeWith(ListSerializer(ReactionType.serializer()))
if (isBig != null) parameters["is_big"] = messageId.toJsonElement()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import eu.vendeli.tgbot.types.internal.options.AnswerInlineQueryOptions
import eu.vendeli.tgbot.utils.builders.ListingBuilder
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer
import eu.vendeli.tgbot.utils.toJsonElement
import kotlinx.serialization.builtins.ListSerializer

Expand All @@ -27,7 +26,7 @@ class AnswerInlineQueryAction(

init {
parameters["inline_query_id"] = inlineQueryId.toJsonElement()
parameters["results"] = results.encodeWith(ListSerializer(DynamicLookupSerializer))
parameters["results"] = results.encodeWith(ListSerializer(InlineQueryResult.serializer()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.vendeli.tgbot.types.SentWebAppMessage
import eu.vendeli.tgbot.types.inline.InlineQueryResult
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer
import eu.vendeli.tgbot.utils.toJsonElement

@TgAPI
Expand All @@ -22,7 +21,7 @@ class AnswerWebAppQueryAction(

init {
parameters["web_app_query_id"] = webAppQueryId.toJsonElement()
parameters["result"] = result.encodeWith(DynamicLookupSerializer)
parameters["result"] = result.encodeWith(InlineQueryResult.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import eu.vendeli.tgbot.types.internal.options.SavePreparedInlineMessageOptions
import eu.vendeli.tgbot.types.msg.PreparedInlineMessage
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer
import eu.vendeli.tgbot.utils.toJsonElement

@TgAPI
Expand All @@ -26,7 +25,7 @@ class SavePreparedInlineMessageAction(

init {
parameters["user_id"] = userId.toJsonElement()
parameters["result"] = result.encodeWith(DynamicLookupSerializer)
parameters["result"] = result.encodeWith(InlineQueryResult.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import eu.vendeli.tgbot.interfaces.action.Action
import eu.vendeli.tgbot.types.keyboard.MenuButton
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer

@TgAPI
class SetChatMenuButtonAction(
Expand All @@ -18,7 +17,7 @@ class SetChatMenuButtonAction(
override val returnType = getReturnType()

init {
parameters["menu_button"] = menuButton.encodeWith(DynamicLookupSerializer)
parameters["menu_button"] = menuButton.encodeWith(MenuButton.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SendMediaGroupAction(
"All elements must be of the same specific type and animation is not supported by telegram api"
}

handleImplicitFileGroup(media)
handleImplicitFileGroup(media, serializer = InputMedia.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SendPaidMediaAction(

init {
parameters["star_count"] = starCount.toJsonElement()
handleImplicitFileGroup(media)
handleImplicitFileGroup(media, serializer = InputPaidMedia.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import eu.vendeli.tgbot.types.media.InputMedia
import eu.vendeli.tgbot.types.msg.Message
import eu.vendeli.tgbot.utils.encodeWith
import eu.vendeli.tgbot.utils.getReturnType
import eu.vendeli.tgbot.utils.serde.DynamicLookupSerializer
import eu.vendeli.tgbot.utils.toJsonElement
import eu.vendeli.tgbot.utils.transform

Expand All @@ -25,13 +24,13 @@ class EditMessageMediaAction :

constructor(media: InputMedia) {
media.media = media.media.transform(multipartData)
parameters["media"] = media.encodeWith(DynamicLookupSerializer)
parameters["media"] = media.encodeWith(InputMedia.serializer())
}

constructor(messageId: Long, media: InputMedia) {
media.media = media.media.transform(multipartData)
parameters["message_id"] = messageId.toJsonElement()
parameters["media"] = media.encodeWith(DynamicLookupSerializer)
parameters["media"] = media.encodeWith(InputMedia.serializer())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package eu.vendeli.tgbot.types

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

/**
* This object describes the way a background is filled based on the selected colors. Currently, it can be one of
Expand All @@ -15,9 +17,9 @@ import kotlinx.serialization.Serializable
*/
@Serializable
sealed class BackgroundFill {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package eu.vendeli.tgbot.types

import eu.vendeli.tgbot.types.media.Document
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

/**
* This object describes the type of a background. Currently, it can be one of
Expand All @@ -17,9 +19,9 @@ import kotlinx.serialization.Serializable
*/
@Serializable
sealed class BackgroundType {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package eu.vendeli.tgbot.types

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

@Serializable
enum class EmojiType(
Expand Down Expand Up @@ -239,9 +241,9 @@ enum class EmojiType(
*/
@Serializable
sealed class ReactionType {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package eu.vendeli.tgbot.types.boost

import eu.vendeli.tgbot.types.User
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.serializer

@Serializable
@JsonClassDiscriminator("source")
@OptIn(ExperimentalSerializationApi::class)
sealed class ChatBoostSource {
abstract val user: User?

@OptIn(InternalSerializationApi::class)
val source: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package eu.vendeli.tgbot.types.bot

import eu.vendeli.tgbot.annotations.internal.TgAPI
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

/**
* This object represents the scope to which bot commands are applied. Currently, the following 7 scopes are supported:
Expand All @@ -20,9 +22,9 @@ import kotlinx.serialization.Serializable
*/
@Serializable
sealed class BotCommandScope {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import eu.vendeli.tgbot.types.User
import eu.vendeli.tgbot.utils.serde.InstantSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.serializer

@Serializable
@OptIn(ExperimentalSerializationApi::class)
@JsonClassDiscriminator("status")
sealed class ChatMember : MultipleResponse {
abstract val user: User

@OptIn(InternalSerializationApi::class)
val status: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import eu.vendeli.tgbot.types.InputMessageContent
import eu.vendeli.tgbot.types.ParseMode
import eu.vendeli.tgbot.types.keyboard.InlineKeyboardMarkup
import eu.vendeli.tgbot.types.msg.MessageEntity
import eu.vendeli.tgbot.types.stars.TransactionPartner
import kotlinx.serialization.Contextual
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseContextualSerialization
import kotlinx.serialization.serializer

/**
* This object represents one result of an inline query. Telegram clients currently support results of the following 20 types:
Expand Down Expand Up @@ -53,11 +54,9 @@ import kotlinx.serialization.UseContextualSerialization
*/
@Serializable
sealed class InlineQueryResult {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
TransactionPartner.Companion
.serializer()
.descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.vendeli.tgbot.types.keyboard

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

/**
* This object describes the bot's menu button in a private chat. It should be one of
Expand All @@ -14,21 +17,24 @@ import kotlinx.serialization.Serializable
*
*/
@Serializable
sealed class MenuButton(
open val type: String,
) {
sealed class MenuButton {
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
this::class.serializer().descriptor.serialName
}

@Serializable
@SerialName("commands")
class Commands : MenuButton(type = "commands")
class Commands : MenuButton()

@Serializable
@SerialName("web_app")
data class WebApp(
val text: String,
val webApp: WebAppInfo,
) : MenuButton(type = "web_app")
) : MenuButton()

@Serializable
@SerialName("default")
class Default : MenuButton(type = "default")
class Default : MenuButton()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import eu.vendeli.tgbot.types.internal.InputFile
import eu.vendeli.tgbot.types.msg.MessageEntity
import eu.vendeli.tgbot.utils.toImplicitFile
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

/**
* This object represents the content of a media message to be sent. It should be one of
Expand All @@ -24,9 +26,9 @@ import kotlinx.serialization.Serializable
@Serializable
@Suppress("OVERRIDE_DEPRECATION")
sealed class InputMedia : ImplicitMediaData {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import eu.vendeli.tgbot.types.internal.ImplicitFile
import eu.vendeli.tgbot.types.internal.InputFile
import eu.vendeli.tgbot.utils.toImplicitFile
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.serializer

@Serializable
@Suppress("OVERRIDE_DEPRECATION")
sealed class InputPaidMedia : ImplicitMediaData {
@OptIn(ExperimentalSerializationApi::class)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
val type: String by lazy {
serializer().descriptor.serialName
this::class.serializer().descriptor.serialName
}

@Serializable
Expand Down
Loading

0 comments on commit 990bfd3

Please sign in to comment.