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

Commit

Permalink
Update path when adding, rename on copy/move field (#102, #170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Apr 1, 2022
1 parent 1d495ab commit ca1cba1
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
13 changes: 6 additions & 7 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ public void apply(final Metafix metafix, final Record record, final List<String>
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(v -> record.add(newName, v)));
Value.asList(record.get(oldName), a -> a.forEach(newValue -> {
record.add(newName, newValue);
newValue.updatePathRename(newName);
}));
}
},
format {
Expand Down Expand Up @@ -151,12 +154,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
move_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(v -> {
record.add(newName, v);
record.remove(oldName);
}));
FixMethod.copy_field.apply(metafix, record, params, options);
record.remove(params.get(0));
}
},
parse_text {
Expand Down
2 changes: 2 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ private Value processRef(final Value referencedValue, final InsertMode mode, fin
final String[] tail) {
if (referencedValue != null) {
final FixPath fixPath = new FixPath(tail);
newValue.updatePathAddBase(referencedValue, field);
return referencedValue.extractType((m, c) -> m
.ifArray(a -> c.accept(fixPath.insertInto(referencedValue.asArray(), mode, newValue)))
.ifHash(h -> c.accept(fixPath.insertInto(referencedValue.asHash(), mode, newValue)))
Expand Down Expand Up @@ -345,6 +346,7 @@ private Value getReferencedValue(final Array array, final String field) {
case $append:
referencedValue = Value.newHash(); // TODO: append non-hash?
array.add(referencedValue);
referencedValue.updatePathAppend(String.valueOf(array.size()), "");
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private void addValue(final String name, final Value value) {
else {
final Value entity = entities.get(index);
entity.matchType()
.ifArray(a -> a.add(value))
.ifArray(a -> a.add(value.updatePathAddBase(entity, name)))
.ifHash(h -> h.add(name, value.updatePathAddBase(entity, name)))
.orElseThrow();
}
Expand Down
9 changes: 8 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ public Value asList(final Consumer<Array> consumer) {
}
}

/*package-private*/ Value updatePathRename(final String newName) {
if (path != null) {
path = newName.replaceAll("\\$[^.]+", split(path)[0]);
}
return this;
}

/*package-private*/ Value updatePathAddBase(final Value container, final String fallback) {
if (container.path != null) {
final String[] pathSegments = split(path != null ? path : fallback);
Expand All @@ -212,7 +219,7 @@ public Value asList(final Consumer<Array> consumer) {
return this;
}

private Value updatePathAppend(final String suffix, final String fallback) {
/*package-private*/ Value updatePathAppend(final String suffix, final String fallback) {
if (path != null) {
path = path + suffix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,6 @@ public void inDoBindCopyFieldWithVarInSourceAndTarget() {
}

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void replaceAllWithWildcardAfterCopyFieldWithVarInSourceAndTarget() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('RSWK[]')",
Expand Down

This file was deleted.

0 comments on commit ca1cba1

Please sign in to comment.