Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Don't restrict bind scope to the bound var (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Dec 1, 2021
1 parent 9a6c9c0 commit eba3159
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,11 @@ else if (sub instanceof Unless) {
private void processBind(final Do theDo, final EList<String> params) {
if (theDo.getName().equals("list")) { // TODO impl multiple binds via FixBind enum
final Map<String, String> options = options(theDo.getOptions());
final Record fullRecord = record.shallowClone();

record.findList(options.get("path"), a -> a.forEach(value -> {
// for each value, bind the current record/scope/context to the given var name:
record = new Record();
record.put(options.get("var"), value);

processSubexpressions(theDo.getElements());
record.remove(options.get("var"));

// and remember the things we added while bound (this probably needs some tweaking):
fullRecord.addAll(record);
}));

record = fullRecord;
}
else {
LOG.warn("Unprocessed bind: {}", theDo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ public void doList() {
});
}

@Test
public void doListFullRecordInScope() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"do list('path': 'name', 'var': 'n')",
" if any_equal('type','book')",
" paste('title','~Book:','n')",
" else",
" paste('title','~Journal:','n')",
" end",
"end",
"retain('title')"),
i -> {
i.startRecord("1");
i.literal("type", "book");
i.literal("name", "A book");
i.endRecord();
i.startRecord("2");
i.literal("type", "journal");
i.literal("name", "A journal");
i.endRecord();
}, o -> {
o.get().startRecord("1");
o.get().literal("title", "Book: A book");
o.get().endRecord();
o.get().startRecord("2");
o.get().literal("title", "Journal: A journal");
o.get().endRecord();
});
}

@Test
public void doListPathWithDots() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Expand Down Expand Up @@ -99,8 +129,8 @@ public void doListPathWithDots() {
@Test
public void doListWithAppendAndLast() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"set_array('author[]')",
"do list('path': 'creator', 'var': 'c')",
" set_array('author[]')",
" copy_field('c.name', 'author[].$append.name')",
" add_field('author[].$last.type', 'Default')",
"end",
Expand Down

0 comments on commit eba3159

Please sign in to comment.