d2ir: Add exception for &label filter

This commit is contained in:
Anmol Sethi 2023-08-17 14:42:52 -07:00
parent 976ec17476
commit 1800ae3448
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
4 changed files with 1300 additions and 1 deletions

View file

@ -804,6 +804,10 @@ func (kp *KeyPath) Copy() *KeyPath {
return &kp2
}
func (kp *KeyPath) Last() *StringBox {
return kp.Path[len(kp.Path)-1]
}
func IsDoubleGlob(pattern []string) bool {
return len(pattern) == 3 && pattern[0] == "*" && pattern[1] == "" && pattern[2] == "*"
}

View file

@ -414,6 +414,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
}
}
for _, n := range ast.Nodes {
switch {
case n.MapKey != nil:
@ -554,7 +555,30 @@ func (c *compiler) ampersandFilter(refctx *RefContext) bool {
return false
}
if len(fa) == 0 {
return false
if refctx.Key.Key.Last().ScalarString() != "label" {
return false
}
kp := refctx.Key.Key.Copy()
kp.Path = kp.Path[:len(kp.Path)-1]
if len(kp.Path) == 0 {
fa = append(fa, ParentField(refctx.ScopeMap))
} else {
fa, err = refctx.ScopeMap.EnsureField(kp, refctx, false, c)
if err != nil {
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
return false
}
}
for _, f := range fa {
label := f.Name
if f.Primary_ != nil {
label = f.Primary_.Value.ScalarString()
}
if label != refctx.Key.Value.ScalarBox().Unbox().ScalarString() {
return false
}
}
return true
}
for _, f := range fa {
ok := c._ampersandFilter(f, refctx)

View file

@ -96,6 +96,31 @@ x -> y
assertQuery(t, m, 0, 0, nil, "(x -> y)[1]")
},
},
{
name: "id-filter",
run: func(t testing.TB) {
m, err := compile(t, `
x
y
p: p
*.style.opacity: 0.1
*: {
&label: x
style.opacity: 1
}
*: {
&label: p
style.opacity: 0.5
}
`)
assert.Success(t, err)
assertQuery(t, m, 9, 0, nil, "")
assertQuery(t, m, 0, 0, 1, "x.style.opacity")
assertQuery(t, m, 0, 0, 0.1, "y.style.opacity")
assertQuery(t, m, 0, 0, 0.5, "p.style.opacity")
},
},
}
runa(t, tca)

1246
testdata/d2ir/TestCompile/filters/id-filter.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff