org.http4k.jsonrpc.MethodBindings.kt Maven / Gradle / Ivy
package org.http4k.jsonrpc
import org.http4k.format.Json
import org.http4k.format.JsonLibAutoMarshallingJson
interface MethodBindings : Iterable> {
fun method(name: String, handler: JsonRpcHandler)
companion object {
open class Manual(private val json: Json) :
MethodBindings {
override fun iterator() = methodMappings
.map { JsonRpcMethodBinding(it.key, it.value) }.iterator()
private val methodMappings = mutableMapOf>()
override fun method(name: String, handler: JsonRpcHandler) {
methodMappings[name] = handler
}
fun handler(paramsLens: Mapping,
resultLens: Mapping,
fn: (IN) -> OUT): JsonRpcHandler =
handler(emptySet(), paramsLens, resultLens, fn)
fun handler(paramsFieldNames: Set,
paramsLens: Mapping,
resultLens: Mapping,
fn: (IN) -> OUT): JsonRpcHandler =
ParamMappingJsonRequestHandler(json, paramsFieldNames, paramsLens, fn, resultLens)
fun handler(resultLens: Mapping, block: () -> OUT): JsonRpcHandler =
NoParamsJsonRequestHandler(block, resultLens)
}
class Auto(val json: JsonLibAutoMarshallingJson) :
Manual(json) {
inline fun handler(paramsFieldNames: Set,
noinline fn: (IN) -> OUT): JsonRpcHandler =
handler(paramsFieldNames, Mapping { json.asA(it, IN::class) }, Mapping { json.asJsonObject(it) }, fn)
inline fun handler(noinline fn: (IN) -> OUT): JsonRpcHandler =
handler(IN::class.javaObjectType.declaredFields.map { it.name }.toSet(), fn)
fun handler(fn: () -> OUT): JsonRpcHandler =
handler(Mapping { json.asJsonObject(it) }, fn)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy