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 22, 2022
1 parent 90cbde4 commit ae8be16
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 44 deletions.
83 changes: 47 additions & 36 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ private FixPath(final String[] path) {
/*package-private*/ Value findIn(final Array array) {

final Value result;
if (path.length > 0) {
if (path.length == 0) {
result = new Value(array);
}
else {
final String currentSegment = path[0];
if (currentSegment.equals(ASTERISK)) {
result = Value.newArray(resultArray -> array.forEach(v -> {
Expand All @@ -79,22 +82,16 @@ private FixPath(final String[] path) {
}
}));
}
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()) {
result = findInValue(array.get(index), tail(path));
else {
final Value referencedValue = getReferencedValue(array, currentSegment);
if (referencedValue != null) {
result = findInValue(referencedValue, tail(path));
}
// TODO: WDCD? copy_field('your.name','author[].name'), where name is an array
else {
result = null;
result = Value.newArray(a -> array.forEach(v -> a.add(findInValue(v, path))));
}
}
// TODO: WDCD? copy_field('your.name','author[].name'), where name is an array
else {
result = Value.newArray(a -> array.forEach(v -> a.add(findInValue(v, path))));
}
}
else {
result = new Value(array);
}
return result;

Expand Down Expand Up @@ -165,7 +162,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 +249,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 Expand Up @@ -334,20 +343,22 @@ private Value getReferencedValue(final Array array, final String field) {
if (reservedField == null && Value.isNumber(field)) {
return array.get(Integer.valueOf(field) - 1);
}
switch (reservedField) {
case $first:
referencedValue = array.get(0);
break;
case $last:
referencedValue = array.get(array.size() - 1);
break;
case $append:
referencedValue = Value.newHash(); // TODO: append non-hash?
array.add(referencedValue);
referencedValue.updatePathAppend(String.valueOf(array.size()), "");
break;
default:
break;
if (reservedField != null) {
switch (reservedField) {
case $first:
referencedValue = array.get(0);
break;
case $last:
referencedValue = array.get(array.size() - 1);
break;
case $append:
referencedValue = Value.newHash(); // TODO: append non-hash?
array.add(referencedValue);
referencedValue.updatePathAppend(String.valueOf(array.size()), "");
break;
default:
break;
}
}
return referencedValue;
}
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 ae8be16

Please sign in to comment.