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

Commit

Permalink
Update path for repeated fields to indexed array (#102, #170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Apr 1, 2022
1 parent f4c4faf commit 4324378
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
5 changes: 3 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ private void addValue(final String name, final Value value) {
currentRecord.add(name, value);
}
else {
entities.get(index).matchType()
final Value entity = entities.get(index);
entity.matchType()
.ifArray(a -> a.add(value))
.ifHash(h -> h.add(name, value))
.ifHash(h -> h.add(name, value.updatePathAddBase(entity, name)))
.orElseThrow();
}
}
Expand Down
35 changes: 33 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Value {

private final Type type;

private final String path;
private String path;

public Value(final Array array) {
type = array != null ? Type.Array : null;
Expand Down Expand Up @@ -203,6 +203,25 @@ public Value asList(final Consumer<Array> consumer) {
}
}

/*package-private*/ Value updatePathAddBase(final Value container, final String fallback) {
if (container.path != null) {
final String[] pathSegments = split(path != null ? path : fallback);
final String lastSegment = pathSegments[pathSegments.length - 1];
this.path = container.path + "." + lastSegment;
}
return this;
}

private Value updatePathAppend(final String suffix, final String fallback) {
if (path != null) {
path = path + suffix;
}
else {
path = fallback + suffix;
}
return this;
}

public TypeMatcher matchType() {
return new TypeMatcher(this);
}
Expand Down Expand Up @@ -557,7 +576,19 @@ public void addAll(final Hash hash) {
*/
public void add(final String field, final Value newValue) {
final Value oldValue = new FixPath(field).findIn(this);
put(field, oldValue == null ? newValue : oldValue.asList(oldVals -> newValue.asList(newVals -> newVals.forEach(oldVals::add))));
if (oldValue == null) {
put(field, newValue);
}
else {
if (!oldValue.isArray()) { // repeated field: convert single val to first in array
oldValue.updatePathAppend(".1", field);
}
put(field, oldValue.asList(oldVals -> newValue.asList(newVals -> {
for (int i = 0; i < newVals.size(); ++i) {
oldVals.add(newVals.get(i).updatePathAppend("." + (i + 1 + oldVals.size()), field));
}
})));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,6 @@ public void shouldNotInsertOptionalArraySubFieldWithAsteriskInReplaceAll() {
}

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void shouldNotInsertOptionalRepeatedHashSubFieldWithAsteriskInReplaceAll() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"replace_all('coll.*.b', 'x', 'y')"
Expand Down

0 comments on commit 4324378

Please sign in to comment.