com.lightningkite.ktordb.Modification.ext.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of db Show documentation
Show all versions of db Show documentation
An abstract tool for communicating with different types of databases.
The newest version!
package com.lightningkite.ktordb
import kotlin.reflect.KProperty1
fun Modification.forFieldOrNull(field: KProperty1): Modification? {
return when (this) {
is Modification.Chain -> modifications.mapNotNull { it.forFieldOrNull(field) }.takeUnless { it.isEmpty() }
?.let { Modification.Chain(it) }
is Modification.OnField<*, *> -> if (this.key == field) this.modification as Modification else null
else -> null
}
}
fun Modification.vet(field: KProperty1, onModification: (Modification) -> Unit) {
when (this) {
is Modification.Assign -> onModification(Modification.Assign(field.get(this.value)))
is Modification.Chain -> modifications.forEach { it.vet(field, onModification) }
is Modification.OnField<*, *> -> if (this.key == field) (this.modification as Modification).vet(onModification) else null
else -> { }
}
}
fun Modification.vet(onModification: (Modification) -> Unit) {
when (this) {
is Modification.Chain -> modifications.forEach { it.vet(onModification) }
else -> onModification(this)
}
}