Skip to content

Commit 1a39883

Browse files
committed
Add distinct objects when inserting with * wildcard
1 parent 73ef459 commit 1a39883

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,14 @@ private void removeNestedFrom(final Value value) {
248248

249249
/*package-private*/ private Value insertInto(final Array array, final InsertMode mode, final Value newValue) {
250250
// basic idea: reuse findIn logic here? setIn(findIn(array), newValue)
251+
251252
final String field = path[0];
252253
if (path.length == 1) {
253254
mode.apply(array, field, newValue);
254255
}
255256
else {
256257
if (ASTERISK.equals(field)) {
257-
array.forEach(value -> insertInto(value, mode, newValue, field, tail(path)));
258+
array.forEach(value -> insertInto(value, mode, newValue.copy(), field, tail(path)));
258259
}
259260
else if (isReference(field)) {
260261
insertInto(getReferencedValue(array, field), mode, newValue, field, tail(path));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ public String getPath() {
293293
this.path = path;
294294
}
295295

296+
/*package-private*/ Value copy() {
297+
return extractType((m, c) -> m
298+
.ifArray(oldArray -> c.accept(Value.newArray(newArray -> oldArray.forEach(v -> newArray.add(v)))))
299+
.ifHash(oldHash -> c.accept(Value.newHash(newHash -> oldHash.forEach((k, v) -> newHash.put(k, v)))))
300+
.ifString(s -> c.accept(new Value(s)))
301+
.orElseThrow());
302+
}
303+
296304
enum Type {
297305
Array,
298306
Hash,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,6 @@ public void shouldCopyBindVarWithDollarAfterLookup() {
20592059
}
20602060

20612061
@Test
2062-
@MetafixToDo("Should insert into specific object, but inserts into all")
20632062
public void shouldCopyToFieldWithIndexAndReservedFieldName() {
20642063
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
20652064
"set_array('a[].*.test[]', 'test')",
@@ -2099,7 +2098,6 @@ public void shouldCopyToFieldWithIndexAndReservedFieldName() {
20992098
}
21002099

21012100
@Test
2102-
@MetafixToDo("Should insert into specific object, but inserts into all")
21032101
public void shouldCopyToFieldWithTwoReservedFieldNames() {
21042102
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
21052103
"set_array('a[].*.test[]', 'test')",

0 commit comments

Comments
 (0)