d2ir: Fix another lazy glob bug found by Alex

This commit is contained in:
Anmol Sethi 2023-08-16 02:21:33 -07:00
parent a24716d1f1
commit 8455a7da8d
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
3 changed files with 724 additions and 7 deletions

View file

@ -710,22 +710,25 @@ 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{}{}
}
}
}
return true
}
faAppend := func(fa2 ...*Field) {
@ -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{

View file

@ -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)

View file

@ -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
}