d2ir: Add label filter exception for edges too

This commit is contained in:
Anmol Sethi 2023-08-17 14:53:51 -07:00
parent 1800ae3448
commit 2efd87f059
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
3 changed files with 4679 additions and 3 deletions

View file

@ -561,7 +561,21 @@ func (c *compiler) ampersandFilter(refctx *RefContext) bool {
kp := refctx.Key.Key.Copy()
kp.Path = kp.Path[:len(kp.Path)-1]
if len(kp.Path) == 0 {
fa = append(fa, ParentField(refctx.ScopeMap))
n := refctx.ScopeMap.Parent()
switch n := n.(type) {
case *Field:
fa = append(fa, n)
case *Edge:
if n.Primary_ == nil {
if refctx.Key.Value.ScalarBox().Unbox().ScalarString() == "" {
return true
}
return false
}
if n.Primary_.Value.ScalarString() != refctx.Key.Value.ScalarBox().Unbox().ScalarString() {
return false
}
}
} else {
fa, err = refctx.ScopeMap.EnsureField(kp, refctx, false, c)
if err != nil {

View file

@ -97,12 +97,13 @@ x -> y
},
},
{
name: "id-filter",
name: "label-filter",
run: func(t testing.TB) {
m, err := compile(t, `
x
y
p: p
a -> z: delta
*.style.opacity: 0.1
*: {
@ -113,12 +114,19 @@ p: p
&label: p
style.opacity: 0.5
}
(* -> *)[*]: {
&label: delta
target-arrowhead.shape: diamond
}
`)
assert.Success(t, err)
assertQuery(t, m, 9, 0, nil, "")
assertQuery(t, m, 17, 1, 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")
assertQuery(t, m, 0, 0, 0.1, "a.style.opacity")
assertQuery(t, m, 0, 0, 0.1, "z.style.opacity")
assertQuery(t, m, 0, 0, "diamond", "(a -> z).target-arrowhead.shape")
},
},
}

4654
testdata/d2ir/TestCompile/filters/label-filter.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff