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 Oct 24, 2023
1 parent 0005947 commit be60171
Showing 1 changed file with 5 additions and 5 deletions.
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 be60171

Please sign in to comment.