Skip to content

Commit

Permalink
Support implicit array creation on $append (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 24, 2024
1 parent 19bbe2c commit 0de7fb9
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 12 deletions.
13 changes: 11 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,18 @@ else if (isReference(field)) {
}
else {
if (!hash.containsField(field)) {
hash.put(field, Value.newHash().withPathSet(newValue.getPath()));
if (ReservedField.$append.name().equals(tail(path)[0])) {
hash.put(field, Value.newArray().withPathSet(newValue.getPath()));
}
else {
hash.put(field, Value.newHash().withPathSet(newValue.getPath()));
}
}
final Value value = hash.get(field);
if (value.isString()) {
hash.put(field, Value.newArray(a -> a.add(value)));
}
insertInto(hash.get(field), mode, newValue, field, tail(path));
insertInto(value, mode, newValue, field, tail(path));
}

return new Value(hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,51 @@ public void addWithAppendInNewArray() {
);
}

@Test
public void addWithAppendInImplicitArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"add_field('my.name.$append','patrick')",
"add_field('my.name.$append','nicolas')"
),
i -> {
i.startRecord("1");
i.endRecord();

i.startRecord("2");
i.startEntity("my");
i.literal("name", "max");
i.endEntity();
i.endRecord();

i.startRecord("3");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().startEntity("my");
o.get().literal("name", "patrick");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();

o.get().startRecord("2");
o.get().startEntity("my");
o.get().literal("name", "max");
o.get().literal("name", "patrick");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();

o.get().startRecord("3");
o.get().startEntity("my");
o.get().literal("name", "patrick");
o.get().literal("name", "nicolas");
o.get().endEntity();
o.get().endRecord();
}
);
}

@Test
public void addWithAppendInArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 0de7fb9

Please sign in to comment.