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

com.lightningkite.lightningserver.db.ModelInfoWithDefault.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.lightningserver.db

import com.lightningkite.lightningdb.FieldCollection
import com.lightningkite.lightningdb.HasId
import kotlinx.serialization.serializer

inline fun , reified ID : Comparable> ModelInfoWithDefault(
    crossinline getCollection: () -> FieldCollection,
    crossinline forUser: suspend FieldCollection.(user: USER) -> FieldCollection,
    crossinline defaultItem: suspend (user: USER) -> T,
    crossinline exampleItem: ()->T? = { null },
    modelName: String = serializer().descriptor.serialName.substringBefore('<').substringAfterLast('.')
) = object : ModelInfoWithDefault {
    override val collectionName: String = modelName
    override suspend fun defaultItem(user: USER): T = defaultItem(user)
    override fun collection(): FieldCollection = getCollection()
    override suspend fun collection(user: USER): FieldCollection =
        this.collection().forUser(user)

    override val serialization: ModelSerializationInfo = ModelSerializationInfo()
    override fun exampleItem(): T? = exampleItem()
}

fun , ID : Comparable> ModelInfoWithDefault(
    serialization: ModelSerializationInfo,
    getCollection: () -> FieldCollection,
    forUser: suspend FieldCollection.(user: USER) -> FieldCollection,
    defaultItem: suspend (user: USER) -> T,
    exampleItem: ()->T? = { null },
    modelName: String = serialization.serializer.descriptor.serialName.substringBefore('<').substringAfterLast('.')
) = object : ModelInfoWithDefault {
    override val collectionName: String = modelName
    override suspend fun defaultItem(user: USER): T = defaultItem(user)
    override fun collection(): FieldCollection = getCollection()
    override suspend fun collection(user: USER): FieldCollection =
        this.collection().forUser(user)

    override val serialization: ModelSerializationInfo = serialization
    override fun exampleItem(): T? = exampleItem()
}

interface ModelInfoWithDefault, ID : Comparable> : ModelInfo {
    suspend fun defaultItem(user: USER): T
    fun exampleItem(): T? = null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy