Skip to content

Commit

Permalink
Added support for merging changes into templates
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin.sciesinski committed Jan 11, 2021
1 parent 2b804ce commit b920d15
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,34 @@ private static void appendTemplateWithFilter(String template, JsonValue result,
private static void appendField(JsonValue field, JsonValue result, ObjectSet<String> 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));
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions libgdx-gemp-lib/test-resources/template/mergeTemplates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tpl:extends": [
"template/simple.json",
"template/simple2.json"
],
"c": "d"
}
6 changes: 6 additions & 0 deletions libgdx-gemp-lib/test-resources/template/overrideMerge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tpl:extends": "template/templateInside.json",
"c": {
"b": "c"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tpl:extends": "template/templateInside.json",
"c": {
"a": "c"
}
}
3 changes: 3 additions & 0 deletions libgdx-gemp-lib/test-resources/template/simple2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"b": "c"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit b920d15

Please sign in to comment.