Skip to content

Commit

Permalink
Add support for nested $first/$last in retain() Fix function. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Nov 8, 2023
1 parent 45bb75b commit 5cc686d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
13 changes: 12 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,18 @@ private Set<Integer> findFields(final String pattern) {
indexes().forEach(fieldSet::add);
}
else {
final int index = Integer.parseInt(pattern) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
final int index;

switch (pattern) {
case "$first":
index = 0;
break;
case "$last":
index = size() - 1;
break;
default:
index = Integer.parseInt(pattern) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
}

if (index >= 0 && index < size()) {
fieldSet.add(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,58 @@ public void retainNested() {
);
}

@Test
public void retainNestedReservedFields() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"retain('b[].$first.b','c[].$last')"
),
i -> {
i.startRecord("1");
i.startEntity("b[]");
i.startEntity("1");
i.literal("a", "1");
i.literal("b", "2");
i.endEntity();
i.startEntity("2");
i.literal("a", "1");
i.literal("b", "2");
i.literal("c", "3");
i.endEntity();
i.startEntity("3");
i.literal("c", "4");
i.endEntity();
i.endEntity();
i.startEntity("c[]");
i.startEntity("1");
i.literal("a", "1");
i.literal("b", "2");
i.endEntity();
i.startEntity("2");
i.literal("a", "1");
i.literal("b", "2");
i.literal("c", "3");
i.endEntity();
i.startEntity("3");
i.literal("c", "4");
i.endEntity();
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("b[]");
o.get().startEntity("1");
o.get().literal("b", "2");
f.apply(2).endEntity();
o.get().startEntity("c[]");
o.get().startEntity("1");
o.get().literal("c", "4");
f.apply(2).endEntity();
o.get().endRecord();
}
);
}

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

0 comments on commit 5cc686d

Please sign in to comment.