entities.TypedIdBSONCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baku Show documentation
Show all versions of baku Show documentation
helps you focus your REST API back-end on the business logic
package com.github.fluidsonic.baku
import org.bson.BsonReader
import org.bson.BsonWriter
internal class TypedIdBSONCodec(
private val idFactoryProvider: EntityIdFactoryProvider
) : AbstractBSONCodec() {
override fun BsonReader.decode(context: BSONCodingContext) =
readDocument {
val factory = readString("type").let { type ->
idFactoryProvider.idFactoryForType(type) ?: throw BSONException("ID type '$type' has not been registered with Baku")
}
readName("id")
readValueOfType(factory.idClass).typed
}
override fun BsonWriter.encode(value: TypedId, context: BSONCodingContext) {
writeDocument {
write("type", string = value.untyped.factory.type)
write("id", value = value.untyped)
}
}
}