diff --git a/build.gradle b/build.gradle index d2aa7cf..b4c11c4 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ allprojects { apply plugin: "maven-publish" group = "com.gempukku.libgdx.lib" - version = '0.1.6' + version = '0.1.7' ext { appName = "Libgdx Graph" gdxVersion = '1.9.12' diff --git a/libgdx-gemp-lib/src/com/gempukku/libgdx/lib/template/JsonTemplateLoader.java b/libgdx-gemp-lib/src/com/gempukku/libgdx/lib/template/JsonTemplateLoader.java index e71082c..91d7099 100644 --- a/libgdx-gemp-lib/src/com/gempukku/libgdx/lib/template/JsonTemplateLoader.java +++ b/libgdx-gemp-lib/src/com/gempukku/libgdx/lib/template/JsonTemplateLoader.java @@ -98,9 +98,34 @@ private static void appendTemplateWithFilter(String template, JsonValue result, private static void appendField(JsonValue field, JsonValue result, ObjectSet removeFields, FileHandleResolver resolver) { String fieldName = field.name(); if (!fieldName.startsWith("tpl:") && (removeFields == null || !removeFields.contains(fieldName))) { - if (result.has(fieldName)) - result.remove(fieldName); - result.addChild(field.name(), resolveJson(field, resolver)); + if (result.has(fieldName)) { + JsonValue existingChild = result.get(fieldName); + if (existingChild.isObject() && field.isObject()) { + mergeIn(field, existingChild); + } else { + result.remove(fieldName); + result.addChild(fieldName, resolveJson(field, resolver)); + } + } else { + result.addChild(fieldName, resolveJson(field, resolver)); + } + } + } + + private static void mergeIn(JsonValue merge, JsonValue into) { + for (JsonValue fieldInObject : merge) { + String fieldName = fieldInObject.name(); + if (into.has(fieldName)) { + JsonValue existingChild = into.get(fieldName); + if (existingChild.isObject() && fieldInObject.isObject()) { + mergeIn(fieldInObject, into); + } else { + into.remove(fieldName); + into.addChild(fieldName, resolveJson(fieldInObject, null)); + } + } else { + into.addChild(fieldName, resolveJson(fieldInObject, null)); + } } } diff --git a/libgdx-gemp-lib/test-resources/template/mergeTemplates.json b/libgdx-gemp-lib/test-resources/template/mergeTemplates.json new file mode 100644 index 0000000..bd199c7 --- /dev/null +++ b/libgdx-gemp-lib/test-resources/template/mergeTemplates.json @@ -0,0 +1,7 @@ +{ + "tpl:extends": [ + "template/simple.json", + "template/simple2.json" + ], + "c": "d" +} \ No newline at end of file diff --git a/libgdx-gemp-lib/test-resources/template/overrideMerge.json b/libgdx-gemp-lib/test-resources/template/overrideMerge.json new file mode 100644 index 0000000..982e255 --- /dev/null +++ b/libgdx-gemp-lib/test-resources/template/overrideMerge.json @@ -0,0 +1,6 @@ +{ + "tpl:extends": "template/templateInside.json", + "c": { + "b": "c" + } +} \ No newline at end of file diff --git a/libgdx-gemp-lib/test-resources/template/overrideMergeOverrides.json b/libgdx-gemp-lib/test-resources/template/overrideMergeOverrides.json new file mode 100644 index 0000000..fe4ce40 --- /dev/null +++ b/libgdx-gemp-lib/test-resources/template/overrideMergeOverrides.json @@ -0,0 +1,6 @@ +{ + "tpl:extends": "template/templateInside.json", + "c": { + "a": "c" + } +} \ No newline at end of file diff --git a/libgdx-gemp-lib/test-resources/template/simple2.json b/libgdx-gemp-lib/test-resources/template/simple2.json new file mode 100644 index 0000000..a6c649f --- /dev/null +++ b/libgdx-gemp-lib/test-resources/template/simple2.json @@ -0,0 +1,3 @@ +{ + "b": "c" +} \ No newline at end of file diff --git a/libgdx-gemp-lib/test/com/gempukku/libgdx/lib/template/JsonTemplateLoaderTest.java b/libgdx-gemp-lib/test/com/gempukku/libgdx/lib/template/JsonTemplateLoaderTest.java index 1a27fba..1d073cb 100644 --- a/libgdx-gemp-lib/test/com/gempukku/libgdx/lib/template/JsonTemplateLoaderTest.java +++ b/libgdx-gemp-lib/test/com/gempukku/libgdx/lib/template/JsonTemplateLoaderTest.java @@ -2,7 +2,6 @@ import com.badlogic.gdx.assets.loaders.resolvers.ClasspathFileHandleResolver; import com.badlogic.gdx.utils.JsonValue; -import com.badlogic.gdx.utils.JsonWriter; import com.gempukku.libgdx.lib.LibGDXTest; import org.junit.Test; @@ -40,7 +39,6 @@ public void testExtends() { @Test public void testOverrideInFile() { JsonValue result = JsonTemplateLoader.loadTemplateFromFile("template/overrideInFile.json", new ClasspathFileHandleResolver()); - System.out.println(result.toJson(JsonWriter.OutputType.json)); assertTrue(result.isObject()); assertEquals(1, result.size); assertEquals("c", result.getString("a")); @@ -97,4 +95,28 @@ public void testTemplateInArray() { JsonValue a = objectInArray.get("a"); assertEquals("b", a.asString()); } + + @Test + public void testOverrideMergesObject() { + JsonValue result = JsonTemplateLoader.loadTemplateFromFile("template/overrideMerge.json", new ClasspathFileHandleResolver()); + assertTrue(result.isObject()); + assertEquals(1, result.size); + JsonValue c = result.get("c"); + assertTrue(c.isObject()); + assertEquals(2, c.size); + JsonValue a = c.get("a"); + assertEquals("b", a.asString()); + JsonValue b = c.get("b"); + assertEquals("c", b.asString()); + } + + @Test + public void testMergeTemplates() { + JsonValue result = JsonTemplateLoader.loadTemplateFromFile("template/mergeTemplates.json", new ClasspathFileHandleResolver()); + assertTrue(result.isObject()); + assertEquals(3, result.size); + assertEquals("b", result.getString("a")); + assertEquals("c", result.getString("b")); + assertEquals("d", result.getString("c")); + } }