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

Commit

Permalink
Fix nested removal from array (#102, #113, #170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Mar 25, 2022
1 parent 8cfe1a8 commit e2cf74c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
26 changes: 17 additions & 9 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ void apply(final Array array, final String field, final Value value) {
else if (path.length >= 1 && Value.isNumber(path[0])) {
final int index = Integer.parseInt(path[0]) - 1; // TODO: 0-based Catmandu vs. 1-based Metafacture
if (index >= 0 && index < array.size()) {
array.remove(index);
if (path.length == 1) {
array.remove(index);
}
else {
removeNestedFrom(array.get(index));
}
}
}
}
Expand All @@ -211,14 +216,17 @@ else if (path.length >= 1 && Value.isNumber(path[0])) {
hash.remove(field);
}
else if (hash.containsField(field)) {
final Value value = hash.get(field);
// TODO: impl and call just value.remove
if (value != null) {
value.matchType()
.ifArray(a -> new FixPath(tail(path)).removeNestedFrom(a))
.ifHash(h -> new FixPath(tail(path)).removeNestedFrom(h))
.orElseThrow();
}
removeNestedFrom(hash.get(field));
}
}

private void removeNestedFrom(final Value value) {
// TODO: impl and call just value.remove
if (value != null) {
value.matchType()
.ifArray(a -> new FixPath(tail(path)).removeNestedFrom(a))
.ifHash(h -> new FixPath(tail(path)).removeNestedFrom(h))
.orElseThrow();
}
}

Expand Down
3 changes: 0 additions & 3 deletions metafix/src/test/java/org/metafacture/metafix/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;
import java.util.function.Consumer;

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

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

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void shouldFindArrayIndexSubfieldAfterTransformingOtherSubfieldToNull() {
shouldFindArrayIndexSubfield(record -> {
final String path = String.join(".", FIELD, "1", OTHER_FIELD);
Expand Down

This file was deleted.

0 comments on commit e2cf74c

Please sign in to comment.