org.http4k.jsonrpc.internal.kt Maven / Gradle / Ivy
package org.http4k.jsonrpc
import org.http4k.format.Json
import org.http4k.format.JsonType
internal class ParamMappingJsonRequestHandler(json: Json,
paramsFieldNames: Iterable,
paramsLens: Mapping,
function: (IN) -> OUT,
resultLens: Mapping) : JsonRpcHandler {
private val handler: (NODE) -> NODE = {
val input = when (json.typeOf(it)) {
JsonType.Array -> {
val elements = json.elements(it).toList()
paramsFieldNames.mapIndexed { index: Int, name: String ->
name to elements.getOrElse(index) { json.nullNode() }
}.takeUnless { it.isEmpty() }
?.let { json.obj(it) }
?: json.nullNode()
}
else -> it
}
paramsLens(input).let(function).let(resultLens)
}
override fun invoke(request: NODE): NODE = handler(request)
}
internal class NoParamsJsonRequestHandler(function: () -> OUT, resultLens: Mapping) :
JsonRpcHandler {
private val handler: (NODE) -> NODE = { function().let(resultLens) }
override fun invoke(request: NODE): NODE = handler(request)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy