Skip to content

Commit 496dac4

Browse files
committed
Fix issue with dollars in var paths when handled as regex
Found via regression in OERSI, see new unit test and https://gitlab.com/oersi/oersi-etl/-/merge_requests/175
1 parent 1f9d5c2 commit 496dac4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

metafix/src/main/java/org/metafacture/metafix/FixPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private String[] tail(final String[] fields) {
298298
return Arrays.copyOfRange(fields, 1, fields.length);
299299
}
300300

301-
private enum ReservedField {
301+
/*package-private*/ enum ReservedField {
302302
$append, $first, $last;
303303

304304
private static final Map<String, ReservedField> STRING_TO_ENUM = new HashMap<>();

metafix/src/main/java/org/metafacture/metafix/Value.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.metafacture.metafix;
1818

1919
import org.metafacture.commons.tries.SimpleRegexTrie;
20+
import org.metafacture.metafix.FixPath.ReservedField;
2021

2122
import java.util.ArrayList;
2223
import java.util.Collection;
@@ -208,7 +209,10 @@ public Value asList(final Consumer<Array> consumer) {
208209

209210
/*package-private*/ Value updatePathRename(final String newName) {
210211
if (path != null) {
211-
path = newName.replaceAll("\\$[^.]+", split(path)[0]);
212+
final String basePath = split(path)[0];
213+
for (final ReservedField rf : ReservedField.values()) {
214+
path = newName.replace(rf.name(), basePath);
215+
}
212216
}
213217
return this;
214218
}

metafix/src/test/java/org/metafacture/metafix/MetafixMethodTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,33 @@ public void shouldReplaceAllRegexesInListCopiedArraySubField() {
20312031
);
20322032
}
20332033

2034+
@Test
2035+
public void shouldCopyBindVarWithDollarAfterLookup() {
2036+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2037+
"set_array('@coll[]')",
2038+
"do list(path: 'a', 'var': '$i')",
2039+
" lookup('$i.name')",
2040+
" copy_field('$i.name', '@coll[].$append')",
2041+
"end",
2042+
"remove_field('a')"
2043+
),
2044+
i -> {
2045+
i.startRecord("1");
2046+
i.startEntity("a");
2047+
i.literal("name", "Dog");
2048+
i.endEntity();
2049+
i.endRecord();
2050+
},
2051+
(o, f) -> {
2052+
o.get().startRecord("1");
2053+
o.get().startEntity("@coll[]");
2054+
o.get().literal("1", "Dog");
2055+
f.apply(1).endEntity();
2056+
o.get().endRecord();
2057+
}
2058+
);
2059+
}
2060+
20342061
@Test
20352062
// See https://github.com/metafacture/metafacture-fix/issues/121
20362063
public void shouldReplaceAllRegexesInNestedArray() {

0 commit comments

Comments
 (0)