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

Commit

Permalink
Tweak lookup behavior, partial delete support (#102, #149)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Mar 4, 2022
1 parent 34c96f1 commit e9dd4ab
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 35 deletions.
5 changes: 4 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ public void apply(final Metafix metafix, final Record record, final List<String>
}

final String defaultValue = map.get(Maps.DEFAULT_MAP_KEY); // TODO: Catmandu uses 'default'
record.transform(params.get(0), k -> map.getOrDefault(k, defaultValue));
record.transform(params.get(0), oldValue -> {
final String newValue = map.getOrDefault(oldValue, defaultValue);
return newValue != null ? newValue : Boolean.valueOf(options.getOrDefault("delete", "false")) ? null : oldValue;
});
}
},
prepend {
Expand Down
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void emit(final String field, final Value value) {
h.forEach(this::emit);
outputStreamReceiver.endEntity();
})
.orElse(v -> outputStreamReceiver.literal(fieldName, v.asString()));
.ifString(s -> outputStreamReceiver.literal(fieldName, s));
}

if (isMulti) {
Expand Down
3 changes: 2 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ private <T> TypeMatcher match(final Type type, final Consumer<T> consumer, final

private abstract static class AbstractValueType {

protected static final Predicate<Value> REMOVE_EMPTY_VALUES = v -> v.extractType((m, c) -> m
protected static final Predicate<Value> REMOVE_EMPTY_VALUES = v ->
v.isNull() ? true : v.extractType((m, c) -> m
.ifArray(a -> {
a.removeEmptyValues();
c.accept(a.isEmpty());
Expand Down
196 changes: 164 additions & 32 deletions metafix/src/test/java/org/metafacture/metafix/MetafixLookupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,156 @@ public void shouldIgnoreOptionsOnLookupInSeparateExternalFileMap() {

@Test
public void shouldNotLookupInExternalFileMapWithWrongOptions() {
MetafixTestHelpers.assertThrows(IllegalStateException.class, "Expected String, got null", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
LOOKUP + " '" + CSV_MAP + "', sep_char: '\t')"
),
i -> {
i.startRecord("1");
i.literal("title", "Aloha");
i.literal("title", "Moin");
i.literal("title", "Hey");
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().endRecord();
}
)
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
LOOKUP + " '" + CSV_MAP + "', sep_char: '\t', delete: 'true')"
),
i -> {
i.startRecord("1");
i.literal("title", "Aloha");
i.literal("title", "Moin");
i.literal("title", "Hey");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().endRecord();
}
);
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/149
public void shouldKeepOriginalValueIfNotFoundAndNoDefault() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"lookup('title.*', Aloha: 'Alohaeha', 'Moin': 'Moin zäme')"
),
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", "Yo");
o.get().endRecord();
}
);
}

@Test
public void shouldUseDefaultValueIfNotFound() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"lookup('title.*', Aloha: 'Alohaeha', 'Moin': 'Moin zäme', __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
// See https://github.com/metafacture/metafacture-fix/issues/149
public void shouldDeleteNonFoundLookupOnDemand() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"lookup('title.*', Aloha: Alohaeha, 'Moin': 'Moin zäme', delete: 'true')"
),
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().endRecord();
}
);
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/149
public void shouldNotDeleteNonFoundLookupExplicitly() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"lookup('title.*', Aloha: Alohaeha, 'Moin': 'Moin zäme', delete: 'false')"
),
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", "Yo");
o.get().endRecord();
}
);
}

@Test
public void shouldDeleteNonFoundLookupOnDemandAndVacuum() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')",
"lookup('title.*', testMap, delete: 'true')",
"vacuum()"
),
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().endRecord();
}
);
}

@Test
public void shouldDeleteNonFoundLookupOnDemandAndMove() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"put_map('testMap', Aloha: Alohaeha, 'Moin': 'Moin zäme')",
"lookup('title.*', testMap, delete: 'true')",
"move_field('title','t')"
),
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("t", "Alohaeha");
o.get().literal("t", "Moin zäme");
o.get().endRecord();
}
);
}

Expand All @@ -282,22 +416,20 @@ public void shouldIgnoreOptionsOnSubsequentLookupInExternalFileMap() {

@Test
public void shouldNotLookupInUnknownInternalMap() {
MetafixTestHelpers.assertThrows(IllegalStateException.class, "Expected String, got null", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
LOOKUP + " 'testMap')"
),
i -> {
i.startRecord("1");
i.literal("title", "Aloha");
i.literal("title", "Moin");
i.literal("title", "Hey");
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().endRecord();
}
)
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
LOOKUP + " 'testMap', delete: 'true')"
),
i -> {
i.startRecord("1");
i.literal("title", "Aloha");
i.literal("title", "Moin");
i.literal("title", "Hey");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().endRecord();
}
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/metafacture/metafacture-fix/issues/149
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See https://github.com/metafacture/metafacture-fix/issues/92

0 comments on commit e9dd4ab

Please sign in to comment.