From 8455a7da8dfbddc2f5f0ab198b9aa9088a1477ed Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Wed, 16 Aug 2023 02:21:33 -0700 Subject: [PATCH] d2ir: Fix another lazy glob bug found by Alex --- d2ir/d2ir.go | 15 +- d2ir/pattern_test.go | 15 +- .../patterns/alixander-review/4.exp.json | 701 ++++++++++++++++++ 3 files changed, 724 insertions(+), 7 deletions(-) create mode 100644 testdata/d2ir/TestCompile/patterns/alixander-review/4.exp.json diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index 5746eaf23..f5319511c 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -710,20 +710,23 @@ func (m *Map) EnsureField(kp *d2ast.KeyPath, refctx *RefContext, create bool, c func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create bool, gctx *globContext, c *compiler, fa *[]*Field) error { filter := func(f *Field, passthrough bool) bool { + ks := d2format.Format(d2ast.MakeKeyPath(BoardIDA(f))) if gctx != nil { // For globs with edges, we only ignore duplicate fields if the glob is not at the terminal of the keypath, the glob is on the common key or the glob is on the edge key. if !kp.HasGlob() { + if !passthrough { + gctx.appliedFields[ks] = struct{}{} + } return true } lastEl := kp.Path[len(kp.Path)-1] if len(refctx.Key.Edges) == 0 || lastEl.UnquotedString == nil || len(lastEl.UnquotedString.Pattern) == 0 || kp == refctx.Key.Key || kp == refctx.Key.EdgeKey { - ks := d2format.Format(d2ast.MakeKeyPath(BoardIDA(f))) if _, ok := gctx.appliedFields[ks]; ok { return false } - if !passthrough { - gctx.appliedFields[ks] = struct{}{} - } + } + if !passthrough { + gctx.appliedFields[ks] = struct{}{} } } return true @@ -745,7 +748,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b } else { for _, f := range fa2 { if !filter(f, true) { - return nil + continue } if f.Map() == nil { f.Composite = &Map{ @@ -766,7 +769,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b faAppend(f) } else { if !filter(f, true) { - return nil + continue } if f.Map() == nil { f.Composite = &Map{ diff --git a/d2ir/pattern_test.go b/d2ir/pattern_test.go index bd32a5355..e5064752d 100644 --- a/d2ir/pattern_test.go +++ b/d2ir/pattern_test.go @@ -432,7 +432,6 @@ layers: { } `) assert.Success(t, err) - t.Log(m) assertQuery(t, m, 14, 0, nil, "") }, }, @@ -469,6 +468,20 @@ layers: { assertQuery(t, m, 8, 2, nil, "") }, }, + { + name: "alixander-review/4", + run: func(t testing.TB) { + m, err := compile(t, ` +**.child + +a +b +c +`) + assert.Success(t, err) + assertQuery(t, m, 6, 0, nil, "") + }, + }, } runa(t, tca) diff --git a/testdata/d2ir/TestCompile/patterns/alixander-review/4.exp.json b/testdata/d2ir/TestCompile/patterns/alixander-review/4.exp.json new file mode 100644 index 000000000..c605e295d --- /dev/null +++ b/testdata/d2ir/TestCompile/patterns/alixander-review/4.exp.json @@ -0,0 +1,701 @@ +{ + "fields": [ + { + "name": "a", + "composite": { + "fields": [ + { + "name": "child", + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + }, + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + }, + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,3:0:11-3:1:12", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + }, + { + "name": "b", + "composite": { + "fields": [ + { + "name": "child", + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + }, + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,4:0:13-4:1:14", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + }, + { + "name": "c", + "composite": { + "fields": [ + { + "name": "child", + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:8:9", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:0:1-1:2:3", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + }, + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,1:3:4-1:8:9", + "value": [ + { + "string": "child", + "raw_string": "child" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "key": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/alixander-review/4.d2,5:0:15-5:1:16", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + } + ] + } + ], + "edges": null +}