Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions add_array and add_hash #374 #379

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ Options:

#### Record-level functions

##### `add_array`

Creates a new array (with optional values).

```perl
add_array("<targetFieldName>")
add_array("<targetFieldName>", "<value_1>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_array)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_array+{")

##### `add_field`

Creates a field with a defined value.
Expand All @@ -315,6 +328,19 @@ add_field("<targetFieldName>", "<fieldValue>")

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_field+{")

##### `add_hash`

Creates a new hash (with optional values).

```perl
add_hash("<targetFieldName>")
add_hash("<targetFieldName>", "subfieldName": "<subfieldValue>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_hash)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_hash+{")

##### `array`

Converts a hash/object into an array.
Expand Down Expand Up @@ -1027,7 +1053,7 @@ E.g.:
```perl
# "ccm:university":["https://ror.org/0304hq317"]
# "ccm:university_DISPLAYNAME":["Gottfried Wilhelm Leibniz Universität Hannover"]
set_array("sourceOrga[]")
add_array("sourceOrga[]")
do list_as(orgId: "ccm:university[]", orgName: "ccm:university_DISPLAYNAME[]")
copy_field(orgId, "sourceOrga[].$append.id")
copy_field(orgName, "sourceOrga[].$last.name")
Expand Down
17 changes: 17 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,29 @@ public void apply(final Metafix metafix, final Record record, final List<String>

// RECORD-LEVEL METHODS:

add_array {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
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()));
}
},
add_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.set(params.get(0), new Value(params.get(1)));
}
},
add_hash {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String field = params.get(0);
final Value newValue = Value.newHash(h -> options.forEach((f, v) -> h.put(f, new Value(v))));
record.set(field, newValue);
}
},
array { // array-from-hash
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
Expand Down
32 changes: 16 additions & 16 deletions metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void doList() {
@Test
public void doListExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'name', 'var': 'n')",
" upcase('n')",
" trim('n')",
Expand Down Expand Up @@ -212,7 +212,7 @@ public void doListPathWithDots() {
@Test
public void doListPathWithDotsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'some.name', 'var': 'n')",
" upcase('n')",
" trim('n')",
Expand All @@ -237,7 +237,7 @@ public void doListPathWithDotsExplicitAppend() {
@Test
public void doListWithAppendAndLast() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" copy_field('c.name', 'author[].$append.name')",
" add_field('author[].$last.type', 'Default')",
Expand Down Expand Up @@ -295,7 +295,7 @@ public void doListEntitesToLiterals() {
@Test
public void doListEntitesToLiteralsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'creator', 'var': 'c')",
" upcase('c.name')",
" trim('c.name')",
Expand All @@ -322,7 +322,7 @@ public void doListEntitesToLiteralsExplicitAppend() {
@Test
public void doListEntitesToEntities() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" copy_field('c.name', 'author[].$append.name')",
" if all_contain('c.name', 'University')",
Expand Down Expand Up @@ -359,7 +359,7 @@ public void doListEntitesToEntities() {
@Test
public void wildcardForNestedEntities() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" if any_match('c.role.*.roleTerm.*.value','aut|cre')",
" copy_field('c.name', 'author[].$append.name')",
Expand Down Expand Up @@ -430,7 +430,7 @@ public void doListIndexedArray() {
@Test
public void doListIndexedArrayToArrayOfObjects() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n', 'author[].$append.name')",
"end",
Expand Down Expand Up @@ -483,7 +483,7 @@ public void doListIndexedArrayOfObjects() {
@Test
public void doListIndexedArrayOfObjectsExplicitAppend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author')",
"add_array('author')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n.name', 'author.$append')",
"end",
Expand All @@ -510,7 +510,7 @@ public void doListIndexedArrayOfObjectsExplicitAppend() {
@Test
public void doListIndexedArrayOfObjectsToArrayOfObjects() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"add_array('author[]')",
"do list('path': 'name[]', 'var': 'n')",
" copy_field('n.name', 'author[].$append.name')",
"end",
Expand Down Expand Up @@ -670,7 +670,7 @@ public void ifInCollectorCombine() {

private void shouldIterateOverList(final String path, final int expectedCount) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_array('trace')",
"do list(path: '" + path + "', 'var': '$i')",
" add_field('trace.$append', 'true')",
"end",
Expand Down Expand Up @@ -716,7 +716,7 @@ public void shouldIterateOverListWithWildcard() {

private void shouldIterateOverListOfHashes(final String path, final int expectedCount) {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('trace')",
"add_array('trace')",
"do list(path: '" + path + "', 'var': '$i')",
" add_field('trace.$append', 'true')",
"end",
Expand Down Expand Up @@ -771,7 +771,7 @@ public void shouldIterateOverListOfHashesWithWildcard() {
// See https://github.com/metafacture/metafacture-fix/issues/119
public void shouldPerformComplexOperationWithPathWildcard() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('coll[]')",
"add_array('coll[]')",
"do list(path: 'feld?', 'var': '$i')",
" add_field('coll[].$append.feldtest', 'true')",
" copy_field('$i.a.value', 'coll[].$last.a')",
Expand Down Expand Up @@ -847,7 +847,7 @@ public void shouldPerformComplexOperationWithPathWildcard() {
@Test
public void shouldDoListAsWithSingleList() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]')",
" copy_field(orgId, 'sourceOrga[].$append.id')",
"end"
Expand Down Expand Up @@ -881,7 +881,7 @@ public void shouldDoListAsWithSingleList() {
@Test
public void shouldDoListAsWithMultipleLists() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]', orgName: 'ccm:university_DISPLAYNAME[]', orgLoc: 'ccm:university_LOCATION[]')",
" copy_field(orgId, 'sourceOrga[].$append.id')",
" copy_field(orgName, 'sourceOrga[].$last.name')",
Expand Down Expand Up @@ -937,9 +937,9 @@ public void shouldDoListAsWithMultipleLists() {
@Test // checkstyle-disable-line JavaNCSS
public void shouldDoListAsWithMultipleListsOfDifferentSizes() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"add_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')",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ public void shouldReportArrayEntityAsArray() {
@Test
public void shouldReportEmptyArrayAsArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array(foo)",
"add_array(foo)",
"if is_array(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -1767,7 +1767,7 @@ public void shouldReportEmptyArrayEntityAsEmpty() {
@Test
public void shouldReportEmptyArrayAsEmpty() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array(foo)",
"add_array(foo)",
"if is_empty(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -2127,7 +2127,7 @@ public void shouldReportHashAsObject() {
@Test
public void shouldReportEmptyHashAsObject() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_hash(foo)",
"add_hash(foo)",
"if is_object(foo)",
" add_field(test,ok)",
"end"
Expand Down Expand Up @@ -2499,7 +2499,7 @@ public void shouldMatchString() {
@Test
public void shouldTestMacroVariable() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('type')",
"add_array('type')",
"do put_macro('test')",
" if str_contain('name', 'a$[var]')",
" add_field('type.$append', 'Organization: $[var]')",
Expand Down
Loading
Loading