Skip to content

Commit

Permalink
Update serializer for OpCode
Browse files Browse the repository at this point in the history
The companion object is no longer the serializer.
  • Loading branch information
lukellmann committed Sep 16, 2023
1 parent fe5ee2b commit a84c9a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions gateway/api/gateway.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
39 changes: 25 additions & 14 deletions gateway/src/commonMain/kotlin/OpCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -68,19 +68,30 @@ public enum class OpCode(public val code: Int) {
*/
HeartbeatACK(11);

public companion object OpCodeSerializer : KSerializer<OpCode> {
override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor("op", PrimitiveKind.INT)

internal object Serializer : KSerializer<OpCode> {
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<OpCode>'.",
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<OpCode>'.",
ReplaceWith("OpCode.serializer()", imports = ["dev.kord.gateway.OpCode"]),
DeprecationLevel.WARNING,
)
public class OpCodeSerializer internal constructor() : KSerializer<OpCode> by Serializer {
public fun serializer(): KSerializer<OpCode> = this
}
}

0 comments on commit a84c9a5

Please sign in to comment.