1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-05 08:09:21 +00:00

overwrite TransactionType

This commit is contained in:
2025-04-30 08:46:13 +06:00
parent e0f5e9b640
commit a408c6c7c7

View File

@@ -8,14 +8,37 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
@Serializable(with = TransactionTypeSerializer::class) @Serializable(TransactionTypeSerializer::class)
enum class TransactionType { sealed interface TransactionType {
INVOICE_PAYMENT, val name: String
PAID_MEDIA_PAYMENT,
GIFT_PURCHASE, @Serializable
PREMIUM_PURCHASE, data object InvoicePayment : TransactionType {
BUSINESS_ACCOUNT_TRANSFER, override val name = "invoice_payment"
UNKNOWN }
@Serializable
data object PaidMediaPayment : TransactionType {
override val name = "paid_media_payment"
}
@Serializable
data object GiftPurchase : TransactionType {
override val name = "gift_purchase"
}
@Serializable
data object PremiumPurchase : TransactionType {
override val name = "premium_purchase"
}
@Serializable
data object BusinessAccountTransfer : TransactionType {
override val name = "business_account_transfer"
}
@Serializable
value class Unknown(override val name: String) : TransactionType
} }
private object TransactionTypeSerializer : KSerializer<TransactionType> { private object TransactionTypeSerializer : KSerializer<TransactionType> {
@@ -25,10 +48,14 @@ private object TransactionTypeSerializer : KSerializer<TransactionType> {
) )
override fun deserialize(decoder: Decoder): TransactionType { override fun deserialize(decoder: Decoder): TransactionType {
return try { val value = decoder.decodeString()
TransactionType.valueOf(decoder.decodeString()) return when (value) {
} catch (e: IllegalArgumentException) { TransactionType.InvoicePayment.name -> TransactionType.InvoicePayment
TransactionType.UNKNOWN TransactionType.PaidMediaPayment.name -> TransactionType.PaidMediaPayment
TransactionType.GiftPurchase.name -> TransactionType.GiftPurchase
TransactionType.PremiumPurchase.name -> TransactionType.PremiumPurchase
TransactionType.BusinessAccountTransfer.name -> TransactionType.BusinessAccountTransfer
else -> TransactionType.Unknown(value)
} }
} }