Skip to content

Commit

Permalink
Merge pull request #118 from metafacture/117-fixExistsNested
Browse files Browse the repository at this point in the history
Fix `exists()` Fix conditional with nested paths.
  • Loading branch information
blackwinter authored Jan 28, 2022
2 parents 25ab541 + 604af9e commit fc67506
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean test(final Metafix metafix, final Record record, final List<Strin
exists {
@Override
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
return record.containsField(params.get(0));
return record.containsPath(params.get(0));
}
},

Expand Down
31 changes: 31 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,37 @@ public boolean containsField(final String field) {
return matchFields(field, Stream::anyMatch);
}

public boolean containsPath(final String fieldPath) {
final String[] path = split(fieldPath);
final String field = path[0];

final boolean containsField = containsField(field);
final boolean containsPath;

if (containsField && path.length > 1) {
final Value value;

try {
value = find(path);
}
catch (final MetafactureException e) {
if (e.getCause() instanceof IllegalStateException) {
return false;
}
else {
throw e;
}
}

containsPath = !isNull(value);
}
else {
containsPath = containsField;
}

return containsPath;
}

/**
* Checks whether this hash is empty.
*
Expand Down
176 changes: 176 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/MetafixIfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,182 @@ public void shouldEqualAnyListBindWildcard() {
shouldEqualAnyListBind("$i.n?me");
}

@Test
public void shouldContainImmediateField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"if exists('name')",
" add_field('type', 'Organization')",
"end"
),
i -> {
i.startRecord("1");
i.literal("name", "Mary");
i.literal("name", "A University");
i.endRecord();

i.startRecord("2");
i.literal("name", "Mary");
i.literal("nome", "Max");
i.endRecord();

i.startRecord("3");
i.literal("nome", "Max");
i.endRecord();

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

o.get().startRecord("2");
o.get().literal("name", "Mary");
o.get().literal("nome", "Max");
o.get().literal("type", "Organization");
o.get().endRecord();

o.get().startRecord("3");
o.get().literal("nome", "Max");
o.get().endRecord();

o.get().startRecord("4");
o.get().endRecord();
}
);
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/117
public void shouldContainNestedField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"if exists('data.name')",
" add_field('type', 'Organization')",
"end"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.literal("name", "Mary");
i.literal("name", "A University");
i.endEntity();
i.endRecord();

i.startRecord("2");
i.startEntity("data");
i.literal("name", "Mary");
i.literal("nome", "Max");
i.endEntity();
i.endRecord();

i.startRecord("3");
i.literal("data", "Mary");
i.literal("name", "Max");
i.endRecord();

i.startRecord("4");
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().startEntity("name");
o.get().literal("1", "Mary");
o.get().literal("2", "A University");
f.apply(2).endEntity();
o.get().literal("type", "Organization");
o.get().endRecord();

o.get().startRecord("2");
o.get().startEntity("data");
o.get().literal("name", "Mary");
o.get().literal("nome", "Max");
o.get().endEntity();
o.get().literal("type", "Organization");
o.get().endRecord();

o.get().startRecord("3");
o.get().literal("data", "Mary");
o.get().literal("name", "Max");
o.get().endRecord();

o.get().startRecord("4");
o.get().endRecord();
}
);
}

@Test
// See https://github.com/metafacture/metafacture-fix/issues/117
public void shouldContainNestedArrayField() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"if exists('data[].*.name')",
" add_field('type', 'Organization')",
"end"
),
i -> {
i.startRecord("1");
i.startEntity("data[]");
i.startEntity("1");
i.literal("nome", "Mary");
i.endEntity();
i.startEntity("2");
i.literal("name", "A University");
i.endEntity();
i.endEntity();
i.endRecord();

i.startRecord("2");
i.startEntity("data[]");
i.literal("1", "Mary");
i.literal("2", "Max");
i.endEntity();
i.endRecord();

i.startRecord("3");
i.literal("data", "Mary");
i.literal("name", "Max");
i.endRecord();

i.startRecord("4");
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("data[]");
o.get().startEntity("1");
o.get().literal("nome", "Mary");
o.get().endEntity();
o.get().startEntity("2");
o.get().literal("name", "A University");
f.apply(2).endEntity();
o.get().literal("type", "Organization");
o.get().endRecord();

o.get().startRecord("2");
o.get().startEntity("data[]");
o.get().literal("1", "Mary");
o.get().literal("2", "Max");
o.get().endEntity();
o.get().literal("type", "Organization"); // FIXME: `data[].*.name` must not return `[Mary, Max]`
o.get().endRecord();

o.get().startRecord("3");
o.get().literal("data", "Mary");
o.get().literal("name", "Max");
o.get().endRecord();

o.get().startRecord("4");
o.get().endRecord();
}
);
}

@Test
public void shouldResolveVariablesInIf() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,16 @@ public void arrayFromHash() {
@Test
public void reject() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"if exists ('_metadata.error')",
"if exists('_metadata.error')",
" reject()",
"end"),
i -> {
i.startRecord("1");
i.literal("_metadata.error", "details");
i.startEntity("_metadata");
i.literal("error", "details");
i.endEntity();
i.endRecord();

i.startRecord("2");
i.endRecord();
}, o -> {
Expand Down

0 comments on commit fc67506

Please sign in to comment.