Skip to content

Commit f36a33f

Browse files
authored
Merge pull request #674 from witheve/fix-673
Make sure records in parenthesis are treated the same
2 parents 44b086e + b085c16 commit f36a33f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/runtime/parser.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,30 @@ export class Parser extends chev.Parser {
882882
self.OR2([
883883
{ALT: () => {
884884
result = self.SUBRULE(self.infix);
885+
// if the result is a parenthesis, we have to make sure that if there are sub-records
886+
// inside that they get eve-auto-index set on them and they also have the parent transfered
887+
// down to them. If we don't do this, we'll end up with children that are shared between
888+
// the parents instead of one child per parent.
889+
if(result.type === "parenthesis") {
890+
for(let item of result.items) {
891+
// this is a bit sad, but by the time we see the parenthesis, the records have been replaced
892+
// with their variables. Those variables are created from the record object though, so we can
893+
// check the from of the variable for a reference to the record.
894+
if(item.type === "variable" && item.from[0] && item.from[0].type === "record") {
895+
let record = item.from[0];
896+
// if we have a parent, we need to make sure it ends up part of our extraProjection set
897+
if(parent && !item.extraProjection) {
898+
record.extraProjection = [parent];
899+
} else if(parent) {
900+
record.extraProjection.push(parent);
901+
}
902+
// Lastly we need to add the eve-auto-index attribute to make sure this is consistent with the case
903+
// where we leave the parenthesis off and just put records one after another.
904+
record.attributes.push(makeNode("attribute", {attribute: "eve-auto-index", value: makeNode("constant", {value: autoIndex, from: [record]}), from: [record]}));
905+
autoIndex++;
906+
}
907+
}
908+
}
885909
}},
886910
{ALT: () => {
887911
result = self.SUBRULE(self.record, [noVar, blockKey, action, parent]);

test/join.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,63 @@ test("search with attribute having multiple values in parenthesis with a functio
191191
assert.end();
192192
})
193193

194+
test.only("sub-records in a parenthesis pick up their parent as part of their identity", (assert) => {
195+
let expected = {
196+
insert: [
197+
["a", "tag", "person"],
198+
["a", "name", "chris"],
199+
["a", "age", 20],
200+
201+
["b", "tag", "person"],
202+
["b", "name", "joe"],
203+
["b", "age", 20],
204+
205+
["c", "tag", "div"],
206+
["c", "p", "a"],
207+
["c", "children", "d"],
208+
["c", "children", "e"],
209+
210+
["d", "tag", "div"],
211+
["d", "text", "age"],
212+
["d", "eve-auto-index", 1],
213+
214+
["e", "tag", "div"],
215+
["e", "text", 20],
216+
["e", "eve-auto-index", 2],
217+
218+
["f", "tag", "div"],
219+
["f", "p", "b"],
220+
["f", "children", "g"],
221+
["f", "children", "h"],
222+
223+
["g", "tag", "div"],
224+
["g", "text", "age"],
225+
["g", "eve-auto-index", 1],
226+
227+
["h", "tag", "div"],
228+
["h", "text", 20],
229+
["h", "eve-auto-index", 2],
230+
],
231+
remove: []
232+
};
233+
evaluate(assert, expected, `
234+
people
235+
~~~
236+
commit
237+
[#person name: "chris" age: 20]
238+
[#person name: "joe" age: 20]
239+
~~~
240+
241+
~~~
242+
search
243+
p = [#person name age]
244+
commit
245+
[#div p children: ([#div text: "age"], [#div text: age])]
246+
~~~
247+
`);
248+
assert.end();
249+
})
250+
194251
test("create a record with numeric attributes", (assert) => {
195252
let expected = {
196253
insert: [

0 commit comments

Comments
 (0)