Skip to content

Commit

Permalink
Support $prepend for inserting & array creation (#92, #93, #127)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Sep 25, 2024
1 parent d0cc26e commit fffb6d9
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 8 deletions.
11 changes: 9 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ void apply(final Array array, final String field, final Value value) {
final ReservedField reservedField = ReservedField.fromString(field);
if (reservedField != null) {
switch (reservedField) {
case $prepend:
array.add(0, value);
break;
case $append:
array.add(value);
break;
Expand Down Expand Up @@ -276,7 +279,7 @@ else if (isReference(field)) {
}
else {
if (!hash.containsField(field)) {
if (ReservedField.$append.name().equals(tail(path)[0])) {
if (ReservedField.$prepend.name().equals(tail(path)[0]) || ReservedField.$append.name().equals(tail(path)[0])) {
hash.put(field, Value.newArray().withPathSet(newValue.getPath()));
}
else {
Expand Down Expand Up @@ -312,7 +315,7 @@ private String[] tail(final String[] fields) {
}

private enum ReservedField {
$append, $first, $last;
$prepend, $append, $first, $last;

private static final Map<String, ReservedField> STRING_TO_ENUM = new HashMap<>();
static {
Expand Down Expand Up @@ -352,6 +355,10 @@ private Value getReferencedValue(final Array array, final String field, final St
referencedValue = Value.newHash().withPathSet(p); // TODO: append non-hash?
array.add(referencedValue);
break;
case $prepend:
referencedValue = Value.newHash().withPathSet(p);
array.add(0, referencedValue);
break;
default:
break;
}
Expand Down
6 changes: 6 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ public void add(final Value value) {
}
}

/* package-private */ void add(final int index, final Value value) {
if (!isNull(value)) {
list.add(index, value);
}
}

public boolean isEmpty() {
return list.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,51 @@ public void addWithAppendInImplicitArray() {
);
}

@Test
public void addWithPrependInImplicitArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"add_field('my.name.$prepend','patrick')",
"add_field('my.name.$prepend','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", "nicolas");
o.get().literal("name", "patrick");
o.get().endEntity();
o.get().endRecord();

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

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

@Test
public void addWithAppendInArray() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down Expand Up @@ -1488,6 +1533,33 @@ public void copyIntoArrayOfHashesImplicitAppend() {
});
}

@Test
public void copyIntoArrayOfHashesImplicitPrepend() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"copy_field('your.name','author[].$prepend.name')",
"remove_field('your')"),
i -> {
i.startRecord("1");
i.startEntity("your");
i.literal("name", "max");
i.endEntity();
i.startEntity("your");
i.literal("name", "mo");
i.endEntity();
i.endRecord();
}, (o, f) -> {
o.get().startRecord("1");
o.get().startEntity("author[]");
o.get().startEntity("1");
o.get().literal("name", "mo");
o.get().endEntity();
o.get().startEntity("2");
o.get().literal("name", "max");
f.apply(2).endEntity();
o.get().endRecord();
});
}

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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
copy_field("test_2", "test[].$prepend")
move_field("test_2", "test[].$prepend")

This file was deleted.

0 comments on commit fffb6d9

Please sign in to comment.