Skip to content

Commit c20f3e6

Browse files
authored
Merge pull request #248 from metafacture/fixEmittingNestedArrays
Fix emitting nested arrays.
2 parents 8c90701 + ecc1efb commit c20f3e6

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
@@ -2405,7 +2405,6 @@ public void shouldCopyToFieldWithTwoReservedFieldNames() {
24052405
}
24062406

24072407
@Test
2408-
@MetafixToDo("Existing value has no path ('[[a]]'), resulting in wrong path for new value")
24092408
public void shouldMoveFieldToPathWithIndexAndReservedField() {
24102409
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
24112410
"move_field('b', 'names[].1.$append')"
@@ -2433,7 +2432,6 @@ public void shouldMoveFieldToPathWithIndexAndReservedField() {
24332432
}
24342433

24352434
@Test
2436-
@MetafixToDo("Existing value has no path ('[[a]]'), resulting in wrong path for new value")
24372435
public void shouldMoveFieldToPathWithTwoReservedFields() {
24382436
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
24392437
"move_field('b', 'names[].$first.$append')"
@@ -2975,7 +2973,6 @@ public void moveToNestedArray() {
29752973
}
29762974

29772975
@Test
2978-
@MetafixToDo("Arrays in arrays need to be preserved. See disabled isArray in FixPath#appendIn.")
29792976
public void shouldSplitMarkedArrayFieldIntoArrayOfArrays() {
29802977
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
29812978
"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)