From b09ca4f7157f475a5ef7d51fc6598874c87f6ebc Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Tue, 24 Sep 2024 09:01:47 +0200 Subject: [PATCH 01/10] Support implicit array creation on `$append` (#127) --- .../java/org/metafacture/metafix/FixPath.java | 11 ++++- .../metafix/MetafixRecordTest.java | 45 +++++++++++++++++++ .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - 12 files changed, 55 insertions(+), 11 deletions(-) delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStrings/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfStrings/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStrings/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStrings/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfStrings/todo.txt diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index ec35670c..b5845d7e 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -276,7 +276,16 @@ else if (isReference(field)) { } else { if (!hash.containsField(field)) { - hash.put(field, Value.newHash().withPathSet(newValue.getPath())); + if (ReservedField.$append.name().equals(tail(path)[0])) { + hash.put(field, Value.newArray().withPathSet(newValue.getPath())); + } + else { + hash.put(field, Value.newHash().withPathSet(newValue.getPath())); + } + } + final Value value = hash.get(field); + if (value.isString()) { + hash.put(field, Value.newArray(a -> a.add(value))); } insertInto(hash.get(field), mode, newValue, field, tail(path)); } diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 67e6ea77..80119922 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -303,6 +303,51 @@ public void addWithAppendInNewArray() { ); } + @Test + public void addWithAppendInImplicitArray() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('my.name.$append','patrick')", + "add_field('my.name.$append','nicolas')" + ), + i -> { + i.startRecord("1"); + i.endRecord(); + + i.startRecord("2"); + i.startEntity("my"); + i.literal("name", "max"); + i.endEntity(); + i.endRecord(); + + i.startRecord("3"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("my"); + o.get().literal("name", "patrick"); + o.get().literal("name", "nicolas"); + o.get().endEntity(); + o.get().endRecord(); + + o.get().startRecord("2"); + o.get().startEntity("my"); + o.get().literal("name", "max"); + o.get().literal("name", "patrick"); + o.get().literal("name", "nicolas"); + o.get().endEntity(); + o.get().endRecord(); + + o.get().startRecord("3"); + o.get().startEntity("my"); + o.get().literal("name", "patrick"); + o.get().literal("name", "nicolas"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + @Test public void addWithAppendInArray() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfStringsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 From d0cc26ebf25762a24de35f8b7ed0e507eb2c3dd3 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 25 Sep 2024 10:57:09 +0200 Subject: [PATCH 02/10] Fix issues in tests for implicit array creation on `$append` (#127) --- .../metafacture/metafix/MetafixRecordTest.java | 4 +--- .../expected.json | 2 +- .../todo.txt | 1 - .../expected.json | 8 ++++---- .../todo.txt | 1 - .../expected.json | 16 ++++++++-------- .../todo.txt | 1 - .../expected.json | 14 ++++++++------ .../todo.txt | 1 - .../expected.json | 6 +++--- .../todo.txt | 1 - .../expected.json | 2 +- .../todo.txt | 1 - .../expected.json | 6 +++--- .../todo.txt | 1 - .../expected.json | 12 +++++++----- .../todo.txt | 1 - 17 files changed, 36 insertions(+), 42 deletions(-) delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/todo.txt diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 80119922..a3e4156f 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -1462,11 +1462,9 @@ public void renameArrayOfHashes() { } @Test - @MetafixToDo("Do we actually need/want implicit $append? WDCD?") public void copyIntoArrayOfHashesImplicitAppend() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( - "set_array('author[]')", - "copy_field('your.name','author[].name')", + "copy_field('your.name','author[].$append.name')", "remove_field('your')"), i -> { i.startRecord("1"); diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/expected.json index 8e7ba3f2..42599003 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/expected.json @@ -1,7 +1,7 @@ { "test" : "test", "object" : { - "arrayOfObjects" : [ { + "arrayOfObject" : [ { "key" : "value" }, { "key" : "more_value" diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAppendAndCreateNestedArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/expected.json index d8894a0c..370535e9 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/expected.json @@ -1,9 +1,9 @@ { - "arrayOfObjects" : [ { + "key" : "value", + "key_2" : "more_value", + "arrayOfObject" : [ { "key" : "value" }, { "key" : "more_value" - } ], - "key" : "value", - "key_2" : "more_value" + } ] } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json index 632a2674..6bc5863e 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json @@ -1,19 +1,19 @@ { - "arrayOfObjects_1" : [ { - "arrayOfObjects_2" : [ { + "key" : "value", + "key_2" : "more_value", + "key_3" : "even_more_value", + "key_4" : "so_much_value", + "arrayOfObject_1" : [ { + "arrayOfObject_2" : [ { "key" : "value" }, { "key" : "more_value" } ] }, { - "arrayOfObjects_3" : [ { + "arrayOfObject_3" : [ { "key" : "even_more_value" }, { "key" : "so_much_value" } ] - } ], - "key" : "value", - "key_2" : "more_value", - "key_3" : "even_more_value", - "key_4" : "so_much_value" + } ] } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/expected.json index d8894a0c..b8423f75 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/expected.json @@ -1,9 +1,11 @@ { - "arrayOfObjects" : [ { - "key" : "value" - }, { - "key" : "more_value" - } ], "key" : "value", - "key_2" : "more_value" + "key_2" : "more_value", + "object" : { + "arrayOfObject" : [ { + "key" : "value" + }, { + "key" : "more_value" + } ] + } } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/expected.json index 34d83a66..00d40868 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/expected.json @@ -1,7 +1,7 @@ { + "key" : "value", + "key_2" : "more_value", "object" : { "test" : [ "value", "more_value" ] - }, - "key" : "value", - "key_2" : "more_value" + } } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAppendAndCreateNestedArrayOfStrings/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/expected.json index 50fe5a3a..8e77e7a0 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/expected.json @@ -1,5 +1,5 @@ { - "arrayOfObjects" : [ { + "arrayOfObject" : [ { "key" : "value" }, { "key" : "more_value" diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json index c13b46bb..b01b2ae3 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/expected.json @@ -1,12 +1,12 @@ { - "arrayOfObjects_1" : [ { - "arrayOfObjects_2" : [ { + "arrayOfObject_1" : [ { + "arrayOfObject_2" : [ { "key" : "value" }, { "key" : "more_value" } ] }, { - "arrayOfObjects_3" : [ { + "arrayOfObject_3" : [ { "key" : "even_more_value" }, { "key" : "so_much_value" diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateArrayOfObjectsInArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/expected.json index 50fe5a3a..cf44f006 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/expected.json @@ -1,7 +1,9 @@ { - "arrayOfObjects" : [ { - "key" : "value" - }, { - "key" : "more_value" - } ] + "object" : { + "arrayOfObject" : [ { + "key" : "value" + }, { + "key" : "more_value" + } ] + } } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAppendAndCreateNestedArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 From fffb6d96207e3d176b130b7c1886820a24904974 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 25 Sep 2024 14:04:55 +0200 Subject: [PATCH 03/10] Support `$prepend` for inserting & array creation (#92, #93, #127) --- .../java/org/metafacture/metafix/FixPath.java | 11 ++- .../java/org/metafacture/metafix/Value.java | 6 ++ .../metafix/MetafixRecordTest.java | 72 +++++++++++++++++++ .../add_fieldPrependAndCreateArray/todo.txt | 1 - .../copy_fieldPrependAndCreateArray/todo.txt | 1 - .../todo.txt | 1 - .../move_fieldPrependAndCreateArray/todo.txt | 1 - .../test.fix | 2 +- .../todo.txt | 1 - 9 files changed, 88 insertions(+), 8 deletions(-) delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldPrependAndCreateArray/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependAndCreateArray/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependObjectToArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependAndCreateArray/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/todo.txt diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index b5845d7e..c2f24153 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -170,6 +170,9 @@ void apply(final Array array, final String field, final Value value) { final ReservedField reservedField = ReservedField.fromString(field); if (reservedField != null) { switch (reservedField) { + case $prepend: + array.add(0, value); + break; case $append: array.add(value); break; @@ -276,7 +279,7 @@ else if (isReference(field)) { } else { if (!hash.containsField(field)) { - if (ReservedField.$append.name().equals(tail(path)[0])) { + if (ReservedField.$prepend.name().equals(tail(path)[0]) || ReservedField.$append.name().equals(tail(path)[0])) { hash.put(field, Value.newArray().withPathSet(newValue.getPath())); } else { @@ -312,7 +315,7 @@ private String[] tail(final String[] fields) { } private enum ReservedField { - $append, $first, $last; + $prepend, $append, $first, $last; private static final Map STRING_TO_ENUM = new HashMap<>(); static { @@ -352,6 +355,10 @@ private Value getReferencedValue(final Array array, final String field, final St referencedValue = Value.newHash().withPathSet(p); // TODO: append non-hash? array.add(referencedValue); break; + case $prepend: + referencedValue = Value.newHash().withPathSet(p); + array.add(0, referencedValue); + break; default: break; } diff --git a/metafix/src/main/java/org/metafacture/metafix/Value.java b/metafix/src/main/java/org/metafacture/metafix/Value.java index 8aeae637..c1fbe988 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Value.java +++ b/metafix/src/main/java/org/metafacture/metafix/Value.java @@ -438,6 +438,12 @@ public void add(final Value value) { } } + /* package-private */ void add(final int index, final Value value) { + if (!isNull(value)) { + list.add(index, value); + } + } + public boolean isEmpty() { return list.isEmpty(); } diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index a3e4156f..8a3624e1 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -348,6 +348,51 @@ public void addWithAppendInImplicitArray() { ); } + @Test + public void addWithPrependInImplicitArray() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('my.name.$prepend','patrick')", + "add_field('my.name.$prepend','nicolas')" + ), + i -> { + i.startRecord("1"); + i.endRecord(); + + i.startRecord("2"); + i.startEntity("my"); + i.literal("name", "max"); + i.endEntity(); + i.endRecord(); + + i.startRecord("3"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("my"); + o.get().literal("name", "nicolas"); + o.get().literal("name", "patrick"); + o.get().endEntity(); + o.get().endRecord(); + + o.get().startRecord("2"); + o.get().startEntity("my"); + o.get().literal("name", "nicolas"); + o.get().literal("name", "patrick"); + o.get().literal("name", "max"); + o.get().endEntity(); + o.get().endRecord(); + + o.get().startRecord("3"); + o.get().startEntity("my"); + o.get().literal("name", "nicolas"); + o.get().literal("name", "patrick"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + @Test public void addWithAppendInArray() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( @@ -1488,6 +1533,33 @@ public void copyIntoArrayOfHashesImplicitAppend() { }); } + @Test + public void copyIntoArrayOfHashesImplicitPrepend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "copy_field('your.name','author[].$prepend.name')", + "remove_field('your')"), + i -> { + i.startRecord("1"); + i.startEntity("your"); + i.literal("name", "max"); + i.endEntity(); + i.startEntity("your"); + i.literal("name", "mo"); + i.endEntity(); + i.endRecord(); + }, (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("author[]"); + o.get().startEntity("1"); + o.get().literal("name", "mo"); + o.get().endEntity(); + o.get().startEntity("2"); + o.get().literal("name", "max"); + f.apply(2).endEntity(); + o.get().endRecord(); + }); + } + @Test public void copyIntoArrayOfHashesExplicitAppend() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldPrependAndCreateArray/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldPrependAndCreateArray/todo.txt deleted file mode 100644 index 636eae9f..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldPrependAndCreateArray/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependAndCreateArray/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependAndCreateArray/todo.txt deleted file mode 100644 index 7c5b8c32..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependAndCreateArray/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 and #93 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependObjectToArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependObjectToArrayOfObjects/todo.txt deleted file mode 100644 index 2f4b8670..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldPrependObjectToArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #92 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependAndCreateArray/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependAndCreateArray/todo.txt deleted file mode 100644 index 7c5b8c32..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependAndCreateArray/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #127 and #93 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/test.fix index cf2c5df0..241bfc75 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/test.fix +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/test.fix @@ -1 +1 @@ -copy_field("test_2", "test[].$prepend") +move_field("test_2", "test[].$prepend") diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/todo.txt deleted file mode 100644 index 2f4b8670..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldPrependObjectToArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #92 From 629a9d69035c92e3ebc5aea6a49811e1279316c6 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 25 Sep 2024 14:14:15 +0200 Subject: [PATCH 04/10] Avoid redundancy: call `tail(path)` once, add `else` block (#127) --- .../java/org/metafacture/metafix/FixPath.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index c2f24153..0fe8357f 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -279,16 +279,15 @@ else if (isReference(field)) { } else { if (!hash.containsField(field)) { - if (ReservedField.$prepend.name().equals(tail(path)[0]) || ReservedField.$append.name().equals(tail(path)[0])) { - hash.put(field, Value.newArray().withPathSet(newValue.getPath())); - } - else { - hash.put(field, Value.newHash().withPathSet(newValue.getPath())); - } + final Value value = Arrays.asList(ReservedField.$prepend.name(), ReservedField.$append.name()) + .contains(tail(path)[0]) ? Value.newArray() : Value.newHash(); + hash.put(field, value.withPathSet(newValue.getPath())); } - final Value value = hash.get(field); - if (value.isString()) { - hash.put(field, Value.newArray(a -> a.add(value))); + else { + final Value value = hash.get(field); + if (value.isString()) { + hash.put(field, Value.newArray(a -> a.add(value))); + } } insertInto(hash.get(field), mode, newValue, field, tail(path)); } From 0cedb17f01c15ee3c281a146a5dd91a55c028823 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 25 Sep 2024 16:25:44 +0200 Subject: [PATCH 05/10] Improve naming of tests: implicit arrays vs. implicit append (#127) --- .../java/org/metafacture/metafix/MetafixRecordTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 8a3624e1..c6f6baa5 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -1507,7 +1507,7 @@ public void renameArrayOfHashes() { } @Test - public void copyIntoArrayOfHashesImplicitAppend() { + public void copyIntoImplicitArrayAppend() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "copy_field('your.name','author[].$append.name')", "remove_field('your')"), @@ -1534,7 +1534,7 @@ public void copyIntoArrayOfHashesImplicitAppend() { } @Test - public void copyIntoArrayOfHashesImplicitPrepend() { + public void copyIntoImplicitArrayPrepend() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "copy_field('your.name','author[].$prepend.name')", "remove_field('your')"), @@ -1561,7 +1561,7 @@ public void copyIntoArrayOfHashesImplicitPrepend() { } @Test - public void copyIntoArrayOfHashesExplicitAppend() { + public void copyIntoExplicitArrayAppend() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "set_array('author[]')", "copy_field('your.name','author[].$append.name')", From 49cfaca00882970c34e04fb2624f59171d6408f8 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Thu, 26 Sep 2024 11:47:23 +0200 Subject: [PATCH 06/10] Avoid collection and additional calls to `tail()` & `get()` (#127) --- .../java/org/metafacture/metafix/FixPath.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index 0fe8357f..f493daca 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -278,18 +278,8 @@ else if (isReference(field)) { mode.apply(hash, field, newValue); } else { - if (!hash.containsField(field)) { - final Value value = Arrays.asList(ReservedField.$prepend.name(), ReservedField.$append.name()) - .contains(tail(path)[0]) ? Value.newArray() : Value.newHash(); - hash.put(field, value.withPathSet(newValue.getPath())); - } - else { - final Value value = hash.get(field); - if (value.isString()) { - hash.put(field, Value.newArray(a -> a.add(value))); - } - } - insertInto(hash.get(field), mode, newValue, field, tail(path)); + final String[] tail = tail(path); + insertInto(getContainerValue(hash, field, newValue.getPath(), tail[0]), mode, newValue, field, tail); } return new Value(hash); @@ -309,6 +299,23 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n } } + private Value getContainerValue(final Hash hash, final String field, final String newPath, final String nextField) { + Value result = hash.get(field); + if (result == null) { + result = (nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()) ? + Value.newArray() : Value.newHash()).withPathSet(newPath); + hash.put(field, result); + } + else { + if (result.isString()) { + final Value value = result; + result = Value.newArray(a -> a.add(value)); + hash.put(field, result); + } + } + return result; + } + private String[] tail(final String[] fields) { return Arrays.copyOfRange(fields, 1, fields.length); } From d20530ecbd7e6b3f09c78f3de8259afbde0b09a6 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Thu, 26 Sep 2024 12:06:23 +0200 Subject: [PATCH 07/10] Add MetafixToDo test for adding with $last in implicit array (#369) --- .../metafix/MetafixRecordTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index c6f6baa5..8c3360a8 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -393,6 +393,42 @@ public void addWithPrependInImplicitArray() { ); } + @Test + @MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/369") + public void addWithLastInImplicitArray() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('my.name.$last','patrick')", + "add_field('my.name.$last','nicolas')" + ), + i -> { + i.startRecord("1"); + i.endRecord(); + + i.startRecord("2"); + i.startEntity("my"); + i.literal("name", "max"); + i.endEntity(); + i.endRecord(); + + i.startRecord("3"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().endRecord(); + + o.get().startRecord("2"); + o.get().startEntity("my"); + o.get().literal("name", "nicolas"); + o.get().endEntity(); + o.get().endRecord(); + + o.get().startRecord("3"); + o.get().endRecord(); + } + ); + } + @Test public void addWithAppendInArray() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( From d00e59a173ba38c8614f65ca4b707523c73b7eae Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Thu, 26 Sep 2024 17:39:57 +0200 Subject: [PATCH 08/10] Update paths of moved values when inserting with `$prepend` (#127) --- .../java/org/metafacture/metafix/Value.java | 14 +++++++++++++- .../metafacture/metafix/MetafixLookupTest.java | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/Value.java b/metafix/src/main/java/org/metafacture/metafix/Value.java index c1fbe988..1280e049 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Value.java +++ b/metafix/src/main/java/org/metafacture/metafix/Value.java @@ -439,8 +439,20 @@ public void add(final Value value) { } /* package-private */ void add(final int index, final Value value) { + add(index, value, true); + } + + /* package-private */ void add(final int index, final Value value, final boolean appendToPath) { if (!isNull(value)) { - list.add(index, value); + list.add(index, appendToPath ? value.withPathAppend(index + 1) : value); + updateIndexesInPathsAfter(index); + } + } + + private void updateIndexesInPathsAfter(final int start) { + for (int index = start + 1; index < list.size(); index = index + 1) { + final Value value = list.get(index); + value.withPathSet(value.getPath().replaceAll("\\d+$", String.valueOf(index + 1))); } } diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java index f67a1b22..e2de4a02 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java @@ -928,14 +928,23 @@ public void shouldLookupInNestedArrays() { } @Test - public void shouldLookupInCopiedNestedArrays() { + public void shouldLookupInCopiedNestedArraysCreatedWithAppend() { + shouldLookupInCopiedNestedArraysCreatedWith("$append"); + } + + @Test + public void shouldLookupInCopiedNestedArraysCreatedWithPrepend() { + shouldLookupInCopiedNestedArraysCreatedWith("$prepend"); + } + + private void shouldLookupInCopiedNestedArraysCreatedWith(final String reservedField) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "put_map('rswk-indicator', s: 'SubjectHeading')", "set_array('subject[]')", - "set_array('subject[].$append.componentList[]')", - "set_array('subject[].$last.componentList[].$append.type[]')", + "set_array('subject[]." + reservedField + ".componentList[]')", + "set_array('subject[].$last.componentList[]." + reservedField + ".type[]')", "do list(path: 'D', 'var': '$i')", - " copy_field('$i', 'subject[].$last.componentList[].$last.type[].$append')", + " copy_field('$i', 'subject[].$last.componentList[].$last.type[]." + reservedField + "')", "end", "lookup('subject[].*.componentList[].*.type[].*', 'rswk-indicator')", "retain('subject[]')" From c5aa947a99a8e27a3a6b5c7ac3b75a1fc42d001e Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Fri, 27 Sep 2024 14:16:03 +0200 Subject: [PATCH 09/10] Wrap string value into array only on `$prepend` / `$append` (#127) --- .../java/org/metafacture/metafix/FixPath.java | 6 +-- .../metafix/MetafixRecordTest.java | 47 ++++++------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index f493daca..7b5846ee 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -301,13 +301,13 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n private Value getContainerValue(final Hash hash, final String field, final String newPath, final String nextField) { Value result = hash.get(field); + final boolean isAddingToArray = nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()); if (result == null) { - result = (nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()) ? - Value.newArray() : Value.newHash()).withPathSet(newPath); + result = (isAddingToArray ? Value.newArray() : Value.newHash()).withPathSet(newPath); hash.put(field, result); } else { - if (result.isString()) { + if (isAddingToArray && result.isString()) { final Value value = result; result = Value.newArray(a -> a.add(value)); hash.put(field, result); diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 8c3360a8..b8c13b87 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -394,38 +394,21 @@ public void addWithPrependInImplicitArray() { } @Test - @MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/369") - public void addWithLastInImplicitArray() { - MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( - "add_field('my.name.$last','patrick')", - "add_field('my.name.$last','nicolas')" - ), - i -> { - i.startRecord("1"); - i.endRecord(); - - i.startRecord("2"); - i.startEntity("my"); - i.literal("name", "max"); - i.endEntity(); - i.endRecord(); - - i.startRecord("3"); - i.endRecord(); - }, - o -> { - o.get().startRecord("1"); - o.get().endRecord(); - - o.get().startRecord("2"); - o.get().startEntity("my"); - o.get().literal("name", "nicolas"); - o.get().endEntity(); - o.get().endRecord(); - - o.get().startRecord("3"); - o.get().endRecord(); - } + public void addWithLastInNonArray() { + MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () -> + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('my.name.$last','patrick')" + ), + i -> { + i.startRecord("1"); + i.startEntity("my"); + i.literal("name", "max"); + i.endEntity(); + i.endRecord(); + }, + o -> { + } + ) ); } From fe84442c0f37983996ef02c31b660bc5235dce12 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Fri, 27 Sep 2024 14:22:07 +0200 Subject: [PATCH 10/10] Implement add(Value, boolean) using add(int, Value, boolean) (#127) --- metafix/src/main/java/org/metafacture/metafix/Value.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/metafix/src/main/java/org/metafacture/metafix/Value.java b/metafix/src/main/java/org/metafacture/metafix/Value.java index 1280e049..25d54496 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Value.java +++ b/metafix/src/main/java/org/metafacture/metafix/Value.java @@ -433,9 +433,7 @@ public void add(final Value value) { } /* package-private */ void add(final Value value, final boolean appendToPath) { - if (!isNull(value)) { - list.add(appendToPath ? value.withPathAppend(list.size() + 1) : value); - } + add(list.size(), value, appendToPath); } /* package-private */ void add(final int index, final Value value) {