Skip to content

Commit ecc1efb

Browse files
committed
Fix emitting nested arrays.
Nested arrays would always be flattened: metafacture/metafacture-fix#244 (comment)
1 parent 93b83e9 commit ecc1efb

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private void emit(final String field, final Value value) {
218218
final String fieldName = isMulti ? String.valueOf(i + 1) : field;
219219

220220
currentValue.matchType()
221-
.ifArray(a -> emit(fieldName, currentValue))
221+
.ifArray(a -> emit(isMulti ? fieldName + ARRAY_MARKER : fieldName, currentValue))
222222
.ifHash(h -> {
223223
outputStreamReceiver.startEntity(fieldName);
224224
h.forEach(this::emit);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,6 @@ public void shouldCopyToFieldWithTwoReservedFieldNames() {
22552255
}
22562256

22572257
@Test
2258-
@MetafixToDo("Existing value has no path ('[[a]]'), resulting in wrong path for new value")
22592258
public void shouldMoveFieldToPathWithIndexAndReservedField() {
22602259
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
22612260
"move_field('b', 'names[].1.$append')"
@@ -2283,7 +2282,6 @@ public void shouldMoveFieldToPathWithIndexAndReservedField() {
22832282
}
22842283

22852284
@Test
2286-
@MetafixToDo("Existing value has no path ('[[a]]'), resulting in wrong path for new value")
22872285
public void shouldMoveFieldToPathWithTwoReservedFields() {
22882286
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
22892287
"move_field('b', 'names[].$first.$append')"
@@ -2825,7 +2823,6 @@ public void moveToNestedArray() {
28252823
}
28262824

28272825
@Test
2828-
@MetafixToDo("Arrays in arrays need to be preserved. See disabled isArray in FixPath#appendIn.")
28292826
public void shouldSplitMarkedArrayFieldIntoArrayOfArrays() {
28302827
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
28312828
"split_field('date[]', '-')"

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,12 +2456,12 @@ public void shouldDeleteEmptyArraysInArrays() {
24562456
i.endEntity();
24572457
i.endRecord();
24582458
},
2459-
o -> {
2459+
(o, f) -> {
24602460
o.get().startRecord("1");
24612461
o.get().startEntity("arrays[]");
2462-
// TODO: Preserve nested array!? (`1[]`)
2462+
o.get().startEntity("1[]");
24632463
o.get().literal("1", ":-P yuck");
2464-
o.get().endEntity();
2464+
f.apply(2).endEntity();
24652465
o.get().endRecord();
24662466
}
24672467
);
@@ -2564,6 +2564,63 @@ public void transformRepeatedField() {
25642564
);
25652565
}
25662566

2567+
@Test
2568+
public void shouldEmitNestedArray() {
2569+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2570+
"nothing()"
2571+
),
2572+
i -> {
2573+
i.startRecord("1");
2574+
i.startEntity("test");
2575+
i.startEntity("zoo[]");
2576+
i.startEntity("1");
2577+
i.startEntity("animals[]");
2578+
i.startEntity("1[]");
2579+
i.literal("1", "ant");
2580+
i.literal("2", "dog");
2581+
i.endEntity();
2582+
i.literal("2", "cat");
2583+
i.startEntity("3[]");
2584+
i.literal("1", "fish");
2585+
i.startEntity("2[]");
2586+
i.literal("1", "zebra");
2587+
i.literal("2", "horse");
2588+
i.endEntity();
2589+
i.literal("3", "hippo");
2590+
i.endEntity();
2591+
i.literal("4", "giraffe");
2592+
i.endEntity();
2593+
i.endEntity();
2594+
i.endEntity();
2595+
i.endEntity();
2596+
i.endRecord();
2597+
},
2598+
(o, f) -> {
2599+
o.get().startRecord("1");
2600+
o.get().startEntity("test");
2601+
o.get().startEntity("zoo[]");
2602+
o.get().startEntity("1");
2603+
o.get().startEntity("animals[]");
2604+
o.get().startEntity("1[]");
2605+
o.get().literal("1", "ant");
2606+
o.get().literal("2", "dog");
2607+
o.get().endEntity();
2608+
o.get().literal("2", "cat");
2609+
o.get().startEntity("3[]");
2610+
o.get().literal("1", "fish");
2611+
o.get().startEntity("2[]");
2612+
o.get().literal("1", "zebra");
2613+
o.get().literal("2", "horse");
2614+
o.get().endEntity();
2615+
o.get().literal("3", "hippo");
2616+
o.get().endEntity();
2617+
o.get().literal("4", "giraffe");
2618+
f.apply(4).endEntity();
2619+
o.get().endRecord();
2620+
}
2621+
);
2622+
}
2623+
25672624
@Test
25682625
public void emitEntityForRepeatedField() {
25692626
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

0 commit comments

Comments
 (0)