com.lightningkite.lightningserver.jsonschema.Util.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.jsonschema
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
private fun MutableMap.remapMerge(
key: K,
value: V,
remap: (V, V) -> V?
): V? {
val oldValue: V? = this[key]
val newValue: V? = if (oldValue == null) value else remap(oldValue, value)
if (newValue == null) {
remove(key)
} else {
put(key, newValue)
}
return newValue
}
fun MutableMap.merge(key: String, element: JsonElement): JsonElement? {
return remapMerge(key, element, ::merge)
}
fun merge(previous: JsonElement, value: JsonElement): JsonElement? {
return when {
previous == value -> value
previous is JsonObject && value is JsonObject -> merge(previous, value)
previous is JsonArray && value is JsonArray -> JsonArray(previous + value)
value is JsonNull -> previous
else -> value
}
}
fun merge(previous: JsonObject, value: JsonObject): JsonObject? {
val elements = mutableMapOf()
val values = (previous.keys + value.keys).toSet().associateWith {
(previous[it] ?: JsonNull) to (value[it] ?: JsonNull)
}
values.forEach { (name, oldNewPair) ->
val (old, new) = oldNewPair
val merged = merge(old, new) ?: return null
elements[name] = merged
}
return JsonObject(elements)
}