Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Fix emitting nested arrays.
Browse files Browse the repository at this point in the history
Nested arrays would always be flattened: #244 (comment)
  • Loading branch information
blackwinter committed Jul 15, 2022
1 parent 93b83e9 commit ecc1efb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private void emit(final String field, final Value value) {
final String fieldName = isMulti ? String.valueOf(i + 1) : field;

currentValue.matchType()
.ifArray(a -> emit(fieldName, currentValue))
.ifArray(a -> emit(isMulti ? fieldName + ARRAY_MARKER : fieldName, currentValue))
.ifHash(h -> {
outputStreamReceiver.startEntity(fieldName);
h.forEach(this::emit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,6 @@ public void shouldCopyToFieldWithTwoReservedFieldNames() {
}

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

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

@Test
@MetafixToDo("Arrays in arrays need to be preserved. See disabled isArray in FixPath#appendIn.")
public void shouldSplitMarkedArrayFieldIntoArrayOfArrays() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"split_field('date[]', '-')"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,12 +2456,12 @@ public void shouldDeleteEmptyArraysInArrays() {
i.endEntity();
i.endRecord();
},
o -> {
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("arrays[]");
// TODO: Preserve nested array!? (`1[]`)
o.get().startEntity("1[]");
o.get().literal("1", ":-P yuck");
o.get().endEntity();
f.apply(2).endEntity();
o.get().endRecord();
}
);
Expand Down Expand Up @@ -2564,6 +2564,63 @@ public void transformRepeatedField() {
);
}

@Test
public void shouldEmitNestedArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"nothing()"
),
i -> {
i.startRecord("1");
i.startEntity("test");
i.startEntity("zoo[]");
i.startEntity("1");
i.startEntity("animals[]");
i.startEntity("1[]");
i.literal("1", "ant");
i.literal("2", "dog");
i.endEntity();
i.literal("2", "cat");
i.startEntity("3[]");
i.literal("1", "fish");
i.startEntity("2[]");
i.literal("1", "zebra");
i.literal("2", "horse");
i.endEntity();
i.literal("3", "hippo");
i.endEntity();
i.literal("4", "giraffe");
i.endEntity();
i.endEntity();
i.endEntity();
i.endEntity();
i.endRecord();
},
(o, f) -> {
o.get().startRecord("1");
o.get().startEntity("test");
o.get().startEntity("zoo[]");
o.get().startEntity("1");
o.get().startEntity("animals[]");
o.get().startEntity("1[]");
o.get().literal("1", "ant");
o.get().literal("2", "dog");
o.get().endEntity();
o.get().literal("2", "cat");
o.get().startEntity("3[]");
o.get().literal("1", "fish");
o.get().startEntity("2[]");
o.get().literal("1", "zebra");
o.get().literal("2", "horse");
o.get().endEntity();
o.get().literal("3", "hippo");
o.get().endEntity();
o.get().literal("4", "giraffe");
f.apply(4).endEntity();
o.get().endRecord();
}
);
}

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

0 comments on commit ecc1efb

Please sign in to comment.