
org.http4k.lens.lens.kt Maven / Gradle / Ivy
package org.http4k.lens
/**
* A Lens provides the uni-directional extraction of an entity from a target.
*/
open class Lens(
val meta: Meta,
private val lensGet: (IN) -> FINAL
) : LensExtractor, Iterable {
override fun iterator(): Iterator = listOf(meta).iterator()
override fun toString(): String = "${if (meta.required) "Required" else "Optional"} ${meta.location} '${meta.name}'"
override operator fun invoke(target: IN): FINAL = try {
lensGet(target)
} catch (e: LensFailure) {
throw e
} catch (e: Exception) {
throw LensFailure(Invalid(meta), cause = e, target = target)
}
}
/**
* A BiDiLens provides the bi-directional extraction of an entity from a target, or the insertion of an entity
* into a target.
*/
class BiDiLens(
meta: Meta,
get: (IN) -> FINAL,
private val lensSet: (FINAL, IN) -> IN
) : LensInjector, Lens(meta, get) {
@Suppress("UNCHECKED_CAST")
override operator fun invoke(value: FINAL, target: R): R = lensSet(value, target) as R
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy