From 704457715d7dc3cec84f21535cf792ef0b0ef2b6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 18 Aug 2019 12:31:33 +0600 Subject: [PATCH] RawGame added --- CHANGELOG.md | 5 +- .../TelegramBotAPI/types/games/Game.kt | 49 ------------------- .../TelegramBotAPI/types/games/RawGame.kt | 35 +++++++++++++ .../types/message/RawMessage.kt | 7 ++- 4 files changed, 41 insertions(+), 55 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f08f9a54c..16bf90fa5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # TelegramBotAPI changelog -## 0.18.0 +## 0.18.0 Raws cleaning * Made internal and not available outside of library: * `RawMessage` @@ -17,8 +17,9 @@ * `TelegramBotAPIMessageDeserializeOnlySerializer` was created. It **MUST NOT** be used to serialize messages * Update of description * Make `Game` object a little bit more standartizated +* `Game` now is not serializable and have no additional trash, related to serialization -## 0.17.0 +## 0.17.0 July 29, 2019 API Update Libraries updates: diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt index 3b9af64398..eb8f033298 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/Game.kt @@ -9,23 +9,14 @@ import kotlinx.serialization.internal.StringDescriptor import kotlinx.serialization.json.* import java.lang.UnsupportedOperationException -@Serializable(GameSerializer::class) data class Game( - @SerialName(titleField) override val title: String, - @SerialName(descriptionField) val description: String, - @Serializable(PhotoSerializer::class) - @SerialName(photoField) val photo: Photo, - @SerialName(textField) override val caption: String? = null, - @SerialName(textEntitiesField) override val captionEntities: List = emptyList(), - @SerialName(animationField) val animation: AnimationFile? = null ) : Titled, CaptionedInput { - @Deprecated( "Missinterfaced field", ReplaceWith("caption") @@ -42,43 +33,3 @@ data class Game( val textEntities: List? get() = captionEntities } - -internal object GameSerializer : KSerializer { - override val descriptor: SerialDescriptor = StringDescriptor.withName("GameSerializer") - - override fun deserialize(decoder: Decoder): Game { - val json = JsonObjectSerializer.deserialize(decoder) - - val text = json.getPrimitiveOrNull(textField) ?.content - - return Game( - json.getPrimitive(titleField).content, - json.getPrimitive(descriptionField).content, - Json.nonstrict.fromJson( - PhotoSerializer, - json.getObject(photoField) - ), - text, - text ?.let { - Json.nonstrict.fromJson( - RawMessageEntitiesSerializer, - json.getArray(textEntitiesField) - ).map { - it.asMessageEntity(text) - } - } ?: emptyList(), - json.getObjectOrNull( - animationField - ) ?.let { animatedFileJson -> - Json.nonstrict.fromJson( - AnimationFile.serializer(), - animatedFileJson - ) - } - ) - } - - override fun serialize(encoder: Encoder, obj: Game) = throw UnsupportedOperationException( - "Objects of class Game can't be serialized for now" - ) -} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt new file mode 100644 index 0000000000..b08c4f1af1 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/games/RawGame.kt @@ -0,0 +1,35 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.games + +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.* +import com.github.insanusmokrassar.TelegramBotAPI.types.files.* +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient + +@Serializable +internal data class RawGame( + @SerialName(titleField) + private val title: String, + @SerialName(descriptionField) + private val description: String, + @Serializable(PhotoSerializer::class) + @SerialName(photoField) + private val photo: Photo, + @SerialName(textField) + private val caption: String? = null, + @SerialName(textEntitiesField) + private val captionEntities: RawMessageEntities = emptyList(), + @SerialName(animationField) + private val animation: AnimationFile? = null +) { + @Transient + val asGame = Game( + title, + description, + photo, + caption, + caption ?.let { _ -> captionEntities.map { it.asMessageEntity(caption) } } ?: emptyList(), + animation + ) +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index 16be55b091..1722678edf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMa import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.files.* import com.github.insanusmokrassar.TelegramBotAPI.types.games.Game +import com.github.insanusmokrassar.TelegramBotAPI.types.games.RawGame import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.ChatEvents.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message @@ -44,15 +45,13 @@ internal data class RawMessage( private val media_group_id: MediaGroupIdentifier? = null, private val author_signature: AuthorSignature? = null, private val text: String? = null, - @Serializable(RawMessageEntitiesSerializer::class) private val entities: RawMessageEntities? = null, private val caption: String? = null, - @Serializable(RawMessageEntitiesSerializer::class) private val caption_entities: RawMessageEntities? = null, private val audio: AudioFile? = null, private val document: DocumentFile? = null, private val animation: AnimationFile? = null, - private val game: Game? = null, + private val game: RawGame? = null, @Serializable(PhotoSerializer::class) private val photo: Photo? = null, private val sticker: Sticker? = null, @@ -128,7 +127,7 @@ internal data class RawMessage( adaptedCaptionEntities ) sticker != null -> StickerContent(sticker) - game != null -> GameContent(game) + game != null -> GameContent(game.asGame) video_note != null -> VideoNoteContent(video_note) contact != null -> ContactContent(contact) location != null -> LocationContent(location)