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

commonMain.maryk.core.models.IsValueDataModel.kt Maven / Gradle / Ivy

Go to download

Maryk is a Kotlin Multiplatform library which helps you to store, query and send data in a structured way over multiple platforms. The data store stores any value with a version, so it is possible to request only the changed data or live listen for updates.

The newest version!
package maryk.core.models

import maryk.core.models.definitions.ValueDataModelDefinition
import maryk.core.models.serializers.IsValueDataModelSerializer
import maryk.core.properties.IsPropertyContext
import maryk.core.properties.definitions.IsFixedStorageBytesEncodable
import maryk.core.properties.types.ValueDataObject
import maryk.core.values.ObjectValues

/**
 * Interface for ValueDataModels.
 * Contains ValueDataModel specific Serializer and Meta definition.
 * Also contains method to create bytes from passed ObjectValues based on this ValueDataModel.
 */
interface IsValueDataModel>:
    IsTypedObjectDataModel,
    IsStorableDataModel {
    override val Serializer: IsValueDataModelSerializer
    override val Meta: ValueDataModelDefinition

    /** Creates bytes for given [values] */
    fun toBytes(values: ObjectValues): ByteArray {
        val bytes = ByteArray(this.Serializer.byteSize)
        var offset = 0

        this.forEachIndexed { index, it ->
            @Suppress("UNCHECKED_CAST")
            val def = it as IsFixedStorageBytesEncodable
            def.writeStorageBytes(values(index.toUInt() + 1u)) {
                bytes[offset++] = it
            }

            if (offset < bytes.size) {
                bytes[offset++] = 1 // separator byte
            }
        }

        return bytes
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy