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

commonMain.org.kodein.db.impl.model.ModelKeyMakerModule.kt Maven / Gradle / Ivy

The newest version!
package org.kodein.db.impl.model

import org.kodein.db.*
import org.kodein.db.data.DataKeyMaker
import org.kodein.db.impl.data.getDocumentKeyType
import org.kodein.memory.io.Memory
import org.kodein.memory.io.wrap
import org.kodein.memory.text.readString
import kotlin.reflect.KClass

internal interface ModelKeyMakerModule : KeyMaker, ModelValueMakerModule {

    val data: DataKeyMaker

    override fun  keyById(type: KClass, vararg id: Any) = Key(data.newKey(mdb.getTypeId(mdb.typeTable.getTypeName(mdb.typeTable.getRootOf(type) ?: type)), valueOf(id)))

    override fun  keyFrom(model: M, vararg options: Options.Puts) = Key(data.newKey(mdb.getTypeId(mdb.typeTable.getTypeName(mdb.typeTable.getRootOf(model::class) ?: model::class)), valueOf(mdb.getMetadata(model, options).id)))

    override fun  keyFromB64(type: KClass, b64: String): Key {
        val key = Key(Memory.wrap(Key.b64Decoder.decode(b64)))
        val keyTypeId = getDocumentKeyType(key.bytes)
        val typeName = mdb.typeTable.getTypeName(mdb.typeTable.getRootOf(type) ?: type)
        val typeId = mdb.getTypeId(typeName, false)
        check(typeId == keyTypeId) {
            val keyTypeName = mdb.getTypeName(keyTypeId)
            if (keyTypeName != null) "Key is of type ${keyTypeName.readString()} but was expected to be of type ${typeName.readString()}. Are you sure this key was created with this Kodein-DB?"
            else "Key is of unknown type, but was expected to be of type ${typeName.readString()}. Are you sure this key was created with this Kodein-DB?"
        }
        return key
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy