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

nativeMain.kotbase.internal.fleece.FLMutableDict.kt Maven / Gradle / Ivy

There is a newer version: 3.1.3-1.1.0
Show newest version
/*
 * Copyright 2022-2023 Jeff Lockhart
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kotbase.internal.fleece

import kotbase.*
import kotbase.ext.toStringMillis
import kotbase.internal.DbContext
import kotlinx.cinterop.convert
import kotlinx.datetime.Instant
import libcblite.*

internal fun FLMutableDict.setValue(key: String, value: Any?, ctxt: DbContext?) {
    @Suppress("UNCHECKED_CAST")
    when (value) {
        is Boolean -> setBoolean(key, value)
        is ByteArray -> setBlob(key, Blob(value), ctxt)
        is Blob -> setBlob(key, value, ctxt)
        is String -> setString(key, value)
        is Instant -> setDate(key, value)
        is Number -> setNumber(key, value)
        is List<*> -> setArray(key, MutableArray(value), ctxt)
        is Array -> setArray(key, value, ctxt)
        is Map<*, *> -> setDictionary(key, MutableDictionary(value as Map), ctxt)
        is Dictionary -> setDictionary(key, value, ctxt)
        null -> FLMutableDict_SetNull(this, key.toFLString())
        else -> invalidTypeError(value)
    }
}

internal fun FLMutableDict.setString(key: String, value: String?) {
    if (value != null) {
        FLMutableDict_SetString(this, key.toFLString(), value.toFLString())
    } else {
        FLMutableDict_SetNull(this, key.toFLString())
    }
}

internal fun FLMutableDict.setNumber(key: String, value: Number?) {
    when (value) {
        is Double -> FLMutableDict_SetDouble(this, key.toFLString(), value)
        is Float -> FLMutableDict_SetFloat(this, key.toFLString(), value)
        null -> FLMutableDict_SetNull(this, key.toFLString())
        else -> FLMutableDict_SetInt(this, key.toFLString(), value.toLong().convert())
    }
}

internal fun FLMutableDict.setInt(key: String, value: Int) {
    FLMutableDict_SetInt(this, key.toFLString(), value.convert())
}

internal fun FLMutableDict.setLong(key: String, value: Long) {
    FLMutableDict_SetInt(this, key.toFLString(), value.convert())
}

internal fun FLMutableDict.setFloat(key: String, value: Float) {
    FLMutableDict_SetFloat(this, key.toFLString(), value)
}

internal fun FLMutableDict.setDouble(key: String, value: Double) {
    FLMutableDict_SetDouble(this, key.toFLString(), value)
}

internal fun FLMutableDict.setBoolean(key: String, value: Boolean) {
    FLMutableDict_SetBool(this, key.toFLString(), value)
}

internal fun FLMutableDict.setBlob(key: String, value: Blob?, ctxt: DbContext?) {
    if (value?.actual == null) {
        FLMutableDict_SetNull(this, key.toFLString())
    } else {
        FLMutableDict_SetBlob(this, key.toFLString(), value.actual)
    }
    value?.checkSetDb(ctxt)
}

internal fun FLMutableDict.setDate(key: String, value: Instant?) {
    if (value != null) {
        FLMutableDict_SetString(this, key.toFLString(), value.toStringMillis().toFLString())
    } else {
        FLMutableDict_SetNull(this, key.toFLString())
    }
}

internal fun FLMutableDict.setArray(key: String, value: Array?, ctxt: DbContext?) {
    if (value != null) {
        value.dbContext = ctxt
        FLMutableDict_SetArray(this, key.toFLString(), value.actual)
    } else {
        FLMutableDict_SetNull(this, key.toFLString())
    }
}

internal fun FLMutableDict.setDictionary(key: String, value: Dictionary?, ctxt: DbContext?) {
    if (value != null) {
        checkSelf(value.actual)
        value.dbContext = ctxt
        FLMutableDict_SetDict(this, key.toFLString(), value.actual)
    } else {
        FLMutableDict_SetNull(this, key.toFLString())
    }
}

private fun FLMutableDict.checkSelf(value: FLMutableDict) {
    if (value === this) {
        throw IllegalArgumentException("Dictionaries cannot ba added to themselves")
    }
}

internal fun FLMutableDict.remove(key: String) {
    FLMutableDict_Remove(this, key.toFLString())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy