From fffb6d96207e3d176b130b7c1886820a24904974 Mon Sep 17 00:00:00 2001 From: Fabian Steeg Date: Wed, 25 Sep 2024 14:04:55 +0200 Subject: [PATCH] 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