Skip to content

Commit

Permalink
Merge pull request #2009 from alixander/exists-filter
Browse files Browse the repository at this point in the history
allow pattern globs in values
  • Loading branch information
alixander authored Jul 18, 2024
2 parents 565168e + 13e6a60 commit e5bdb5f
Show file tree
Hide file tree
Showing 7 changed files with 802 additions and 9 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#### Features 🚀

- Glob inverse filters are implemented (e.g. `*: {!&shape: circle; style.fill: red}` to turn all non-circles red) [#2008](https://github.com/terrastruct/d2/pull/2008)
- Globs can be used in glob filter values, including checking for existence (e.g. `*: {&link: *; style.fill: red}` to turn all objects with a link red) [#2009](https://github.com/terrastruct/d2/pull/2009)

#### Improvements 🧹

Expand Down
34 changes: 34 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,40 @@ a -> b
assert.Equal(t, "red", g.Edges[1].Attributes.Style.Stroke.Value)
},
},
{
name: "exists-filter",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
*: {
&link: *
style.underline: true
}
x
y.link: https://google.com
`, ``)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[0].Attributes.Style.Underline)
assert.Equal(t, "true", g.Objects[1].Attributes.Style.Underline.Value)
},
},
{
name: "glob-filter",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
*: {
&link: *google*
style.underline: true
}
x
y.link: https://google.com
z.link: https://yahoo.com
`, ``)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[0].Attributes.Style.Underline)
assert.Equal(t, "true", g.Objects[1].Attributes.Style.Underline.Value)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Underline)
},
},
}

for _, tc := range tca {
Expand Down
10 changes: 8 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,14 @@ func (c *compiler) _ampersandFilter(f *Field, refctx *RefContext) bool {
return false
}

if refctx.Key.Value.ScalarBox().Unbox().ScalarString() != f.Primary_.Value.ScalarString() {
return false
us, ok := refctx.Key.Value.ScalarBox().Unbox().(*d2ast.UnquotedString)

if ok && us.Pattern != nil {
return matchPattern(f.Primary_.Value.ScalarString(), us.Pattern)
} else {
if refctx.Key.Value.ScalarBox().Unbox().ScalarString() != f.Primary_.Value.ScalarString() {
return false
}
}

return true
Expand Down
16 changes: 9 additions & 7 deletions d2parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,16 +1163,18 @@ func (p *parser) parseUnquotedString(inKey bool) (s *d2ast.UnquotedString) {
sb.WriteRune(r)
rawb.WriteRune(r)
r = r2
case '*':
if sb.Len() == 0 {
s.Pattern = append(s.Pattern, "*")
} else {
s.Pattern = append(s.Pattern, sb.String()[lastPatternIndex:], "*")
}
lastPatternIndex = len(sb.String()) + 1
}
}

if r == '*' {
if sb.Len() == 0 {
s.Pattern = append(s.Pattern, "*")
} else {
s.Pattern = append(s.Pattern, sb.String()[lastPatternIndex:], "*")
}
lastPatternIndex = len(sb.String()) + 1
}

p.commit()

if !unicode.IsSpace(r) {
Expand Down
Loading

0 comments on commit e5bdb5f

Please sign in to comment.