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 e848eed
Show file tree
Hide file tree
Showing 4 changed files with 809 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
22 changes: 21 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,26 @@ 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 {
if f.Name != refctx.Key.Value.ScalarBox().Unbox().ScalarString() {
return false
}
}
return true
}
for _, f := range fa {
ok := c._ampersandFilter(f, refctx)
Expand Down
19 changes: 19 additions & 0 deletions d2ir/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ 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
*.style.opacity: 0.1
*: {
&label: x
style.opacity: 1
}
`)
assert.Success(t, err)
assertQuery(t, m, 6, 0, nil, "")
assertQuery(t, m, 0, 0, 1, "x.style.opacity")
assertQuery(t, m, 0, 0, 0.1, "y.style.opacity")
},
},
}

runa(t, tca)
Expand Down
Loading

0 comments on commit e848eed

Please sign in to comment.