Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Merge pull request #356 from metafacture/333-defaultOptionWIP
Browse files Browse the repository at this point in the history
Use the default option as in catmandu #333
  • Loading branch information
TobiasNx authored Oct 14, 2024
2 parents 82d36d3 + 2460c92 commit cdce76f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down Expand Up @@ -782,7 +782,7 @@ put_rdfmap("path/to/file", "rdf-map", target: "<rdfProperty>")
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit cdce76f

Please sign in to comment.