From 269d2075f61c60850a91697344f37f6c42b82388 Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Thu, 10 Nov 2022 13:59:17 +0100 Subject: [PATCH] Add `list_as()` Fix bind. (#267) --- README.md | 4 ++ .../java/org/metafacture/metafix/FixBind.java | 28 ++++++++++++++ .../metafacture/metafix/MetafixBindTest.java | 37 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/README.md b/README.md index 41293d14..2cec9c90 100644 --- a/README.md +++ b/README.md @@ -678,6 +678,10 @@ do list(path: "", "var": "") end ``` +#### `do list_as` + +TODO + #### `do once` Executes the statements only once (when the bind is first encountered), not repeatedly for each record. diff --git a/metafix/src/main/java/org/metafacture/metafix/FixBind.java b/metafix/src/main/java/org/metafacture/metafix/FixBind.java index c942d913..25891280 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixBind.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixBind.java @@ -60,6 +60,34 @@ public void execute(final Metafix metafix, final Record record, final List params, final Map options, final RecordTransformer recordTransformer) { + final Map 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> executed = new HashMap<>(); diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java index 9daa45fd..07953cd8 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixBindTest.java @@ -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(