Skip to content

Commit cdce76f

Browse files
authored
Merge pull request #356 from metafacture/333-defaultOptionWIP
Use the default option as in catmandu #333
2 parents 82d36d3 + 2460c92 commit cdce76f

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ Parameters:
740740

741741
Options:
742742

743-
- `__default`: Default value to use for unknown values. (Default: Old value)
743+
- `default`: Default value to use for unknown values. The option needs to be written in quotation marks because it is a reserved word in Java. (Default: Old value)
744744
- `delete`: Whether to delete unknown values. (Default: `false`)
745745
- `print_unknown`: Whether to print unknown values. (Default: `false`)
746746

@@ -782,7 +782,7 @@ put_rdfmap("path/to/file", "rdf-map", target: "<rdfProperty>")
782782
lookup("path.to.field", "rdf-map")
783783

784784
# with default value
785-
lookup("path.to.field", "map-name", __default: "NA")
785+
lookup("path.to.field", "map-name", "default": "NA")
786786

787787
# with printing unknown values to a file
788788
lookup("path.to.field", "map-name", print_unknown: "true", destination: "unknown.txt")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
555555
map = metafix.getMap(mapName);
556556
}
557557

558-
final String defaultValue = map.get(Maps.DEFAULT_MAP_KEY); // TODO: Catmandu uses 'default'
558+
final String defaultValue = options.getOrDefault("default", map.get(Maps.DEFAULT_MAP_KEY));
559559
final boolean delete = getBoolean(options, "delete");
560560
final boolean printUnknown = getBoolean(options, "print_unknown");
561561

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,14 @@ public void shouldLookupInSeparateInternalMap() {
453453
);
454454
}
455455

456+
@Test
457+
public void shouldUseDefaultOptionFromLookupOption() {
458+
assertMap(
459+
"put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')",
460+
LOOKUP + " 'testMap', 'default': 'Tach')"
461+
);
462+
}
463+
456464
@Test
457465
public void shouldLookupInSeparateExternalFileMap() {
458466
assertMap(
@@ -630,6 +638,52 @@ public void shouldUseDefaultValueIfNotFound() {
630638
);
631639
}
632640

641+
@Test
642+
public void shouldUseDefaultOptionValueIfNotFound() {
643+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
644+
"put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')",
645+
"lookup('title.*', 'testMap', 'default': 'Tach')"
646+
),
647+
i -> {
648+
i.startRecord("1");
649+
i.literal("title", "Aloha");
650+
i.literal("title", "Moin");
651+
i.literal("title", "Yo");
652+
i.endRecord();
653+
},
654+
o -> {
655+
o.get().startRecord("1");
656+
o.get().literal("title", "Alohaeha");
657+
o.get().literal("title", "Moin zäme");
658+
o.get().literal("title", "Tach");
659+
o.get().endRecord();
660+
}
661+
);
662+
}
663+
664+
@Test
665+
public void shouldPreferDefaultOptionValueOverDefaultMapValue() {
666+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
667+
"put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme', __default: Tach)",
668+
"lookup('title.*', 'testMap', 'default': 'Hi')"
669+
),
670+
i -> {
671+
i.startRecord("1");
672+
i.literal("title", "Aloha");
673+
i.literal("title", "Moin");
674+
i.literal("title", "Yo");
675+
i.endRecord();
676+
},
677+
o -> {
678+
o.get().startRecord("1");
679+
o.get().literal("title", "Alohaeha");
680+
o.get().literal("title", "Moin zäme");
681+
o.get().literal("title", "Hi");
682+
o.get().endRecord();
683+
}
684+
);
685+
}
686+
633687
@Test
634688
// See https://github.com/metafacture/metafacture-fix/issues/149
635689
public void shouldDeleteNonFoundLookupOnDemand() {

0 commit comments

Comments
 (0)