Skip to content

Commit 17f12cc

Browse files
committed
Handle $first, $last for find and replace in arrays (#135, #144)
1 parent b7c4462 commit 17f12cc

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

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

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private FixPath(final String[] path) {
6868
final Value result;
6969
if (path.length > 0) {
7070
final String currentSegment = path[0];
71+
final ReservedField reservedField = ReservedField.fromString(currentSegment);
7172
if (currentSegment.equals(ASTERISK)) {
7273
result = Value.newArray(resultArray -> array.forEach(v -> {
7374
final Value findInValue = findInValue(v, tail(path));
@@ -79,6 +80,19 @@ private FixPath(final String[] path) {
7980
}
8081
}));
8182
}
83+
else if (reservedField != null) {
84+
switch (reservedField) {
85+
case $first:
86+
result = findInValue(array.get(0), tail(path));
87+
break;
88+
case $last:
89+
result = findInValue(array.get(array.size() - 1), tail(path));
90+
break;
91+
default:
92+
result = null;
93+
break;
94+
}
95+
}
8296
else if (Value.isNumber(currentSegment)) {
8397
final int index = Integer.parseInt(currentSegment) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
8498
if (index >= 0 && index < array.size()) {
@@ -165,7 +179,25 @@ void apply(final Hash hash, final String field, final Value value) {
165179
@Override
166180
void apply(final Array array, final String field, final Value value) {
167181
try {
168-
array.set(Integer.valueOf(field) - 1, value);
182+
final ReservedField reservedField = ReservedField.fromString(field);
183+
if (reservedField != null) {
184+
switch (reservedField) {
185+
case $append:
186+
array.add(value);
187+
break;
188+
case $first:
189+
array.set(0, value);
190+
break;
191+
case $last:
192+
array.set(array.size() - 1, value);
193+
break;
194+
default:
195+
break;
196+
}
197+
}
198+
else {
199+
array.set(Integer.valueOf(field) - 1, value);
200+
}
169201
}
170202
catch (final NumberFormatException e) {
171203
throw new IllegalStateException("Expected Hash, got Array", e);
@@ -234,17 +266,11 @@ private void removeNestedFrom(final Value value) {
234266
if (path.length == 1) {
235267
if (field.equals(ASTERISK)) {
236268
for (int i = 0; i < array.size(); ++i) {
237-
mode.apply(array, "" + (i + 1), newValue);
269+
mode.apply(array, String.valueOf(i + 1), newValue);
238270
}
239271
}
240272
else {
241-
// TODO unify ref usage from below
242-
if ("$append".equals(field)) {
243-
array.add(newValue);
244-
}
245-
else {
246-
mode.apply(array, field, newValue);
247-
}
273+
mode.apply(array, field, newValue);
248274
}
249275
}
250276
else {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import nl.jqno.equalsverifier.EqualsVerifier;
2020
import org.junit.jupiter.api.Assertions;
2121
import org.junit.jupiter.api.Test;
22-
import org.junit.jupiter.api.extension.ExtendWith;
2322

2423
import java.util.Arrays;
2524

26-
@ExtendWith(MetafixToDo.Extension.class)
2725
public class HashValueTest {
2826

2927
private static final String FIELD = "field";
@@ -394,7 +392,6 @@ public void shouldFindArrayIndex() {
394392
}
395393

396394
@Test
397-
@MetafixToDo("Expected String, got Array")
398395
public void shouldFindArrayWildcard() {
399396
shouldFindArray("$last");
400397
}
@@ -412,7 +409,6 @@ public void shouldFindArrayIndexSubfield() {
412409
}
413410

414411
@Test
415-
@MetafixToDo("Expected String, got Array")
416412
public void shouldFindArrayWildcardSubfield() {
417413
shouldFindArraySubfield("$last");
418414
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,6 @@ public void shouldReplaceAllRegexesInArrayByIndex() {
20512051
}
20522052

20532053
@Test
2054-
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/135")
20552054
public void shouldReplaceAllRegexesInArrayByArrayWildcard() {
20562055
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
20572056
"replace_all('names.$last', 'a', 'X')"
@@ -2103,7 +2102,6 @@ public void shouldReplaceAllRegexesInArraySubFieldByIndex() {
21032102
}
21042103

21052104
@Test
2106-
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/135")
21072105
public void shouldReplaceAllRegexesInArraySubFieldByArrayWildcard() {
21082106
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
21092107
"replace_all('names[].$last.name', 'a', 'X')"

metafix/src/test/resources/org/metafacture/metafix/integration/method/fromJson/toJson/prependArrayOfObjectsWithFirstArrayWildcard/todo.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

metafix/src/test/resources/org/metafacture/metafix/integration/method/fromJson/toJson/replace_allInSubfieldOfArrayOfObjectsWithLastWildcard/todo.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)