Use prefix to merge result from codec with result json

This commit is contained in:
DBotThePony 2023-05-08 09:38:57 +07:00
parent d5bbd717b6
commit f71bed5dae
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 14 additions and 18 deletions

View File

@ -36,12 +36,12 @@ fun <V : Any> Codec<V>.fromJsonStrict(value: JsonElement): V {
return decode(JsonOps.INSTANCE, value).get().map({ left -> left.first }, { throw JsonSyntaxException("Error decoding element: ${it.message()}") }) return decode(JsonOps.INSTANCE, value).get().map({ left -> left.first }, { throw JsonSyntaxException("Error decoding element: ${it.message()}") })
} }
fun <V : Any> Codec<V>.toJson(value: V): JsonElement? { fun <V : Any> Codec<V>.toJson(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement? {
return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { null }) return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { null })
} }
fun <V : Any> Codec<V>.toJsonStrict(value: V): JsonElement { fun <V : Any> Codec<V>.toJsonStrict(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement {
return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
} }
fun <V : Any> Codec<V>.fromNbt(value: Tag): V? { fun <V : Any> Codec<V>.fromNbt(value: Tag): V? {
@ -52,12 +52,12 @@ fun <V : Any> Codec<V>.fromNbtStrict(value: Tag): V {
return decode(NbtOps.INSTANCE, value).get().map({ left -> left.first }, { throw RuntimeException("Error decoding element: ${it.message()}") }) return decode(NbtOps.INSTANCE, value).get().map({ left -> left.first }, { throw RuntimeException("Error decoding element: ${it.message()}") })
} }
fun <V : Any> Codec<V>.toNbt(value: V): Tag? { fun <V : Any> Codec<V>.toNbt(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag? {
return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { null }) return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { null })
} }
fun <V : Any> Codec<V>.toNbtStrict(value: V): Tag { fun <V : Any> Codec<V>.toNbtStrict(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag {
return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") })
} }
// 1.19 being 1.19 // 1.19 being 1.19

View File

@ -13,19 +13,15 @@ import ru.dbotthepony.mc.otm.core.toJsonStrict
class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = true) : Serializer<T> { class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = true) : Serializer<T> {
override fun serialize(p_79325_: JsonObject, p_79326_: T, p_79327_: JsonSerializationContext) { override fun serialize(p_79325_: JsonObject, p_79326_: T, p_79327_: JsonSerializationContext) {
if (embed) { if (embed) {
val json = codec.toJsonStrict(p_79326_) val result = codec.toJsonStrict(p_79326_, p_79325_)
if (json !is JsonObject) { if (result !is JsonObject) {
throw RuntimeException("It was specified to embed result from codec into target json, but codec returned ${json::class.qualifiedName}, while was expecting JsonObject") throw RuntimeException("Expected JsonObject from codec, got ${result::class.qualifiedName}")
} }
for ((k, v) in json.entrySet()) { val keys = ArrayList(p_79325_.keySet())
if (p_79325_.has(k)) { for (k in keys) p_79325_.remove(k)
throw RuntimeException("Codec returned object with '$k' as member, which is not allowed (it was written by upstream code into provided JsonObject)") for ((k, v) in result.entrySet()) p_79325_[k] = v
}
p_79325_[k] = v
}
} else { } else {
p_79325_["value"] = codec.toJsonStrict(p_79326_) p_79325_["value"] = codec.toJsonStrict(p_79326_)
} }