mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
update EncryptedElementSerializer
This commit is contained in:
parent
08b8710772
commit
9983e111ec
@ -12,7 +12,7 @@ klock_version=2.0.3
|
|||||||
uuid_version=0.2.3
|
uuid_version=0.2.3
|
||||||
ktor_version=1.5.0
|
ktor_version=1.5.0
|
||||||
|
|
||||||
micro_utils_version=0.4.19
|
micro_utils_version=0.4.20
|
||||||
|
|
||||||
javax_activation_version=1.1.1
|
javax_activation_version=1.1.1
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ kotlin {
|
|||||||
|
|
||||||
api "dev.inmo:micro_utils.coroutines:$micro_utils_version"
|
api "dev.inmo:micro_utils.coroutines:$micro_utils_version"
|
||||||
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
|
api "dev.inmo:micro_utils.serialization.base64:$micro_utils_version"
|
||||||
|
api "dev.inmo:micro_utils.serialization.encapsulator:$micro_utils_version"
|
||||||
|
|
||||||
api "io.ktor:ktor-client-core:$ktor_version"
|
api "io.ktor:ktor-client-core:$ktor_version"
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.types.passport.encrypted_data
|
package dev.inmo.tgbotapi.types.passport.encrypted_data
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.serialization.encapsulator.Encapsulator
|
||||||
import dev.inmo.tgbotapi.types.hashField
|
import dev.inmo.tgbotapi.types.hashField
|
||||||
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.EncryptedPassportElement
|
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.EncryptedPassportElement
|
||||||
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.UnknownEncryptedPassportElement
|
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.UnknownEncryptedPassportElement
|
||||||
@ -12,6 +13,22 @@ import kotlinx.serialization.encoding.Decoder
|
|||||||
import kotlinx.serialization.encoding.Encoder
|
import kotlinx.serialization.encoding.Encoder
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
|
|
||||||
|
val encryptedElementsClassesByTypes = mapOf(
|
||||||
|
"personal_details" to Encapsulator(EncryptedPersonalDetails::class, EncryptedPersonalDetails.serializer()),
|
||||||
|
"passport" to Encapsulator(CommonPassport::class, CommonPassport.serializer()),
|
||||||
|
"driver_license" to Encapsulator(DriverLicense::class, DriverLicense.serializer()),
|
||||||
|
"identity_card" to Encapsulator(IdentityCard::class, IdentityCard.serializer()),
|
||||||
|
"internal_passport" to Encapsulator(InternalPassport::class, InternalPassport.serializer()),
|
||||||
|
"address" to Encapsulator(EncryptedAddress::class, EncryptedAddress.serializer()),
|
||||||
|
"utility_bill" to Encapsulator(UtilityBill::class, UtilityBill.serializer()),
|
||||||
|
"bank_statement" to Encapsulator(BankStatement::class, BankStatement.serializer()),
|
||||||
|
"rental_agreement" to Encapsulator(RentalAgreement::class, RentalAgreement.serializer()),
|
||||||
|
"passport_registration" to Encapsulator(PassportRegistration::class, PassportRegistration.serializer()),
|
||||||
|
"temporary_registration" to Encapsulator(TemporaryRegistration::class, TemporaryRegistration.serializer()),
|
||||||
|
"phone_number" to Encapsulator(PhoneNumber::class, PhoneNumber.serializer()),
|
||||||
|
"email" to Encapsulator(Email::class, Email.serializer())
|
||||||
|
)
|
||||||
|
|
||||||
@Serializer(EncryptedPassportElement::class)
|
@Serializer(EncryptedPassportElement::class)
|
||||||
object EncryptedElementSerializer : KSerializer<EncryptedPassportElement> {
|
object EncryptedElementSerializer : KSerializer<EncryptedPassportElement> {
|
||||||
private val jsonSerializer = JsonObject.serializer()
|
private val jsonSerializer = JsonObject.serializer()
|
||||||
@ -19,69 +36,23 @@ object EncryptedElementSerializer : KSerializer<EncryptedPassportElement> {
|
|||||||
|
|
||||||
override fun deserialize(decoder: Decoder): EncryptedPassportElement {
|
override fun deserialize(decoder: Decoder): EncryptedPassportElement {
|
||||||
val json = jsonSerializer.deserialize(decoder)
|
val json = jsonSerializer.deserialize(decoder)
|
||||||
return when (json[typeField] ?.jsonPrimitive ?.content) {
|
return json[typeField] ?.jsonPrimitive ?.content ?.let { type ->
|
||||||
"personal_details" -> nonstrictJsonFormat.decodeFromJsonElement(EncryptedPersonalDetails.serializer(), json)
|
encryptedElementsClassesByTypes[type] ?.serializer ?.let { deserializer ->
|
||||||
"passport" -> nonstrictJsonFormat.decodeFromJsonElement(CommonPassport.serializer(), json)
|
nonstrictJsonFormat.decodeFromJsonElement(deserializer, json)
|
||||||
"driver_license" -> nonstrictJsonFormat.decodeFromJsonElement(DriverLicense.serializer(), json)
|
}
|
||||||
"identity_card" -> nonstrictJsonFormat.decodeFromJsonElement(IdentityCard.serializer(), json)
|
} ?: UnknownEncryptedPassportElement(json, json[hashField] ?.jsonPrimitive ?.content ?: "")
|
||||||
"internal_passport" -> nonstrictJsonFormat.decodeFromJsonElement(InternalPassport.serializer(), json)
|
|
||||||
"address" -> nonstrictJsonFormat.decodeFromJsonElement(EncryptedAddress.serializer(), json)
|
|
||||||
"utility_bill" -> nonstrictJsonFormat.decodeFromJsonElement(UtilityBill.serializer(), json)
|
|
||||||
"bank_statement" -> nonstrictJsonFormat.decodeFromJsonElement(BankStatement.serializer(), json)
|
|
||||||
"rental_agreement" -> nonstrictJsonFormat.decodeFromJsonElement(RentalAgreement.serializer(), json)
|
|
||||||
"passport_registration" -> nonstrictJsonFormat.decodeFromJsonElement(PassportRegistration.serializer(), json)
|
|
||||||
"temporary_registration" -> nonstrictJsonFormat.decodeFromJsonElement(TemporaryRegistration.serializer(), json)
|
|
||||||
"phone_number" -> nonstrictJsonFormat.decodeFromJsonElement(PhoneNumber.serializer(), json)
|
|
||||||
"email" -> nonstrictJsonFormat.decodeFromJsonElement(Email.serializer(), json)
|
|
||||||
else -> UnknownEncryptedPassportElement(json, json[hashField] ?.jsonPrimitive ?.content ?: "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: EncryptedPassportElement) {
|
override fun serialize(encoder: Encoder, value: EncryptedPassportElement) {
|
||||||
val json = when (value) {
|
val json = value.let {
|
||||||
is EncryptedPersonalDetails -> JsonObject(
|
encryptedElementsClassesByTypes.forEach { (key, encapsulator) ->
|
||||||
nonstrictJsonFormat.encodeToJsonElement(EncryptedPersonalDetails.serializer(), value).jsonObject + (typeField to JsonPrimitive("personal_details"))
|
val json = encapsulator.encapsulate(value) { data ->
|
||||||
)
|
nonstrictJsonFormat.encodeToJsonElement(this as KSerializer<EncryptedPassportElement>, data).jsonObject
|
||||||
is CommonPassport -> JsonObject(
|
} ?: return@forEach
|
||||||
nonstrictJsonFormat.encodeToJsonElement(CommonPassport.serializer(), value).jsonObject + (typeField to JsonPrimitive("passport"))
|
return@let JsonObject(json + (typeField to JsonPrimitive(key)))
|
||||||
)
|
}
|
||||||
is DriverLicense -> JsonObject(
|
(value as? UnknownEncryptedPassportElement) ?.rawJson ?: return
|
||||||
nonstrictJsonFormat.encodeToJsonElement(DriverLicense.serializer(), value).jsonObject + (typeField to JsonPrimitive("driver_license"))
|
|
||||||
)
|
|
||||||
is IdentityCard -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(IdentityCard.serializer(), value).jsonObject + (typeField to JsonPrimitive("identity_card"))
|
|
||||||
)
|
|
||||||
is InternalPassport -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(InternalPassport.serializer(), value).jsonObject + (typeField to JsonPrimitive("internal_passport"))
|
|
||||||
)
|
|
||||||
is EncryptedAddress -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(EncryptedAddress.serializer(), value).jsonObject + (typeField to JsonPrimitive("address"))
|
|
||||||
)
|
|
||||||
is UtilityBill -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(UtilityBill.serializer(), value).jsonObject + (typeField to JsonPrimitive("utility_bill"))
|
|
||||||
)
|
|
||||||
is BankStatement -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(BankStatement.serializer(), value).jsonObject + (typeField to JsonPrimitive("bank_statement"))
|
|
||||||
)
|
|
||||||
is RentalAgreement -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(RentalAgreement.serializer(), value).jsonObject + (typeField to JsonPrimitive("rental_agreement"))
|
|
||||||
)
|
|
||||||
is PassportRegistration -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(PassportRegistration.serializer(), value).jsonObject + (typeField to JsonPrimitive("passport_registration"))
|
|
||||||
)
|
|
||||||
is TemporaryRegistration -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(TemporaryRegistration.serializer(), value).jsonObject + (typeField to JsonPrimitive("temporary_registration"))
|
|
||||||
)
|
|
||||||
is PhoneNumber -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(PhoneNumber.serializer(), value).jsonObject + (typeField to JsonPrimitive("phone_number"))
|
|
||||||
)
|
|
||||||
is Email -> JsonObject(
|
|
||||||
nonstrictJsonFormat.encodeToJsonElement(Email.serializer(), value).jsonObject + (typeField to JsonPrimitive("email"))
|
|
||||||
)
|
|
||||||
is UnknownEncryptedPassportElement -> value.rawJson
|
|
||||||
else -> return
|
|
||||||
}
|
}
|
||||||
jsonSerializer.serialize(encoder, json)
|
jsonSerializer.serialize(encoder, json)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user