All Downloads are FREE. Search and download functionalities are using the official Maven repository.

entities.TypedIdBSONCodec.kt Maven / Gradle / Ivy

There is a newer version: 0.9.27
Show newest version
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)
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy