Skip to content

Commit

Permalink
d2ir: Add exception for &label filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Aug 17, 2023
1 parent aa2be81 commit 317964a
Show file tree
Hide file tree
Showing 4 changed files with 1,300 additions and 1 deletion.
4 changes: 4 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,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] == "*"
}
Expand Down
26 changes: 25 additions & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
}
}

for _, n := range ast.Nodes {
switch {
case n.MapKey != nil:
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions d2ir/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 317964a

Please sign in to comment.