From 128e69e3e3851dd1ace4472ae8f370c54d9ef0bc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Nov 2019 12:25:22 +0600 Subject: [PATCH] adapt code to new versions --- CHANGELOG.md | 4 ++- .../TelegramBotAPI/types/ChatIdentifier.kt | 2 +- .../types/ParseMode/ParseMode.kt | 2 +- .../TelegramBotAPI/types/TelegramDate.kt | 2 +- .../TelegramBotAPI/types/actions/BotAction.kt | 26 +++++++++---------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a48d3f821..e012496414 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,9 @@ * `kotlin serialization`: 0.11.1 -> 0.14.0 * `joda time`: 2.10.3 -> 2.10.5 * `ktor`: 1.2.3 -> 1.2.5 - +* `BotAction` now will be deserialized in a little bit other way + * `BotActionSerializer` now is internal +* Most part of serializers now are objects (instead of classes as was previously) ## 0.17.0 July 29, 2019 API Update diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index c1d5928304..d184438d5e 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -37,7 +37,7 @@ data class Username( fun String.toUsername(): Username = Username(this) @Serializer(ChatIdentifier::class) -internal class ChatIdentifierSerializer: KSerializer { +internal object ChatIdentifierSerializer : KSerializer { override fun deserialize(decoder: Decoder): ChatIdentifier { val id = decoder.decodeString() return id.toLongOrNull() ?.let { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt index 98a1d1b1b4..be80eb450c 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ParseMode/ParseMode.kt @@ -27,7 +27,7 @@ typealias Markdown = MarkdownParseMode typealias HTML = HTMLParseMode @Serializer(ParseMode::class) -internal class ParseModeSerializerObject: KSerializer { +internal object ParseModeSerializerObject : KSerializer { override fun deserialize(decoder: Decoder): ParseMode { val mode = decoder.decodeString() return when (mode) { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt index b83ec0ace6..9b53bc3b2b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/TelegramDate.kt @@ -21,7 +21,7 @@ class TelegramDate( fun DateTime.toTelegramDate(): TelegramDate = TelegramDate(this) @Serializer(TelegramDate::class) -internal class TelegramDateSerializer: KSerializer { +internal object TelegramDateSerializer : KSerializer { override fun serialize(encoder: Encoder, obj: TelegramDate) { encoder.encodeLong( TimeUnit.MILLISECONDS.toSeconds(obj.asDate.millis) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt index 4dde4ccf52..8563a874c7 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/actions/BotAction.kt @@ -2,31 +2,29 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.actions import kotlinx.serialization.* -private val actions = listOf( - TypingAction, - UploadPhotoAction, - RecordVideoAction, - UploadVideoAction, - RecordAudioAction, - UploadAudioAction, - UploadDocumentAction, - FindLocationAction -) - @Serializable(BotActionSerializer::class) sealed class BotAction { abstract val actionName: String } @Serializer(BotAction::class) -class BotActionSerializer: KSerializer { +internal object BotActionSerializer: KSerializer { override fun serialize(encoder: Encoder, obj: BotAction) { encoder.encodeString(obj.actionName) } override fun deserialize(decoder: Decoder): BotAction { - val actionName = decoder.decodeString() - return actions.firstOrNull { it.actionName == actionName } ?: throw IllegalStateException("Unknown action type: $actionName") + return when (val actionName = decoder.decodeString()) { + TypingAction.actionName -> TypingAction + UploadPhotoAction.actionName -> UploadPhotoAction + RecordVideoAction.actionName -> RecordVideoAction + UploadVideoAction.actionName -> UploadVideoAction + RecordAudioAction.actionName -> RecordAudioAction + UploadAudioAction.actionName -> UploadAudioAction + UploadDocumentAction.actionName -> UploadDocumentAction + FindLocationAction.actionName -> FindLocationAction + else -> throw IllegalStateException("Unknown action type: $actionName") + } } }