Skip to content

Commit

Permalink
Handle $first, $last for find and replace in arrays (#135, #144)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Apr 26, 2022
1 parent b7c4462 commit 17f12cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
44 changes: 35 additions & 9 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private FixPath(final String[] path) {
final Value result;
if (path.length > 0) {
final String currentSegment = path[0];
final ReservedField reservedField = ReservedField.fromString(currentSegment);
if (currentSegment.equals(ASTERISK)) {
result = Value.newArray(resultArray -> array.forEach(v -> {
final Value findInValue = findInValue(v, tail(path));
Expand All @@ -79,6 +80,19 @@ private FixPath(final String[] path) {
}
}));
}
else if (reservedField != null) {
switch (reservedField) {
case $first:
result = findInValue(array.get(0), tail(path));
break;
case $last:
result = findInValue(array.get(array.size() - 1), tail(path));
break;
default:
result = null;
break;
}
}
else if (Value.isNumber(currentSegment)) {
final int index = Integer.parseInt(currentSegment) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
if (index >= 0 && index < array.size()) {
Expand Down Expand Up @@ -165,7 +179,25 @@ void apply(final Hash hash, final String field, final Value value) {
@Override
void apply(final Array array, final String field, final Value value) {
try {
array.set(Integer.valueOf(field) - 1, value);
final ReservedField reservedField = ReservedField.fromString(field);
if (reservedField != null) {
switch (reservedField) {
case $append:
array.add(value);
break;
case $first:
array.set(0, value);
break;
case $last:
array.set(array.size() - 1, value);
break;
default:
break;
}
}
else {
array.set(Integer.valueOf(field) - 1, value);
}
}
catch (final NumberFormatException e) {
throw new IllegalStateException("Expected Hash, got Array", e);
Expand Down Expand Up @@ -234,17 +266,11 @@ private void removeNestedFrom(final Value value) {
if (path.length == 1) {
if (field.equals(ASTERISK)) {
for (int i = 0; i < array.size(); ++i) {
mode.apply(array, "" + (i + 1), newValue);
mode.apply(array, String.valueOf(i + 1), newValue);
}
}
else {
// TODO unify ref usage from below
if ("$append".equals(field)) {
array.add(newValue);
}
else {
mode.apply(array, field, newValue);
}
mode.apply(array, field, newValue);
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;

@ExtendWith(MetafixToDo.Extension.class)
public class HashValueTest {

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

@Test
@MetafixToDo("Expected String, got Array")
public void shouldFindArrayWildcard() {
shouldFindArray("$last");
}
Expand All @@ -412,7 +409,6 @@ public void shouldFindArrayIndexSubfield() {
}

@Test
@MetafixToDo("Expected String, got Array")
public void shouldFindArrayWildcardSubfield() {
shouldFindArraySubfield("$last");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,6 @@ public void shouldReplaceAllRegexesInArrayByIndex() {
}

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

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

This file was deleted.

This file was deleted.

0 comments on commit 17f12cc

Please sign in to comment.