From a84c9a547dc4ab143121b9566da895932e0d0b2d Mon Sep 17 00:00:00 2001 From: lukellmann Date: Sat, 16 Sep 2023 16:22:45 +0200 Subject: [PATCH] Update serializer for OpCode The companion object is no longer the serializer. --- gateway/api/gateway.api | 5 ++++ gateway/src/commonMain/kotlin/OpCode.kt | 39 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/gateway/api/gateway.api b/gateway/api/gateway.api index 58ff1fb5e4c..2b132782e92 100644 --- a/gateway/api/gateway.api +++ b/gateway/api/gateway.api @@ -1540,6 +1540,7 @@ public final class dev/kord/gateway/MessageUpdate : dev/kord/gateway/DispatchEve } public final class dev/kord/gateway/OpCode : java/lang/Enum { + public static final field Companion Ldev/kord/gateway/OpCode$Companion; public static final field Dispatch Ldev/kord/gateway/OpCode; public static final field Heartbeat Ldev/kord/gateway/OpCode; public static final field HeartbeatACK Ldev/kord/gateway/OpCode; @@ -1559,6 +1560,10 @@ public final class dev/kord/gateway/OpCode : java/lang/Enum { public static fun values ()[Ldev/kord/gateway/OpCode; } +public final class dev/kord/gateway/OpCode$Companion { + public final fun serializer ()Lkotlinx/serialization/KSerializer; +} + public final class dev/kord/gateway/OpCode$OpCodeSerializer : kotlinx/serialization/KSerializer { public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ldev/kord/gateway/OpCode; public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object; diff --git a/gateway/src/commonMain/kotlin/OpCode.kt b/gateway/src/commonMain/kotlin/OpCode.kt index bf063fe862d..232b830e8e7 100644 --- a/gateway/src/commonMain/kotlin/OpCode.kt +++ b/gateway/src/commonMain/kotlin/OpCode.kt @@ -4,11 +4,11 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.descriptors.PrimitiveKind import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor -import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import kotlin.jvm.JvmField -@Serializable(with = OpCode.OpCodeSerializer::class) +@Serializable(with = OpCode.Serializer::class) public enum class OpCode(public val code: Int) { /** The default code for unknown values. */ Unknown(Int.MIN_VALUE), @@ -68,19 +68,30 @@ public enum class OpCode(public val code: Int) { */ HeartbeatACK(11); - public companion object OpCodeSerializer : KSerializer { - override val descriptor: SerialDescriptor - get() = PrimitiveSerialDescriptor("op", PrimitiveKind.INT) - + internal object Serializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("dev.kord.gateway.OpCode", PrimitiveKind.INT) + override fun serialize(encoder: Encoder, value: OpCode) = encoder.encodeInt(value.code) private val entriesByCode = entries.associateBy { it.code } - override fun deserialize(decoder: Decoder): OpCode { - val code = decoder.decodeInt() - return entriesByCode[code] ?: Unknown - } - - override fun serialize(encoder: Encoder, value: OpCode) { - encoder.encodeInt(value.code) - } + override fun deserialize(decoder: Decoder) = entriesByCode[decoder.decodeInt()] ?: Unknown + } + + public companion object { + @Suppress("DEPRECATION") + @Deprecated( + "Renamed to 'Companion', which no longer implements 'KSerializer'.", + ReplaceWith("OpCode.serializer()", imports = ["dev.kord.gateway.OpCode"]), + DeprecationLevel.WARNING, + ) + @JvmField + public val OpCodeSerializer: OpCodeSerializer = OpCodeSerializer() } + @Deprecated( + "Renamed to 'Companion', which no longer implements 'KSerializer'.", + ReplaceWith("OpCode.serializer()", imports = ["dev.kord.gateway.OpCode"]), + DeprecationLevel.WARNING, + ) + public class OpCodeSerializer internal constructor() : KSerializer by Serializer { + public fun serializer(): KSerializer = this + } }