From f278ef2ccae51646ba275e92df52e4c285e8b58d Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Tue, 3 May 2022 18:08:32 +0200 Subject: [PATCH] Fix asterisk handling on insert into array (#113, #121, #130, #156) --- .../java/org/metafacture/metafix/FixPath.java | 12 +++---- .../metafix/MetafixMethodTest.java | 3 -- .../metafix/MetafixRecordTest.java | 33 +++++++++++++++++++ .../todo.txt | 1 - .../todo.txt | 2 +- .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - .../todo.txt | 1 - 9 files changed, 38 insertions(+), 17 deletions(-) delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAsteriskIntoArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfStringsIntoArrayOfObjectsWithAsteriskWildcard/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAsteriskIntoArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAsteriskIntoArrayOfObjects/todo.txt delete mode 100644 metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAsterisk/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 2dad76f5..5dee05f5 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -250,14 +250,10 @@ private void removeNestedFrom(final Value value) { // basic idea: reuse findIn logic here? setIn(findIn(array), newValue) final String field = path[0]; if (field.equals(ASTERISK)) { - if (path.length == 1) { - for (int i = 0; i < array.size(); ++i) { - mode.apply(array, String.valueOf(i + 1), newValue); - } - } - else { - array.add(Value.newHash(h -> new FixPath(tail(path)).insertInto(h, mode, newValue))); - } + array.forEach(value -> value.matchType() + .ifArray(a -> new FixPath(tail(path)).insertInto(a, mode, newValue)) + .ifHash(h -> new FixPath(tail(path)).insertInto(h, mode, newValue)) + .orElseThrow()); } else { if (path.length == 1) { diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java index eb781aca..01404d0c 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java @@ -2369,7 +2369,6 @@ public void shouldSortFieldAndRemoveDuplicates() { } @Test - @MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/121") public void shouldSortArrayFieldWithAsterisk() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "sort_field('OTHERS[].*.dnimals[]')" @@ -2558,7 +2557,6 @@ public void shouldSplitHashField() { } @Test - @MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/100 and https://github.com/metafacture/metafacture-fix/issues/121") public void shouldSplitNestedField() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "split_field('others[].*.tools', '--')" @@ -2607,7 +2605,6 @@ public void shouldSumNumbers() { } @Test - @MetafixToDo("java.lang.IllegalStateException: Expected String, got Array; see https://github.com/metafacture/metafacture-fix/issues/121") public void shouldSumArrayFieldWithAsterisk() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "sum('OTHERS[].*.dumbers[]')" diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 6f783b4d..643859a9 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -1685,6 +1685,39 @@ public void setArrayReplaceExisting() { }); } + @Test + // See https://github.com/metafacture/metafacture-fix/issues/130 + public void setArrayInArrayWithAsterisk() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_array('foo[].*.test[]', 'test')"), + i -> { + i.startRecord("1"); + i.startEntity("foo[]"); + i.startEntity("1"); + i.literal("id", "A"); + i.endEntity(); + i.startEntity("2"); + i.literal("id", "B"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("foo[]"); + o.get().startEntity("1"); + o.get().literal("id", "A"); + o.get().startEntity("test[]"); + o.get().literal("1", "test"); + f.apply(2).endEntity(); + o.get().startEntity("2"); + o.get().literal("id", "B"); + o.get().startEntity("test[]"); + o.get().literal("1", "test"); + f.apply(3).endEntity(); + o.get().endRecord(); + }); + } + @Test public void setHash() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAsteriskIntoArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAsteriskIntoArrayOfObjects/todo.txt deleted file mode 100644 index 26adbe67..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_fieldAsteriskIntoArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #156 and #121 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfObjectsAsRepeatedObjectAndThenArrayOfStringsIntoTheArrayOfObjectsWithLastWildcard/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfObjectsAsRepeatedObjectAndThenArrayOfStringsIntoTheArrayOfObjectsWithLastWildcard/todo.txt index b19226bb..9b87121b 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfObjectsAsRepeatedObjectAndThenArrayOfStringsIntoTheArrayOfObjectsWithLastWildcard/todo.txt +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfObjectsAsRepeatedObjectAndThenArrayOfStringsIntoTheArrayOfObjectsWithLastWildcard/todo.txt @@ -1 +1 @@ -See issue #132 may also be related to #113 +See issue #132 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfStringsIntoArrayOfObjectsWithAsteriskWildcard/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfStringsIntoArrayOfObjectsWithAsteriskWildcard/todo.txt deleted file mode 100644 index 0b9661d2..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldArrayOfStringsIntoArrayOfObjectsWithAsteriskWildcard/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #121 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAsteriskIntoArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAsteriskIntoArrayOfObjects/todo.txt deleted file mode 100644 index 5d692bde..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/copy_fieldAsteriskIntoArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #156 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAsteriskIntoArrayOfObjects/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAsteriskIntoArrayOfObjects/todo.txt deleted file mode 100644 index 5d692bde..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/move_fieldAsteriskIntoArrayOfObjects/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #156 diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAsterisk/todo.txt b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAsterisk/todo.txt deleted file mode 100644 index c8097c36..00000000 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAsterisk/todo.txt +++ /dev/null @@ -1 +0,0 @@ -See issue #113, #130 and #156