Skip to content

Commit

Permalink
Merge pull request #2299 from alixander/container-glob-filter
Browse files Browse the repository at this point in the history
globs: add filters
  • Loading branch information
alixander authored Jan 21, 2025
2 parents 7d4b42c + ce5df70 commit dc0e5fa
Show file tree
Hide file tree
Showing 5 changed files with 850 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Markdown: Github-flavored tables work in `md` blocks [#2221](https://github.com/terrastruct/d2/pull/2221)
- `d2 fmt` now supports a `--check` flag [#2253](https://github.com/terrastruct/d2/pull/2253)
- CLI: PNG output to stdout is supported using `--stdout-format png -` [#2291](https://github.com/terrastruct/d2/pull/2291)
- Globs: `&connected` and `&leaf` filters are implemented [#2299](https://github.com/terrastruct/d2/pull/2299)

#### Improvements 🧹

Expand Down
37 changes: 37 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5197,6 +5197,43 @@ y.link: https://google.com
assert.Equal(t, "true", g.Objects[1].Attributes.Style.Underline.Value)
},
},
{
name: "leaf-filter",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
**: {
&leaf: false
style.fill: red
}
a.b.c
`, ``)
assert.Equal(t, "a", g.Objects[0].ID)
assert.Equal(t, "red", g.Objects[0].Attributes.Style.Fill.Value)
assert.Equal(t, "b", g.Objects[1].ID)
assert.Equal(t, "red", g.Objects[1].Attributes.Style.Fill.Value)
assert.Equal(t, "c", g.Objects[2].ID)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Fill)
},
},
{
name: "connected-filter",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
*: {
&connected: true
style.fill: red
}
a -> b
c
`, ``)
assert.Equal(t, "a", g.Objects[0].ID)
assert.Equal(t, "red", g.Objects[0].Attributes.Style.Fill.Value)
assert.Equal(t, "b", g.Objects[1].ID)
assert.Equal(t, "red", g.Objects[1].Attributes.Style.Fill.Value)
assert.Equal(t, "c", g.Objects[2].ID)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Fill)
},
},
{
name: "glob-filter",
run: func(t *testing.T) {
Expand Down
27 changes: 27 additions & 0 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,33 @@ func (c *compiler) ampersandFilter(refctx *RefContext) bool {
},
}
return c._ampersandFilter(f, refctx)
case "leaf":
raw := refctx.Key.Value.ScalarBox().Unbox().ScalarString()
boolVal, err := strconv.ParseBool(raw)
if err != nil {
c.errorf(refctx.Key, `&leaf must be "true" or "false", got %q`, raw)
return false
}

f := refctx.ScopeMap.Parent().(*Field)
isLeaf := f.Map() == nil || !f.Map().IsContainer()
return isLeaf == boolVal
case "connected":
raw := refctx.Key.Value.ScalarBox().Unbox().ScalarString()
boolVal, err := strconv.ParseBool(raw)
if err != nil {
c.errorf(refctx.Key, `&connected must be "true" or "false", got %q`, raw)
return false
}
f := refctx.ScopeMap.Parent().(*Field)
isConnected := false
for _, r := range f.References {
if r.InEdge() {
isConnected = true
break
}
}
return isConnected == boolVal
case "label":
f := &Field{}
n := refctx.ScopeMap.Parent()
Expand Down
Loading

0 comments on commit dc0e5fa

Please sign in to comment.