From 5e8bf82358c998f6f1db70bf728ee1690b093eaa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 2 Jun 2019 22:02:12 +0800 Subject: [PATCH] InlineKeyboardButton as sealed class --- CHANGELOG.md | 1 + ...URLInlineKeyboardButton.kt => LoginURL.kt} | 10 +--- .../CallbackDataInlineKeyboardButton.kt | 14 ------ .../InlineKeyboardButton.kt | 47 +++++++++++++++++-- .../InlineKeyboardButtonSerializer.kt | 44 +++++++++++++++++ .../PayInlineKeyboardButton.kt | 10 ---- ...ineQueryCurrentChatInlineKeyboardButton.kt | 12 ----- .../SwitchInlineQueryInlineKeyboardButton.kt | 11 ----- .../URLInlineKeyboardButton.kt | 9 ---- 9 files changed, 90 insertions(+), 68 deletions(-) rename src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/{buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt => LoginURL.kt} (55%) delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index e23c6f6230..209f60bbb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * `LoginURL` and `LoginURLInlineKeyboardButton` has been added * `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface * `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed +* `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file ## 0.15.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt similarity index 55% rename from src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt rename to src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt index fcf0bea1fa..13f223eebf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt @@ -1,16 +1,8 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +package com.github.insanusmokrassar.TelegramBotAPI.types -import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -@Serializable -data class LoginURLInlineKeyboardButton( - override val text: String, - @SerialName(loginUrlField) - val loginUrl: LoginURL -) : InlineKeyboardButton - @Serializable data class LoginURL( @SerialName(urlField) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt deleted file mode 100644 index 2610605f94..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import com.github.insanusmokrassar.TelegramBotAPI.types.callbackDataField -import com.github.insanusmokrassar.TelegramBotAPI.types.textField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class CallbackDataInlineKeyboardButton( - @SerialName(textField) - override val text: String, - @SerialName(callbackDataField) - val callbackData: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 35741de405..22b1a27dad 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -1,10 +1,51 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* @Serializable(InlineKeyboardButtonSerializer::class) -interface InlineKeyboardButton { - val text: String +sealed class InlineKeyboardButton { + abstract val text: String } -object InlineKeyboardButtonSerializer : KSerializer by ContextSerializer(InlineKeyboardButton::class) +//TODO:: add check that this button first in a row (it MUST be first in a row) +@Serializable +data class PayInlineKeyboardButton( + override val text: String, + val pay: Boolean +) : InlineKeyboardButton() + +@Serializable +data class CallbackDataInlineKeyboardButton( + @SerialName(textField) + override val text: String, + @SerialName(callbackDataField) + val callbackData: String +) : InlineKeyboardButton() + +@Serializable +data class LoginURLInlineKeyboardButton( + override val text: String, + @SerialName(loginUrlField) + val loginUrl: LoginURL +) : InlineKeyboardButton() + +@Serializable +data class SwitchInlineQueryCurrentChatInlineKeyboardButton( + override val text: String, + @SerialName(switchInlineQueryCurrentChatField) + val switchInlineQueryCurrentChat: String +) : InlineKeyboardButton() + +@Serializable +data class SwitchInlineQueryInlineKeyboardButton( + override val text: String, + @SerialName("switch_inline_query") + val switchInlineQuery: String +) : InlineKeyboardButton() + +@Serializable +data class URLInlineKeyboardButton( + override val text: String, + val url: String +) : InlineKeyboardButton() diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt new file mode 100644 index 0000000000..2f289e39a0 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -0,0 +1,44 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons + +import kotlinx.serialization.* +import kotlinx.serialization.internal.StringDescriptor +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElementSerializer + +object InlineKeyboardButtonSerializer : KSerializer { + override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton") + + private val serializers = listOf( + CallbackDataInlineKeyboardButton.serializer(), + LoginURLInlineKeyboardButton.serializer(), + PayInlineKeyboardButton.serializer(), + SwitchInlineQueryInlineKeyboardButton.serializer(), + SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(), + URLInlineKeyboardButton.serializer() + ) + + override fun deserialize(decoder: Decoder): InlineKeyboardButton { + val json = JsonElementSerializer.deserialize(decoder) + + serializers.forEach { + try { + return Json.nonstrict.fromJson(it, json) + } catch (e: SerializationException) { + e + } + } + + throw IllegalArgumentException("There is no known type of serializer for \"$json\" as inline button") + } + + override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) { + when (obj) { + is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, obj) + is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, obj) + is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, obj) + is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, obj) + is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, obj) + is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, obj) + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt deleted file mode 100644 index edc357b468..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.Serializable - -//TODO:: add check that this button first in a row (it MUST be first in a row) -@Serializable -data class PayInlineKeyboardButton( - override val text: String, - val pay: Boolean -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt deleted file mode 100644 index fdfc663c49..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import com.github.insanusmokrassar.TelegramBotAPI.types.switchInlineQueryCurrentChatField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class SwitchInlineQueryCurrentChatInlineKeyboardButton( - override val text: String, - @SerialName(switchInlineQueryCurrentChatField) - val switchInlineQueryCurrentChat: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt deleted file mode 100644 index a5134c6a8b..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class SwitchInlineQueryInlineKeyboardButton( - override val text: String, - @SerialName("switch_inline_query") - val switchInlineQuery: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt deleted file mode 100644 index 5272e43f29..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.Serializable - -@Serializable -data class URLInlineKeyboardButton( - override val text: String, - val url: String -) : InlineKeyboardButton