Skip to content

Commit

Permalink
Delegate OptionalSnowflake serialization to Snowflake serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
zTrap committed May 29, 2024
1 parent 442bbc1 commit c48d8c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
12 changes: 4 additions & 8 deletions common/src/commonMain/kotlin/entity/Snowflake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,10 @@ public value class Snowflake(public val value: ULong) : Comparable<Snowflake> {
}

internal object Serializer : KSerializer<Snowflake> {
override val descriptor: SerialDescriptor = ULong.serializer().descriptor

override fun deserialize(decoder: Decoder): Snowflake =
Snowflake(decoder.decodeInline(descriptor).decodeLong().toULong())

override fun serialize(encoder: Encoder, value: Snowflake) {
encoder.encodeInline(descriptor).encodeLong(value.value.toLong())
}
private val delegate = ULong.serializer()
override val descriptor: SerialDescriptor = delegate.descriptor
override fun deserialize(decoder: Decoder): Snowflake = Snowflake(delegate.deserialize(decoder))
override fun serialize(encoder: Encoder, value: Snowflake) = delegate.serialize(encoder, value.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.kord.common.entity.optional
import dev.kord.common.entity.Snowflake
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 Down Expand Up @@ -91,14 +90,15 @@ public sealed class OptionalSnowflake {
}

internal object Serializer : KSerializer<OptionalSnowflake> {
override val descriptor: SerialDescriptor = ULong.serializer().descriptor
private val delegate = Snowflake.serializer()

override fun deserialize(decoder: Decoder): OptionalSnowflake =
Value(Snowflake(decoder.decodeInline(descriptor).decodeLong().toULong()))
override val descriptor: SerialDescriptor = delegate.descriptor

override fun deserialize(decoder: Decoder): OptionalSnowflake = Value(delegate.deserialize(decoder))

override fun serialize(encoder: Encoder, value: OptionalSnowflake) = when (value) {
Missing -> Unit // ignore value
is Value -> encoder.encodeInline(descriptor).encodeLong(value.value.value.toLong())
is Value -> delegate.serialize(encoder, value.value)
}
}
}
Expand Down

0 comments on commit c48d8c8

Please sign in to comment.