diff --git a/README.md b/README.md index cee1233d..b5d82efb 100644 --- a/README.md +++ b/README.md @@ -740,7 +740,7 @@ Parameters: Options: -- `__default`: Default value to use for unknown values. (Default: Old value) +- `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) - `delete`: Whether to delete unknown values. (Default: `false`) - `print_unknown`: Whether to print unknown values. (Default: `false`) @@ -782,7 +782,7 @@ put_rdfmap("path/to/file", "rdf-map", target: "") lookup("path.to.field", "rdf-map") # with default value -lookup("path.to.field", "map-name", __default: "NA") +lookup("path.to.field", "map-name", "default": "NA") # with printing unknown values to a file lookup("path.to.field", "map-name", print_unknown: "true", destination: "unknown.txt") diff --git a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java index 7bd18bf6..8eddcfe5 100644 --- a/metafix/src/main/java/org/metafacture/metafix/FixMethod.java +++ b/metafix/src/main/java/org/metafacture/metafix/FixMethod.java @@ -555,7 +555,7 @@ public void apply(final Metafix metafix, final Record record, final List map = metafix.getMap(mapName); } - final String defaultValue = map.get(Maps.DEFAULT_MAP_KEY); // TODO: Catmandu uses 'default' + final String defaultValue = options.getOrDefault("default", map.get(Maps.DEFAULT_MAP_KEY)); final boolean delete = getBoolean(options, "delete"); final boolean printUnknown = getBoolean(options, "print_unknown"); diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java index e2de4a02..d527fb80 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java @@ -453,6 +453,14 @@ public void shouldLookupInSeparateInternalMap() { ); } + @Test + public void shouldUseDefaultOptionFromLookupOption() { + assertMap( + "put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')", + LOOKUP + " 'testMap', 'default': 'Tach')" + ); + } + @Test public void shouldLookupInSeparateExternalFileMap() { assertMap( @@ -630,6 +638,52 @@ public void shouldUseDefaultValueIfNotFound() { ); } + @Test + public void shouldUseDefaultOptionValueIfNotFound() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')", + "lookup('title.*', 'testMap', 'default': 'Tach')" + ), + i -> { + i.startRecord("1"); + i.literal("title", "Aloha"); + i.literal("title", "Moin"); + i.literal("title", "Yo"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().literal("title", "Alohaeha"); + o.get().literal("title", "Moin zäme"); + o.get().literal("title", "Tach"); + o.get().endRecord(); + } + ); + } + + @Test + public void shouldPreferDefaultOptionValueOverDefaultMapValue() { + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( + "put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme', __default: Tach)", + "lookup('title.*', 'testMap', 'default': 'Hi')" + ), + i -> { + i.startRecord("1"); + i.literal("title", "Aloha"); + i.literal("title", "Moin"); + i.literal("title", "Yo"); + i.endRecord(); + }, + o -> { + o.get().startRecord("1"); + o.get().literal("title", "Alohaeha"); + o.get().literal("title", "Moin zäme"); + o.get().literal("title", "Hi"); + o.get().endRecord(); + } + ); + } + @Test // See https://github.com/metafacture/metafacture-fix/issues/149 public void shouldDeleteNonFoundLookupOnDemand() {