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

com.lightningkite.ktordb.Modification.ext.kt Maven / Gradle / Ivy

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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy