org.http4k.lens.HasLens.kt Maven / Gradle / Ivy
package org.http4k.lens
import org.http4k.core.with
import org.http4k.format.AutoMarshalling
import org.http4k.lens.ParamMeta.ObjectParam
import kotlin.reflect.KClass
/**
* Experimental superclass companion objects to provide a lens for a class.
*
* Access the lens using Class.lens
*/
abstract class HasLens(
private val autoMarshalling: AutoMarshalling,
private val clazz: KClass,
metadata: Map = emptyMap()
) {
val lens = BiDiBodyLens(
listOf(Meta(true, "body", ObjectParam, clazz.java.simpleName, null, metadata)),
autoMarshalling.defaultContentType,
{ autoMarshalling.asA(it.bodyString(), clazz) },
{ value, target ->
target.body(autoMarshalling.asFormatString(value))
.with(Header.CONTENT_TYPE of autoMarshalling.defaultContentType)
})
companion object {
@JvmStatic
protected inline fun kClass(): KClass = T::class
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy