com.lightningkite.lightningserver.settings.Pluggable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
package com.lightningkite.lightningserver.settings
abstract class Pluggable {
private val handlers = HashMap T>()
val options: Set get() = handlers.keys
fun register(key: String, handler: (S) -> T) {
handlers[key] = handler
}
fun parse(key: String, setting: S): T {
val h = handlers[key]
?: throw IllegalArgumentException("No handler $key for ${this::class.qualifiedName} - available handlers are ${options.joinToString()}")
return h(setting)
}
fun parseParameterString(params: String): Map> = params
.takeIf { it.isNotBlank() }
?.split("&")
?.filter { it.isNotBlank() }
?.map {
it.substringBefore('=') to it.substringAfter('=', "")
}
?.groupBy { it.first }
?.mapValues { it.value.map { it.second } }
?: emptyMap()
}