From cdcfaf5a296aa6e8844b434a73e1889bc2ad27f4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 22 Jan 2021 23:14:24 +0600 Subject: [PATCH] fixes in passport errors serialization and replacing of PassportElementError --- .../requests/SetPassportDataErrors.kt | 2 +- .../{errors => }/PassportElementError.kt | 38 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) rename tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/{errors => }/PassportElementError.kt (75%) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/SetPassportDataErrors.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/SetPassportDataErrors.kt index 3bb3b34e74..d98d88574e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/SetPassportDataErrors.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/SetPassportDataErrors.kt @@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.requests import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.types.* -import dev.inmo.tgbotapi.types.passport.errors.PassportElementError +import dev.inmo.tgbotapi.types.passport.PassportElementError import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/errors/PassportElementError.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/PassportElementError.kt similarity index 75% rename from tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/errors/PassportElementError.kt rename to tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/PassportElementError.kt index d7526eee44..5fa154736a 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/errors/PassportElementError.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/passport/PassportElementError.kt @@ -1,11 +1,17 @@ -package dev.inmo.tgbotapi.types.passport.errors +@file:Suppress("unused", "EXPERIMENTAL_API_USAGE") + +package dev.inmo.tgbotapi.types.passport import dev.inmo.micro_utils.serialization.base64.Base64StringSerializer import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.* import dev.inmo.tgbotapi.types.passport.encrypted_data.type +import dev.inmo.tgbotapi.utils.nonstrictJsonFormat import kotlinx.serialization.* +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* @Serializable(PassportElementErrorSerializer::class) sealed class PassportElementError { @@ -14,8 +20,33 @@ sealed class PassportElementError { abstract val message: String } -@Serializer(PassportElementError::class) -internal object PassportElementErrorSerializer : KSerializer { +data class UnknownPassportElementError( + val raw: JsonObject +) : PassportElementError() { + override val source: String = raw[sourceField] ?.jsonPrimitive ?.contentOrNull ?: "" + override val type: String = raw[typeField] ?.jsonPrimitive ?.contentOrNull ?: "" + override val message: String = raw[messageField] ?.jsonPrimitive ?.contentOrNull ?: "" +} + +object PassportElementErrorSerializer : KSerializer { + private val jsonObjectSerializer = JsonObject.serializer() + override val descriptor: SerialDescriptor + get() = jsonObjectSerializer.descriptor + override fun deserialize(decoder: Decoder): PassportElementError { + val json = jsonObjectSerializer.deserialize(decoder) + return when (json[sourceField] ?.jsonPrimitive ?.content) { + "dataField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorDataField.serializer(), json) + "frontSideField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorFrontSide.serializer(), json) + "reverseSideField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorReverseSide.serializer(), json) + "selfieField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorSelfie.serializer(), json) + "fileField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementFileError.serializer(), json) + "filesField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementFilesError.serializer(), json) + "translationFileField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorTranslationFile.serializer(), json) + "translationFilesField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorTranslationFiles.serializer(), json) + "unspecifiedField" -> nonstrictJsonFormat.decodeFromJsonElement(PassportElementErrorUnspecified.serializer(), json) + else -> UnknownPassportElementError(json) + } + } override fun serialize(encoder: Encoder, value: PassportElementError) { val neverMindAboutThisVariable = when (value) { is PassportElementErrorFrontSide -> PassportElementErrorFrontSide.serializer().serialize(encoder, value) @@ -27,6 +58,7 @@ internal object PassportElementErrorSerializer : KSerializer PassportElementErrorDataField.serializer().serialize(encoder, value) is PassportElementErrorFiles -> PassportElementErrorFiles.serializer().serialize(encoder, value) is PassportElementErrorTranslationFiles -> PassportElementErrorTranslationFiles.serializer().serialize(encoder, value) + is UnknownPassportElementError -> jsonObjectSerializer.serialize(encoder, value.raw) } } }