io.wavebeans.execution.Call.kt Maven / Gradle / Ivy
package io.wavebeans.execution
import kotlinx.serialization.Serializable
import kotlinx.serialization.protobuf.ProtoNumber
import java.util.concurrent.TimeUnit
import kotlin.reflect.KType
import kotlin.reflect.typeOf
@Serializable
data class Call(
@ProtoNumber(1)
val method: String,
@ProtoNumber(2)
val params: Map = emptyMap()
) {
companion object {
val empty = Call("", emptyMap())
fun parseRequest(request: String): Call {
val split = request.split("?", limit = 2)
val method = split[0]
val q = split.elementAtOrNull(1)
val params = q?.split("&")
?.map { it.split("=", limit = 2) }
?.map { it[0] to it[1] }
?.toMap()
?: emptyMap()
return Call(method, params)
}
}
fun param(key: String, type: KType): Any? {
return when (type) {
typeOf() -> params[key]?.toInt()
typeOf() -> params[key]?.toFloat()
typeOf() -> params[key]?.toLong()
typeOf() -> params[key]?.toBoolean()
typeOf() -> params[key]?.let { TimeUnit.valueOf(it) }
else -> throw UnsupportedOperationException("$type is unsupported during call to `$method`")
}
}
}