diff --git a/README.md b/README.md index 1cc5ebbab..7444c830d 100644 --- a/README.md +++ b/README.md @@ -642,21 +642,41 @@ retain(""[, ...]) ##### `set_array` -_Currently alias for [`add_array`](#add_array)._ +Creates a new array (with optional values), provided that the intermediate structures (i.e. parent fields) exist. To create any missing intermediate structures, use [`add_array`](#add_array) instead. -We advise you to use [`add_array`](#add_array) instead of `set_array` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309) +```perl +set_array("") +set_array("", ""[, ...]) +``` + +[Example in Playground](https://metafacture.org/playground/?example=set_array) + +[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_array+{") ##### `set_field` -_Currently alias for [`add_field`](#add_field)._ +Creates a field with a defined value, provided that the intermediate structures (i.e. parent fields) exist. To create any missing intermediate structures, use [`add_field`](#add_field) instead. + +```perl +set_field("", "") +``` + +[Example in Playground](https://metafacture.org/playground/?example=set_field) -We advise you to use [`add_field`](#add_field) instead of `set_field` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309) +[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_field+{") ##### `set_hash` -_Currently alias for [`add_hash`](#add_hash)._ +Creates a new hash (with optional values), provided that the intermediate structures (i.e. parent fields) exist. To create any missing intermediate structures, use [`add_hash`](#add_hash) instead. + +```perl +set_hash("") +set_hash("", "subfieldName": ""[, ...]) +``` + +[Example in Playground](https://metafacture.org/playground/?example=set_hash) -We advise you to use [`add_hash`](#add_hash) instead of `set_hash` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309) +[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixMethod.java+"+set_hash+{") ##### `timestamp` diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index b06352338..c7132e37d 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -170,7 +170,7 @@ public void apply(final Metafix metafix, final Record record, final List final String field = params.get(0); final Value newValue = newArray(params.subList(1, params.size()).stream().map(Value::new)); record.set(field, newValue); - newValue.asArray().forEach(value -> value.withPathSet(newValue.getPath() + "." + value.getPath())); + newValue.asArray().forEach(value -> value.withPathSet(newValue.getPath() + Value.FIELD_PATH_SEPARATOR + value.getPath())); } }, add_field { @@ -408,19 +408,25 @@ public void apply(final Metafix metafix, final Record record, final List set_array { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - add_array.apply(metafix, record, params, options); + if (parentFieldExists(record, params.get(0))) { + add_array.apply(metafix, record, params, options); + } } }, set_field { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - add_field.apply(metafix, record, params, options); + if (parentFieldExists(record, params.get(0))) { + add_field.apply(metafix, record, params, options); + } } }, set_hash { @Override public void apply(final Metafix metafix, final Record record, final List params, final Map options) { - add_hash.apply(metafix, record, params, options); + if (parentFieldExists(record, params.get(0))) { + add_hash.apply(metafix, record, params, options); + } } }, timestamp { diff --git a/metafix/src/main/java/org/metafacture/metafix/FixPath.java b/metafix/src/main/java/org/metafacture/metafix/FixPath.java index 0b96b8410..497706d1c 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixPath.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixPath.java @@ -29,14 +29,22 @@ * With all get/set/update/create/delete logic collected here. * * @author Fabian Steeg (fsteeg) - * */ -/*package-private*/ class FixPath { +public class FixPath { private static final String ASTERISK = "*"; + + private static final String INDEX_SUBPATH_PATTERN = Value.FIELD_PATH_SEPARATOR_PATTERN + "\\d" + Value.FIELD_PATH_SEPARATOR_PATTERN; + private static final String ASTERISK_SUBPATH = Value.FIELD_PATH_SEPARATOR + ASTERISK + Value.FIELD_PATH_SEPARATOR; + private String[] path; - /*package-private*/ FixPath(final String path) { + /** + * Creates an instance of {@link FixPath} based on the given path string. + * + * @param path the path string + */ + public FixPath(final String path) { this(Value.split(path)); } @@ -111,7 +119,7 @@ private Value findInValue(final Value value, final String[] p) { @Override public String toString() { - return String.join(".", path); + return String.join(Value.FIELD_PATH_SEPARATOR, path); } /*package-private*/ int size() { @@ -141,7 +149,9 @@ else if (value.getPath() != null && hasWildcard()) { } private boolean matches(final String thatPath) { - return thatPath != null && thatPath.replaceAll("\\.\\d+\\.", ".*.").equals(String.join(".", this.path)); + return thatPath != null && thatPath + .replaceAll(INDEX_SUBPATH_PATTERN, ASTERISK_SUBPATH) + .equals(String.join(Value.FIELD_PATH_SEPARATOR, this.path)); } private String[] replaceInPath(final String find, final int i) { @@ -299,9 +309,29 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n } } + /** + * Checks whether the given field is adding to an array. + * + * @param field the field + * + * @return true if the given field is adding to an array + */ + public static boolean isAddingToArray(final String field) { + return field.equals(ReservedField.$prepend.name()) || field.equals(ReservedField.$append.name()); + } + + /** + * Checks whether any of the path segments are adding to an array. + * + * @return true if any of the path segments are adding to an array + */ + public boolean isAddingToArray() { + return Arrays.stream(path).anyMatch(FixPath::isAddingToArray); + } + 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()); + final boolean isAddingToArray = isAddingToArray(nextField); if (result == null) { result = (isAddingToArray ? Value.newArray() : Value.newHash()).withPathSet(newPath); hash.put(field, result); @@ -320,6 +350,15 @@ private String[] tail(final String[] fields) { return Arrays.copyOfRange(fields, 1, fields.length); } + /** + * Creates an instance of {@link FixPath} based on the parent path. + * + * @return an instance of {@link FixPath}, or {@code null} if already at the top level + */ + public FixPath getParentPath() { + return path.length > 1 ? new FixPath(Arrays.copyOfRange(path, 0, path.length - 1)) : null; + } + private enum ReservedField { $prepend, $append, $first, $last; diff --git a/metafix/src/main/java/org/metafacture/metafix/Value.java b/metafix/src/main/java/org/metafacture/metafix/Value.java index 5f97c4ac7..5cca2c488 100644 --- a/metafix/src/main/java/org/metafacture/metafix/Value.java +++ b/metafix/src/main/java/org/metafacture/metafix/Value.java @@ -52,7 +52,8 @@ */ public class Value implements JsonValue { // checkstyle-disable-line ClassDataAbstractionCoupling - private static final String FIELD_PATH_SEPARATOR = "\\."; + public static final String FIELD_PATH_SEPARATOR = "."; + /*package-private*/ static final String FIELD_PATH_SEPARATOR_PATTERN = Pattern.quote(FIELD_PATH_SEPARATOR); private final Array array; private final Hash hash; @@ -409,7 +410,7 @@ public void toJson(final JsonGenerator jsonGenerator) { } /*package-private*/ static String[] split(final String fieldPath) { - return fieldPath.split(FIELD_PATH_SEPARATOR); + return fieldPath.split(FIELD_PATH_SEPARATOR_PATTERN); } /** @@ -431,7 +432,7 @@ private Value withPathAppend(final int i) { } private Value withPathAppend(final String field) { - return withPathSet(path == null || path.isEmpty() ? field : path + "." + field); + return withPathSet(path == null || path.isEmpty() ? field : path + FIELD_PATH_SEPARATOR + field); } /*package-private*/ Value copy() { @@ -574,7 +575,7 @@ protected Map> retainFields(final Collection f final Map> retainFields = new HashMap<>(); fields.forEach(p -> { - final String[] parts = p.split(FIELD_PATH_SEPARATOR, 2); + final String[] parts = p.split(FIELD_PATH_SEPARATOR_PATTERN, 2); function.apply(parts[0]).forEach(f -> { final Collection retainNested = retainFields.computeIfAbsent(f, k -> new HashSet<>()); diff --git a/metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java b/metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java index 4fb7891d6..f3033513f 100644 --- a/metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java +++ b/metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java @@ -18,6 +18,7 @@ import org.metafacture.framework.StandardEventNames; import org.metafacture.io.ObjectWriter; +import org.metafacture.metafix.FixPath; import org.metafacture.metafix.Metafix; import org.metafacture.metafix.Record; import org.metafacture.metafix.Value; @@ -194,4 +195,17 @@ default Stream flatten(final Stream stream) { )); } + /** + * Checks whether the given field's parent field exists in the record. + * + * @param record the record + * @param field the field + * + * @return true if the given field's parent field exists in the record + */ + default boolean parentFieldExists(final Record record, final String field) { + final FixPath parentPath = new FixPath(field).getParentPath(); + return parentPath == null || !parentPath.isAddingToArray() && record.containsPath(parentPath.toString()); + } + } diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java index 3527a54f3..6af9477a4 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java @@ -810,7 +810,7 @@ public void shouldDoListAsWithMultipleListsOfDifferentSizes() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "set_array('sourceOrga[]')", "do list_as(orgId: 'ccm:university[]', orgName: 'ccm:university_DISPLAYNAME[]', orgLoc: 'ccm:university_LOCATION[]')", - " set_hash('sourceOrga[].$append')", + " add_hash('sourceOrga[].$append')", " copy_field(orgId, 'sourceOrga[].$last.id')", " copy_field(orgName, 'sourceOrga[].$last.name')", " copy_field(orgLoc, 'sourceOrga[].$last.location')", diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java index 24c26f501..53298e0e5 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java @@ -994,9 +994,9 @@ public void shouldLookupInCopiedNestedArraysCreatedWithPrepend() { private void shouldLookupInCopiedNestedArraysCreatedWith(final String reservedField) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "put_map('rswk-indicator', s: 'SubjectHeading')", - "set_array('subject[]')", - "set_array('subject[]." + reservedField + ".componentList[]')", - "set_array('subject[].$last.componentList[]." + reservedField + ".type[]')", + "add_array('subject[]')", + "add_array('subject[]." + reservedField + ".componentList[]')", + "add_array('subject[].$last.componentList[]." + reservedField + ".type[]')", "do list(path: 'D', 'var': '$i')", " copy_field('$i', 'subject[].$last.componentList[].$last.type[]." + reservedField + "')", "end", diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java index 7f0b94744..46ea82aca 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java @@ -2818,11 +2818,9 @@ public void addFieldWithReplaceAllArray() { private void addFieldWithReplaceAllArray(final boolean replaceAll) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "do list(path: 'contribution[]', 'var': '$i')", - " set_array('$i.agent.altLabel[]')", " add_field('$i.agent.altLabel[].$append', 'contribution')", "end", "do list(path: 'subject[]', 'var': '$i')", - " set_array('$i.altLabel[]')", " add_field('$i.altLabel[].$append', 'subject')", "end", replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "", @@ -2875,10 +2873,10 @@ public void setArrayWithReplaceAll() { private void setArrayWithReplaceAll(final boolean replaceAll) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "do list(path: 'contribution[]', 'var': '$i')", - " set_array('$i.agent.altLabel[]', 'contribution')", + " add_array('$i.agent.altLabel[]', 'contribution')", "end", "do list(path: 'subject[]', 'var': '$i')", - " set_array('$i.altLabel[]', 'subject')", + " add_array('$i.altLabel[]', 'subject')", "end", replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "", replaceAll ? "replace_all('subject[].*.altLabel[].*', 't', '')" : "" @@ -2930,11 +2928,9 @@ public void copyFieldWithReplaceAllArray() { private void copyFieldWithReplaceAllArray(final boolean replaceAll) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "do list(path: 'contribution[]', 'var': '$i')", - " set_array('$i.agent.altLabel[]')", " copy_field('$i.label', '$i.agent.altLabel[].$append')", "end", "do list(path: 'subject[]', 'var': '$i')", - " set_array('$i.altLabel[]')", " copy_field('$i.label', '$i.altLabel[].$append')", "end", replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', 't', '')" : "", @@ -2987,11 +2983,9 @@ public void pasteWithReplaceAll() { private void pasteWithReplaceAll(final boolean replaceAll) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "do list(path: 'contribution[]', 'var': '$i')", - " set_array('$i.agent.altLabel[]')", " paste('$i.agent.altLabel[].$append', '$i.label', '~!')", "end", "do list(path: 'subject[]', 'var': '$i')", - " set_array('$i.altLabel[]')", " paste('$i.altLabel[].$append', '$i.label', '~!')", "end", replaceAll ? "replace_all('contribution[].*.agent.altLabel[].*', ' !', '')" : "", diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java index 31bfab6f3..e0642c4f4 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java @@ -164,10 +164,10 @@ public void entitiesPassThroughRepeatNestedEntity() { } @Test - public void setEmpty() { + public void addEmpty() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( - "set_field('my.nested.name','patrick')", - "set_field('your.nested.name','nicolas')"), + "add_field('my.nested.name','patrick')", + "add_field('your.nested.name','nicolas')"), i -> { i.startRecord("1"); i.endRecord(); @@ -185,6 +185,20 @@ public void setEmpty() { }); } + @Test + public void setEmpty() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_field('my.nested.name','patrick')", + "set_field('your.nested.name','nicolas')"), + i -> { + i.startRecord("1"); + i.endRecord(); + }, (o, f) -> { + o.get().startRecord("1"); + o.get().endRecord(); + }); + } + @Test public void setExisting() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( @@ -1965,6 +1979,424 @@ public void setHashReplaceExisting() { }); } + @Test + public void addArrayAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_array('array[].$append')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2[]"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void addArrayAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_array('array[].$append.test[]')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2"); + o.get().startEntity("test[]"); + f.apply(3).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setArrayAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_array('array[].$append')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2[]"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setArrayAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_array('array[].$append.test[]')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setArrayLast() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_array('array[].$last')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().startEntity("1[]"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setArrayLastNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_array('array[].$last.test[]')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.startEntity("1"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().startEntity("1"); + o.get().startEntity("test[]"); + f.apply(3).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void addFieldAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('array[].$append', 'test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().literal("2", "test"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void addFieldAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_field('array[].$append.test', 'bar')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2"); + o.get().literal("test", "bar"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setFieldAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_field('array[].$append', 'test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().literal("2", "test"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setFieldAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_field('array[].$append.test', 'bar')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setFieldLast() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_field('array[].$last', 'test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "test"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setFieldLastNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_field('array[].$last.test', 'baz')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.startEntity("1"); + i.literal("foo", "bar"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().startEntity("1"); + o.get().literal("foo", "bar"); + o.get().literal("test", "baz"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void addHashAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_hash('array[].$append')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void addHashAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "add_hash('array[].$append.test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2"); + o.get().startEntity("test"); + f.apply(3).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setHashAppend() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_hash('array[].$append')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().startEntity("2"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setHashAppendNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_hash('array[].$append.test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().literal("1", "foo"); + o.get().endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setHashLast() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_hash('array[].$last')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.literal("1", "foo"); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().startEntity("1"); + f.apply(2).endEntity(); + o.get().endRecord(); + } + ); + } + + @Test + public void setHashLastNested() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "set_hash('array[].$last.test')" + ), + i -> { + i.startRecord("1"); + i.startEntity("array[]"); + i.startEntity("1"); + i.literal("foo", "bar"); + i.endEntity(); + i.endEntity(); + i.endRecord(); + }, + (o, f) -> { + o.get().startRecord("1"); + o.get().startEntity("array[]"); + o.get().startEntity("1"); + o.get().literal("foo", "bar"); + o.get().startEntity("test"); + f.apply(3).endEntity(); + o.get().endRecord(); + } + ); + } + @Test public void paste() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( @@ -2435,7 +2867,7 @@ public void appendArray() { public void mixedArray() { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "set_array('@context[]', 'https://w3id.org/kim/lrmi-profile/draft/context.jsonld')", - "set_hash('@context[].$append', '@language': 'de')"), + "add_hash('@context[].$append', '@language': 'de')"), i -> { i.startRecord("1"); i.endRecord(); diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromXml/toJson/lookupInDeeplyNestedArrayOfObjects/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromXml/toJson/lookupInDeeplyNestedArrayOfObjects/test.fix index 6a9c42c5a..5775c303d 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromXml/toJson/lookupInDeeplyNestedArrayOfObjects/test.fix +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/lookup/fromXml/toJson/lookupInDeeplyNestedArrayOfObjects/test.fix @@ -1,11 +1,11 @@ put_map("rswk-indicator", s: "SubjectHeading") if exists("6890?") - set_array("subject[].$append.type[]", "ComplexSubject") + add_array("subject[].$append.type[]", "ComplexSubject") set_array("subject[].$last.label") set_array("subject[].$last.componentList[]") do list(path: "6890?", "var": "$i") - set_array("subject[].$last.componentList[].$append.type[]") + add_array("subject[].$last.componentList[].$append.type[]") do list(path: "$i.D", "var": "$k") copy_field("$k", "subject[].$last.componentList[].$last.type[].$append") end diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpper/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpper/test.fix index 6bc9cbcdc..9b6c18bbb 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpper/test.fix +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpper/test.fix @@ -1,5 +1,5 @@ do list(path: "700[01] ", "var": "$i") - set_hash("contribution[].$append.agent") + add_hash("contribution[].$append.agent") copy_field("$i.a", "contribution[].$last.agent.label") end diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpperStrictnessHandlesProcessExceptions/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpperStrictnessHandlesProcessExceptions/test.fix index 6bc9cbcdc..9b6c18bbb 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpperStrictnessHandlesProcessExceptions/test.fix +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/method/fromXml/toJson/replace_toUpperStrictnessHandlesProcessExceptions/test.fix @@ -1,5 +1,5 @@ do list(path: "700[01] ", "var": "$i") - set_hash("contribution[].$append.agent") + add_hash("contribution[].$append.agent") copy_field("$i.a", "contribution[].$last.agent.label") end diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/expected.json new file mode 100644 index 000000000..1a843afd0 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/expected.json @@ -0,0 +1,18 @@ +{ + "animals" : [ { + "name" : "Jake", + "type" : "dog" + }, { + "name" : "Blacky", + "type" : "bird" + } ], + "animals_2" : [ { + "name" : "Jake", + "type" : "dog" + }, { + "name" : "Blacky", + "type" : "bird" + }, { + "test" : [ "test", "test", "test" ] + } ] +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/input.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/input.json new file mode 100644 index 000000000..f4640bcdb --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/input.json @@ -0,0 +1,16 @@ +{ + "animals" : [ { + "name" : "Jake", + "type" : "dog" + }, { + "name" : "Blacky", + "type" : "bird" + } ], + "animals_2" : [ { + "name" : "Jake", + "type" : "dog" + }, { + "name" : "Blacky", + "type" : "bird" + } ] +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.fix new file mode 100644 index 000000000..4d1b7dd6d --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.fix @@ -0,0 +1 @@ +add_array("animals_2[].$append.test[]", "test", "test", "test") diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.flux b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.flux new file mode 100644 index 000000000..7c3575fac --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayIntoArrayOfObjectsWithAppend/test.flux @@ -0,0 +1,8 @@ +FLUX_DIR + "input.json" +|open-file +|as-records +|decode-json +|fix(FLUX_DIR + "test.fix") +|encode-json(prettyPrinting="true") +|write(FLUX_DIR + "output-metafix.json") +; diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/expected.json new file mode 100644 index 000000000..b2777dedc --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/expected.json @@ -0,0 +1,6 @@ +{ + "key" : "value", + "object" : { + "test" : [ "dog", "cat", "bird" ] + } +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/input.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/input.json new file mode 100644 index 000000000..2bb482200 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/input.json @@ -0,0 +1,3 @@ +{ + "key":"value" +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.fix new file mode 100644 index 000000000..f89d1fa5c --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.fix @@ -0,0 +1 @@ +add_array("object.test[]", "dog", "cat", "bird") diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.flux b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.flux new file mode 100644 index 000000000..7c3575fac --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithMultipleValues/test.flux @@ -0,0 +1,8 @@ +FLUX_DIR + "input.json" +|open-file +|as-records +|decode-json +|fix(FLUX_DIR + "test.fix") +|encode-json(prettyPrinting="true") +|write(FLUX_DIR + "output-metafix.json") +; diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/expected.json new file mode 100644 index 000000000..84542e0ef --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/expected.json @@ -0,0 +1,6 @@ +{ + "key" : "value", + "object" : { + "test" : [ "dog" ] + } +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/input.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/input.json new file mode 100644 index 000000000..2bb482200 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/input.json @@ -0,0 +1,3 @@ +{ + "key":"value" +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.fix new file mode 100644 index 000000000..7e3807e3c --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.fix @@ -0,0 +1 @@ +add_array("object.test[]", "dog") diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.flux b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.flux new file mode 100644 index 000000000..7c3575fac --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithSingleValue/test.flux @@ -0,0 +1,8 @@ +FLUX_DIR + "input.json" +|open-file +|as-records +|decode-json +|fix(FLUX_DIR + "test.fix") +|encode-json(prettyPrinting="true") +|write(FLUX_DIR + "output-metafix.json") +; diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/expected.json new file mode 100644 index 000000000..9c6e32f3f --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/expected.json @@ -0,0 +1,6 @@ +{ + "key" : "value", + "object" : { + "test" : [ ] + } +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/input.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/input.json new file mode 100644 index 000000000..2bb482200 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/input.json @@ -0,0 +1,3 @@ +{ + "key":"value" +} diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.fix b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.fix new file mode 100644 index 000000000..d1c646335 --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.fix @@ -0,0 +1 @@ +add_array("object.test[]") diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.flux b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.flux new file mode 100644 index 000000000..7c3575fac --- /dev/null +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/add_arrayNestedSimpleWithoutValues/test.flux @@ -0,0 +1,8 @@ +FLUX_DIR + "input.json" +|open-file +|as-records +|decode-json +|fix(FLUX_DIR + "test.fix") +|encode-json(prettyPrinting="true") +|write(FLUX_DIR + "output-metafix.json") +; diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAppend/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAppend/expected.json index 1a843afd0..f4640bcdb 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAppend/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayIntoArrayOfObjectsWithAppend/expected.json @@ -12,7 +12,5 @@ }, { "name" : "Blacky", "type" : "bird" - }, { - "test" : [ "test", "test", "test" ] } ] } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithMultipleValues/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithMultipleValues/expected.json index b2777dedc..d7a7e3b1f 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithMultipleValues/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithMultipleValues/expected.json @@ -1,6 +1,3 @@ { - "key" : "value", - "object" : { - "test" : [ "dog", "cat", "bird" ] - } + "key" : "value" } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithSingleValue/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithSingleValue/expected.json index 84542e0ef..d7a7e3b1f 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithSingleValue/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithSingleValue/expected.json @@ -1,6 +1,3 @@ { - "key" : "value", - "object" : { - "test" : [ "dog" ] - } + "key" : "value" } diff --git a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithoutValues/expected.json b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithoutValues/expected.json index 9c6e32f3f..d7a7e3b1f 100644 --- a/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithoutValues/expected.json +++ b/metafix/src/test/resources/org/metafacture/metafix/integration/record/fromJson/toJson/set_arrayNestedSimpleWithoutValues/expected.json @@ -1,6 +1,3 @@ { - "key" : "value", - "object" : { - "test" : [ ] - } + "key" : "value" }