From f71bed5daee4352055c80d7231affdf4c4d04bbf Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 8 May 2023 09:38:57 +0700 Subject: [PATCH] Use prefix to merge result from codec with result json --- .../dbotthepony/mc/otm/core/UnOverengineering.kt | 16 ++++++++-------- .../dbotthepony/mc/otm/data/Codec2Serializer.kt | 16 ++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt index e3130551a..354dd0ee5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/UnOverengineering.kt @@ -36,12 +36,12 @@ fun Codec.fromJsonStrict(value: JsonElement): V { return decode(JsonOps.INSTANCE, value).get().map({ left -> left.first }, { throw JsonSyntaxException("Error decoding element: ${it.message()}") }) } -fun Codec.toJson(value: V): JsonElement? { - return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { null }) +fun Codec.toJson(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement? { + return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { null }) } -fun Codec.toJsonStrict(value: V): JsonElement { - return encode(value, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) +fun Codec.toJsonStrict(value: V, prefix: JsonElement = JsonOps.INSTANCE.empty()): JsonElement { + return encode(value, JsonOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) } fun Codec.fromNbt(value: Tag): V? { @@ -52,12 +52,12 @@ fun Codec.fromNbtStrict(value: Tag): V { return decode(NbtOps.INSTANCE, value).get().map({ left -> left.first }, { throw RuntimeException("Error decoding element: ${it.message()}") }) } -fun Codec.toNbt(value: V): Tag? { - return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { null }) +fun Codec.toNbt(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag? { + return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { null }) } -fun Codec.toNbtStrict(value: V): Tag { - return encode(value, NbtOps.INSTANCE, NbtOps.INSTANCE.empty()).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) +fun Codec.toNbtStrict(value: V, prefix: Tag = NbtOps.INSTANCE.empty()): Tag { + return encode(value, NbtOps.INSTANCE, prefix).get().map({ it }, { throw RuntimeException("Error encoding element: ${it.message()}") }) } // 1.19 being 1.19 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2Serializer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2Serializer.kt index f42659e5e..6d026f219 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2Serializer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2Serializer.kt @@ -13,19 +13,15 @@ import ru.dbotthepony.mc.otm.core.toJsonStrict class Codec2Serializer(val codec: Codec, val embed: Boolean = true) : Serializer { override fun serialize(p_79325_: JsonObject, p_79326_: T, p_79327_: JsonSerializationContext) { if (embed) { - val json = codec.toJsonStrict(p_79326_) + val result = codec.toJsonStrict(p_79326_, p_79325_) - if (json !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") + if (result !is JsonObject) { + throw RuntimeException("Expected JsonObject from codec, got ${result::class.qualifiedName}") } - for ((k, v) in json.entrySet()) { - if (p_79325_.has(k)) { - throw RuntimeException("Codec returned object with '$k' as member, which is not allowed (it was written by upstream code into provided JsonObject)") - } - - p_79325_[k] = v - } + val keys = ArrayList(p_79325_.keySet()) + for (k in keys) p_79325_.remove(k) + for ((k, v) in result.entrySet()) p_79325_[k] = v } else { p_79325_["value"] = codec.toJsonStrict(p_79326_) }