Skip to content

Commit dec5b36

Browse files
committed
Fix destination option for lookup() Fix function. (#269)
Also add `prefix` option.
1 parent 41a1a5c commit dec5b36

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ Additional options when printing unknown values:
575575
- `footer`: Footer which is written at the end of the output. (Default: `\n`)
576576
- `header`: Header which is written at the beginning of the output. (Default: Empty string)
577577
- `id`: Field name which contains the record ID; if found, will be available for inclusion in `destination`. (Default: `_id`)
578+
- `prefix`: Prefix to print before the unknown value; may include [format directives](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax) for counter and record ID (in that order). (Default: Empty string)
578579
- `separator`: Separator which is written after the unknown value. (Default: `\n`)
579580

580581
```perl

metafix/src/main/java/org/metafacture/metafix/FixMethod.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.metafacture.metafix;
1818

19-
import org.metafacture.framework.StandardEventNames;
20-
import org.metafacture.io.ObjectWriter;
2119
import org.metafacture.metafix.api.FixFunction;
2220
import org.metafacture.metamorph.api.Maps;
2321
import org.metafacture.metamorph.functions.ISBN;
@@ -242,31 +240,25 @@ private boolean literalString(final String s) {
242240

243241
@Override
244242
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
245-
final Value idValue = record.get(options.getOrDefault("id", StandardEventNames.ID));
246-
247243
final boolean internal = getBoolean(options, "internal");
248244
final boolean pretty = getBoolean(options, "pretty");
249245

250-
final LongAdder counter = scopedCounter.computeIfAbsent(metafix, k -> new LongAdder());
251-
counter.increment();
252-
253-
final UnaryOperator<String> formatter = s -> String.format(s,
254-
counter.sum(), Value.isNull(idValue) ? "" : idValue.toString());
255-
256-
final String prefix = params.isEmpty() ? "" : formatter.apply(params.get(0));
246+
if (!params.isEmpty()) {
247+
options.put("prefix", params.get(0));
248+
}
257249

258-
withWriter(options, formatter, w -> {
250+
withWriter(metafix, record, options, scopedCounter, c -> {
259251
if (internal) {
260252
if (pretty) {
261-
record.forEach((f, v) -> w.process(prefix + f + "=" + v));
253+
record.forEach((f, v) -> c.accept(f + "=" + v));
262254
}
263255
else {
264-
w.process(prefix + record);
256+
c.accept(record.toString());
265257
}
266258
}
267259
else {
268260
try {
269-
w.process(prefix + record.toJson(pretty));
261+
c.accept(record.toJson(pretty));
270262
}
271263
catch (final IOException e) {
272264
// Log a warning? Print string representation instead?
@@ -474,6 +466,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
474466
}
475467
},
476468
lookup {
469+
private final Map<Metafix, LongAdder> scopedCounter = new HashMap<>();
470+
477471
@Override
478472
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
479473
final Map<String, String> map;
@@ -500,14 +494,14 @@ public void apply(final Metafix metafix, final Record record, final List<String>
500494
final boolean delete = getBoolean(options, "delete");
501495
final boolean printUnknown = getBoolean(options, "print_unknown");
502496

503-
final Consumer<ObjectWriter<String>> consumer = w -> record.transform(params.get(0), oldValue -> {
497+
final Consumer<Consumer<String>> consumer = c -> record.transform(params.get(0), oldValue -> {
504498
final String newValue = map.get(oldValue);
505499
if (newValue != null) {
506500
return newValue;
507501
}
508502
else {
509-
if (w != null) {
510-
w.process(oldValue);
503+
if (c != null) {
504+
c.accept(oldValue);
511505
}
512506

513507
return defaultValue != null ? defaultValue : delete ? null : oldValue;
@@ -516,7 +510,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
516510

517511
if (printUnknown) {
518512
options.putIfAbsent("append", "true");
519-
withWriter(options, null, consumer);
513+
withWriter(metafix, record, options, scopedCounter, consumer);
520514
}
521515
else {
522516
consumer.accept(null);

metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.metafacture.metafix.api;
1818

19+
import org.metafacture.framework.StandardEventNames;
1920
import org.metafacture.io.ObjectWriter;
2021
import org.metafacture.metafix.Metafix;
2122
import org.metafacture.metafix.Record;
@@ -25,6 +26,7 @@
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Set;
29+
import java.util.concurrent.atomic.LongAdder;
2830
import java.util.function.BiFunction;
2931
import java.util.function.Consumer;
3032
import java.util.function.UnaryOperator;
@@ -64,6 +66,19 @@ default void withWriter(final Map<String, String> options, final UnaryOperator<S
6466
}
6567
}
6668

69+
default void withWriter(final Metafix metafix, final Record record, final Map<String, String> options, final Map<Metafix, LongAdder> scopedCounter, final Consumer<Consumer<String>> consumer) {
70+
final Value idValue = record.get(options.getOrDefault("id", StandardEventNames.ID));
71+
72+
final LongAdder counter = scopedCounter.computeIfAbsent(metafix, k -> new LongAdder());
73+
counter.increment();
74+
75+
final UnaryOperator<String> formatter = s -> String.format(s,
76+
counter.sum(), Value.isNull(idValue) ? "" : idValue.toString());
77+
78+
final String prefix = formatter.apply(options.getOrDefault("prefix", ""));
79+
withWriter(options, formatter, w -> consumer.accept(s -> w.process(prefix + s)));
80+
}
81+
6782
default boolean getBoolean(final Map<String, String> options, final String key) {
6883
return Boolean.parseBoolean(options.get(key));
6984
}

metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ private void shouldPrintUnknown(final String args, final String defaultValue, fi
854854
),
855855
i -> {
856856
i.startRecord("rec1");
857+
i.literal("name", "moe");
857858
i.literal("title", "Aloha");
858859
i.literal("title", "Moin");
859860
i.literal("title", "Hey");
@@ -862,6 +863,7 @@ private void shouldPrintUnknown(final String args, final String defaultValue, fi
862863
i.endRecord();
863864

864865
i.startRecord("rec2");
866+
i.literal("name", "joe");
865867
i.literal("title", "Aloha");
866868
i.literal("title", "you");
867869
i.literal("title", "too");
@@ -871,6 +873,7 @@ private void shouldPrintUnknown(final String args, final String defaultValue, fi
871873
final boolean delete = "__delete".equals(defaultValue);
872874

873875
o.get().startRecord("rec1");
876+
o.get().literal("name", "moe");
874877
o.get().literal("title", "Alohaeha");
875878
o.get().literal("title", "Moin zäme");
876879

@@ -886,6 +889,7 @@ else if (!delete) {
886889
o.get().endRecord();
887890

888891
o.get().startRecord("rec2");
892+
o.get().literal("name", "joe");
889893
o.get().literal("title", "Alohaeha");
890894

891895
if (defaultValue == null) {
@@ -917,6 +921,16 @@ public void shouldPrintUnknownWithDelete() {
917921
shouldPrintUnknown(", delete: 'true'", "__delete", "Hey\nyou\nthere\nyou\ntoo\n");
918922
}
919923

924+
@Test
925+
public void shouldPrintUnknownWithPrefix() {
926+
shouldPrintUnknown(", prefix: '<%d:%s>'", null, "<1:rec1>Hey\n<1:rec1>you\n<1:rec1>there\n<2:rec2>you\n<2:rec2>too\n");
927+
}
928+
929+
@Test
930+
public void shouldPrintUnknownWithPrefixAndIdField() {
931+
shouldPrintUnknown(", prefix: '<%d:%s>', id: 'name'", null, "<1:moe>Hey\n<1:moe>you\n<1:moe>there\n<2:joe>you\n<2:joe>too\n");
932+
}
933+
920934
@Test
921935
public void shouldPrintUnknownWithHeader() {
922936
shouldPrintUnknown(", header: '<%d:%s>'", null, "<%d:%s>Hey\nyou\nthere\n<%d:%s>you\ntoo\n");

0 commit comments

Comments
 (0)