Skip to content

Commit

Permalink
Add list_as() Fix bind. (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Nov 10, 2022
1 parent 9ee2194 commit 269d207
Showing 3 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -678,6 +678,10 @@ do list(path: "<sourceField>", "var": "<variableName>")
end
```

#### `do list_as`

TODO

#### `do once`

Executes the statements only once (when the bind is first encountered), not repeatedly for each record.
28 changes: 28 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixBind.java
Original file line number Diff line number Diff line change
@@ -60,6 +60,34 @@ public void execute(final Metafix metafix, final Record record, final List<Strin
}
},

list_as {
@Override
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
final Map<String, Value.Array> lists = new HashMap<>();
options.forEach((k, v) -> Value.asList(record.get(v), a -> lists.put(k, a)));

final int size = lists.values().stream().mapToInt(a -> a.size()).max().orElse(0);
for (int i = 0; i < size; ++i) {
final int index = i;

lists.forEach((k, v) -> {
final Value value = index < v.size() ? v.get(index) : null;

if (value != null) {
record.put(k, v.get(index));
}
else {
record.remove(k);
}
});

recordTransformer.transform(record);
}

lists.keySet().forEach(record::remove);
}
},

once {
private final Map<Metafix, Set<String>> executed = new HashMap<>();

37 changes: 37 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java
Original file line number Diff line number Diff line change
@@ -744,6 +744,43 @@ public void shouldPerformComplexOperationWithPathWildcard() {
);
}

@Test
public void shouldDoListAs() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('sourceOrga[]')",
"do list_as(orgId: 'ccm:university[]', orgName: 'ccm:university_DISPLAYNAME[]')",
" copy_field(orgId, 'sourceOrga[].$append.id')",
" copy_field(orgName, 'sourceOrga[].$last.name')",
"end"
),
i -> {
i.startRecord("1");
i.startEntity("ccm:university[]");
i.literal("1", "https://ror.org/0304hq317");
i.endEntity();
i.startEntity("ccm:university_DISPLAYNAME[]");
i.literal("1", "Gottfried Wilhelm Leibniz Universität Hannover");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("ccm:university[]");
o.get().literal("1", "https://ror.org/0304hq317");
o.get().endEntity();
o.get().startEntity("ccm:university_DISPLAYNAME[]");
o.get().literal("1", "Gottfried Wilhelm Leibniz Universität Hannover");
o.get().endEntity();
o.get().startEntity("sourceOrga[]");
o.get().startEntity("1");
o.get().literal("id", "https://ror.org/0304hq317");
o.get().literal("name", "Gottfried Wilhelm Leibniz Universität Hannover");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

@Test
public void shouldExecuteOnlyOnce() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

0 comments on commit 269d207

Please sign in to comment.