Skip to content

Commit 5cc686d

Browse files
committed
Add support for nested $first/$last in retain() Fix function. (#329)
1 parent 45bb75b commit 5cc686d

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

metafix/src/main/java/org/metafacture/metafix/Value.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,18 @@ private Set<Integer> findFields(final String pattern) {
484484
indexes().forEach(fieldSet::add);
485485
}
486486
else {
487-
final int index = Integer.parseInt(pattern) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
487+
final int index;
488+
489+
switch (pattern) {
490+
case "$first":
491+
index = 0;
492+
break;
493+
case "$last":
494+
index = size() - 1;
495+
break;
496+
default:
497+
index = Integer.parseInt(pattern) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
498+
}
488499

489500
if (index >= 0 && index < size()) {
490501
fieldSet.add(index);

metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,58 @@ public void retainNested() {
24182418
);
24192419
}
24202420

2421+
@Test
2422+
public void retainNestedReservedFields() {
2423+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2424+
"retain('b[].$first.b','c[].$last')"
2425+
),
2426+
i -> {
2427+
i.startRecord("1");
2428+
i.startEntity("b[]");
2429+
i.startEntity("1");
2430+
i.literal("a", "1");
2431+
i.literal("b", "2");
2432+
i.endEntity();
2433+
i.startEntity("2");
2434+
i.literal("a", "1");
2435+
i.literal("b", "2");
2436+
i.literal("c", "3");
2437+
i.endEntity();
2438+
i.startEntity("3");
2439+
i.literal("c", "4");
2440+
i.endEntity();
2441+
i.endEntity();
2442+
i.startEntity("c[]");
2443+
i.startEntity("1");
2444+
i.literal("a", "1");
2445+
i.literal("b", "2");
2446+
i.endEntity();
2447+
i.startEntity("2");
2448+
i.literal("a", "1");
2449+
i.literal("b", "2");
2450+
i.literal("c", "3");
2451+
i.endEntity();
2452+
i.startEntity("3");
2453+
i.literal("c", "4");
2454+
i.endEntity();
2455+
i.endEntity();
2456+
i.endRecord();
2457+
},
2458+
(o, f) -> {
2459+
o.get().startRecord("1");
2460+
o.get().startEntity("b[]");
2461+
o.get().startEntity("1");
2462+
o.get().literal("b", "2");
2463+
f.apply(2).endEntity();
2464+
o.get().startEntity("c[]");
2465+
o.get().startEntity("1");
2466+
o.get().literal("c", "4");
2467+
f.apply(2).endEntity();
2468+
o.get().endRecord();
2469+
}
2470+
);
2471+
}
2472+
24212473
@Test
24222474
public void shouldDeleteEmptyArrays() {
24232475
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

0 commit comments

Comments
 (0)