Skip to content

Commit

Permalink
mongo adapter: Allow reading of old INT64 Snowflake from databases (#238
Browse files Browse the repository at this point in the history
)

* mongo adapter: Allow reading of old INT64 Snowflake from databases

* Update SnowflakeCodec.kt
  • Loading branch information
NoComment1105 authored Sep 16, 2023
1 parent 5c370d7 commit 1b01954
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
package com.kotlindiscord.kord.extensions.adapters.mongodb.codecs

import dev.kord.common.entity.Snowflake
import org.bson.BsonInvalidOperationException
import org.bson.BsonReader
import org.bson.BsonWriter
import org.bson.codecs.Codec
import org.bson.codecs.DecoderContext
import org.bson.codecs.EncoderContext

public object SnowflakeCodec : Codec<Snowflake> {
override fun decode(reader: BsonReader, decoderContext: DecoderContext): Snowflake =
Snowflake(reader.readString())
override fun decode(reader: BsonReader, decoderContext: DecoderContext): Snowflake = try {
Snowflake(reader.readString())
} catch (e: BsonInvalidOperationException) {
Snowflake(reader.readInt64())
}

override fun encode(writer: BsonWriter, value: Snowflake, encoderContext: EncoderContext) {
writer.writeString(value.toString())
Expand Down

0 comments on commit 1b01954

Please sign in to comment.