Skip to content

Commit

Permalink
Fix predicates should not implicitly match nested fields.
Browse files Browse the repository at this point in the history
Related to #115.
  • Loading branch information
blackwinter committed Jan 31, 2022
1 parent fc67506 commit da80860
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ enum Type {
String
}

/*private-private*/ static class TypeMatcher {
public static class TypeMatcher {

private final Set<Type> expected = new HashSet<>();
private final Value value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ default boolean testConditional(final Record record, final List<String> params,
final String string = params.get(1);

final Value value = record.find(field);
return value != null && qualifier.test(value.asList(null).asArray().stream(), v -> conditional.test(v.toString(), string));
return value != null && qualifier.test(value.asList(null).asArray().stream(), v -> v.extractType((m, c) -> m
.ifString(s -> c.accept(conditional.test(s, string)))
.orElse(w -> c.accept(false))
));
}

}
40 changes: 40 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,46 @@ public void ifAnyMatchNested() {
});
}

@Test
public void shouldNotImplicitlyMatchNestedField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"if any_match('author.name', '.*University.*')",
" add_field('author.type', 'Organization')",
"end"
),
i -> {
i.startRecord("1");
i.startEntity("author");
i.literal("name", "A University");
i.endEntity();
i.endRecord();

i.startRecord("2");
i.startEntity("author");
i.startEntity("name");
i.literal("label", "Some University");
i.endEntity();
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("author");
o.get().literal("name", "A University");
o.get().literal("type", "Organization");
o.get().endEntity();
o.get().endRecord();

o.get().startRecord("2");
o.get().startEntity("author");
o.get().startEntity("name");
o.get().literal("label", "Some University");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

@Test
public void ifAnyMatchFirstRecord() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

0 comments on commit da80860

Please sign in to comment.