Skip to content

Commit c5aa947

Browse files
committed
Wrap string value into array only on $prepend / $append (#127)
1 parent d00e59a commit c5aa947

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ private Value insertInto(final Value value, final InsertMode mode, final Value n
301301

302302
private Value getContainerValue(final Hash hash, final String field, final String newPath, final String nextField) {
303303
Value result = hash.get(field);
304+
final boolean isAddingToArray = nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name());
304305
if (result == null) {
305-
result = (nextField.equals(ReservedField.$prepend.name()) || nextField.equals(ReservedField.$append.name()) ?
306-
Value.newArray() : Value.newHash()).withPathSet(newPath);
306+
result = (isAddingToArray ? Value.newArray() : Value.newHash()).withPathSet(newPath);
307307
hash.put(field, result);
308308
}
309309
else {
310-
if (result.isString()) {
310+
if (isAddingToArray && result.isString()) {
311311
final Value value = result;
312312
result = Value.newArray(a -> a.add(value));
313313
hash.put(field, result);

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -394,38 +394,21 @@ public void addWithPrependInImplicitArray() {
394394
}
395395

396396
@Test
397-
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/issues/369")
398-
public void addWithLastInImplicitArray() {
399-
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
400-
"add_field('my.name.$last','patrick')",
401-
"add_field('my.name.$last','nicolas')"
402-
),
403-
i -> {
404-
i.startRecord("1");
405-
i.endRecord();
406-
407-
i.startRecord("2");
408-
i.startEntity("my");
409-
i.literal("name", "max");
410-
i.endEntity();
411-
i.endRecord();
412-
413-
i.startRecord("3");
414-
i.endRecord();
415-
},
416-
o -> {
417-
o.get().startRecord("1");
418-
o.get().endRecord();
419-
420-
o.get().startRecord("2");
421-
o.get().startEntity("my");
422-
o.get().literal("name", "nicolas");
423-
o.get().endEntity();
424-
o.get().endRecord();
425-
426-
o.get().startRecord("3");
427-
o.get().endRecord();
428-
}
397+
public void addWithLastInNonArray() {
398+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected Array or Hash, got String", () ->
399+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
400+
"add_field('my.name.$last','patrick')"
401+
),
402+
i -> {
403+
i.startRecord("1");
404+
i.startEntity("my");
405+
i.literal("name", "max");
406+
i.endEntity();
407+
i.endRecord();
408+
},
409+
o -> {
410+
}
411+
)
429412
);
430413
}
431414

0 commit comments

Comments
 (0)