Skip to content

Commit d00e59a

Browse files
committed
Update paths of moved values when inserting with $prepend (#127)
1 parent d20530e commit d00e59a

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,20 @@ public void add(final Value value) {
439439
}
440440

441441
/* package-private */ void add(final int index, final Value value) {
442+
add(index, value, true);
443+
}
444+
445+
/* package-private */ void add(final int index, final Value value, final boolean appendToPath) {
442446
if (!isNull(value)) {
443-
list.add(index, value);
447+
list.add(index, appendToPath ? value.withPathAppend(index + 1) : value);
448+
updateIndexesInPathsAfter(index);
449+
}
450+
}
451+
452+
private void updateIndexesInPathsAfter(final int start) {
453+
for (int index = start + 1; index < list.size(); index = index + 1) {
454+
final Value value = list.get(index);
455+
value.withPathSet(value.getPath().replaceAll("\\d+$", String.valueOf(index + 1)));
444456
}
445457
}
446458

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,14 +928,23 @@ public void shouldLookupInNestedArrays() {
928928
}
929929

930930
@Test
931-
public void shouldLookupInCopiedNestedArrays() {
931+
public void shouldLookupInCopiedNestedArraysCreatedWithAppend() {
932+
shouldLookupInCopiedNestedArraysCreatedWith("$append");
933+
}
934+
935+
@Test
936+
public void shouldLookupInCopiedNestedArraysCreatedWithPrepend() {
937+
shouldLookupInCopiedNestedArraysCreatedWith("$prepend");
938+
}
939+
940+
private void shouldLookupInCopiedNestedArraysCreatedWith(final String reservedField) {
932941
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
933942
"put_map('rswk-indicator', s: 'SubjectHeading')",
934943
"set_array('subject[]')",
935-
"set_array('subject[].$append.componentList[]')",
936-
"set_array('subject[].$last.componentList[].$append.type[]')",
944+
"set_array('subject[]." + reservedField + ".componentList[]')",
945+
"set_array('subject[].$last.componentList[]." + reservedField + ".type[]')",
937946
"do list(path: 'D', 'var': '$i')",
938-
" copy_field('$i', 'subject[].$last.componentList[].$last.type[].$append')",
947+
" copy_field('$i', 'subject[].$last.componentList[].$last.type[]." + reservedField + "')",
939948
"end",
940949
"lookup('subject[].*.componentList[].*.type[].*', 'rswk-indicator')",
941950
"retain('subject[]')"

0 commit comments

Comments
 (0)