Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Use the default option as in catmandu #333 #356

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,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 @@ -748,7 +748,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 @@ -517,7 +517,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
Loading