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

org.http4k.lens.delegates.kt Maven / Gradle / Ivy

There is a newer version: 5.35.2.0
Show newest version
package org.http4k.lens

import org.http4k.core.HttpMessage
import org.http4k.core.Request
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

sealed interface TypedField {

    class Required(internal val spec: BiDiLensBuilder) :
        ReadWriteProperty, TypedField {
        override fun getValue(thisRef: IN, property: KProperty<*>) = spec.required(property.name)(thisRef)

        override fun setValue(thisRef: IN, property: KProperty<*>, value: OUT) {
            spec.required(property.name)(value, thisRef)
        }
    }

    class Optional(internal val spec: BiDiLensBuilder) :
        ReadWriteProperty, TypedField {
        override fun getValue(thisRef: IN, property: KProperty<*>) = spec.optional(property.name)(thisRef)

        override fun setValue(thisRef: IN, property: KProperty<*>, value: OUT?) {
            spec.optional(property.name)(value, thisRef)
        }
    }

    class Defaulted(
        internal val spec: BiDiLensBuilder,
        private val default: (IN) -> OUT
    ) : ReadWriteProperty, TypedField {
        override fun getValue(thisRef: IN, property: KProperty<*>) = spec.defaulted(property.name, default)(thisRef)

        override fun setValue(thisRef: IN, property: KProperty<*>, value: OUT) {
            spec.optional(property.name)(value, thisRef)
        }
    }

    class Body(internal val spec: BiDiBodyLensSpec, val example: OUT?) :
        ReadWriteProperty, TypedField {
        override fun getValue(thisRef: IN, property: KProperty<*>) = spec.toLens()(thisRef)

        override fun setValue(thisRef: IN, property: KProperty<*>, value: OUT) {
            spec.toLens()(value, thisRef)
        }
    }

    class Path(internal val spec: PathLensSpec) : ReadOnlyProperty, TypedField {
        override fun getValue(thisRef: Request, property: KProperty<*>): OUT {
            return spec.of(property.name)(thisRef)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy