com.lightningkite.lightningserver.db.ModelInfoWithDefault.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
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
}