Skip to content

Commit

Permalink
upfix of chatmember statuses serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Sep 29, 2023
1 parent 38c9732 commit 284fe58
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
Expand All @@ -15,15 +16,35 @@ import kotlinx.serialization.json.jsonPrimitive

@Serializable(ChatMemberSerializer::class)
sealed interface ChatMember : WithUser {
@Serializable
enum class Status(val status: String, val deserializationStrategy: DeserializationStrategy<ChatMember>) {
@Serializable(StatusSerializer::class)
enum class Status(
val status: String,
val deserializationStrategy: DeserializationStrategy<ChatMember>
) {
Creator("creator", OwnerChatMember.serializer()),
Administrator("administrator", AdministratorChatMemberImpl.serializer()),
Member("member", MemberChatMemberImpl.serializer()),
Restricted("restricted", RestrictedChatMember.serializer()),
Left("left", LeftChatMemberImpl.serializer()),
Kicked("kicked", KickedChatMember.serializer())
}

object StatusSerializer : KSerializer<Status> {
override val descriptor: SerialDescriptor
get() = String.serializer().descriptor

override fun deserialize(decoder: Decoder): Status {
val status = decoder.decodeString()
return Status.values().first {
it.status == status
}
}

override fun serialize(encoder: Encoder, value: Status) {
encoder.encodeString(value.status)
}
}

val status: Status
}

Expand Down

0 comments on commit 284fe58

Please sign in to comment.