Skip to content

Commit

Permalink
fix double globs in multiple scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Nov 5, 2024
1 parent a814db6 commit 7fc9069
Show file tree
Hide file tree
Showing 8 changed files with 1,575 additions and 537 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

- Render: fixes edge case of a 3d shape with outside label being cut off [#2132](https://github.com/terrastruct/d2/pull/2132)
- Composition: labels for boards set with shorthand `x: y` was not applied [#2182](https://github.com/terrastruct/d2/pull/2182)
- Globs: double globs (`**`) were erroring when used with multiple scenario boards [#2195](https://github.com/terrastruct/d2/pull/2195)
15 changes: 15 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,21 @@ func (mk *Key) HasTripleGlob() bool {
return false
}

func (mk *Key) HasMultiGlob() bool {
if mk.Key.HasMultiGlob() {
return true
}
for _, e := range mk.Edges {
if e.Src.HasMultiGlob() || e.Dst.HasMultiGlob() {
return true
}
}
if mk.EdgeKey.HasMultiGlob() {
return true
}
return false
}

func (mk *Key) SupportsGlobFilters() bool {
if mk.Key.HasGlob() && len(mk.Edges) == 0 {
return true
Expand Down
34 changes: 34 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3576,6 +3576,40 @@ steps: {
`, `d2/testdata/d2compiler/TestCompile2/boards/errs/duplicate_board.d2:9:2: board name one already used by another board`)
},
},
{
name: "style-nested-boards",
run: func(t *testing.T) {
g, _ := assertCompile(t, `**.style.stroke: black
scenarios: {
a: {
x
}
b: {
x
}
}
steps: {
c: {
x
}
d: {
x
}
}
layers: {
e: {
x
}
}
`, ``)
assert.Equal(t, "black", g.Scenarios[0].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Scenarios[1].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Steps[0].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Steps[1].Objects[0].Style.Stroke.Value)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Layers[0].Objects[0].Style.Stroke)
},
},
}

for _, tc := range tca {
Expand Down
8 changes: 6 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ func (g *globContext) copy() *globContext {
func (g *globContext) copyApplied(from *globContext) {
g.appliedFields = make(map[string]struct{})
for k, v := range from.appliedFields {
// b, _ := json.MarshalIndent(k, "", " ")
// println("\033[1;31m--- from glob has key:", string(b), "\033[m")
g.appliedFields[k] = v
}
g.appliedEdges = make(map[string]struct{})
Expand Down Expand Up @@ -456,7 +458,7 @@ func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
return false
}
var ks string
if gctx.refctx.Key.HasTripleGlob() {
if gctx.refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(dst)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(dst)))
Expand All @@ -483,12 +485,13 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
}
} else if NodeBoardKind(dst) == BoardScenario {
// println("\033[1;31m--- compiling scenario globs:", len(c.globContextStack), "\033[m")
for _, g := range previousGlobs {
g2 := g.prefixed(dst)
// We don't want globs applied in a given scenario to affect future boards
// Copying the applied fields and edges keeps the applications scoped to this board
// Note that this is different from steps, where applications carry over
if !g.refctx.Key.HasTripleGlob() {
if !g.refctx.Key.HasMultiGlob() {
// Triple globs already apply independently to each board
g2.copyApplied(g)
}
Expand All @@ -504,6 +507,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
c.globContextStack = append(c.globContextStack, globs)
defer func() {
// println("\033[1;31m--- DEBUG:", "popping glob stack", "\033[m")
dst.globs = c.globContexts()
c.globContextStack = c.globContextStack[:len(c.globContextStack)-1]
}()
Expand Down
11 changes: 7 additions & 4 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
filter := func(f *Field, passthrough bool) bool {
if gctx != nil {
var ks string
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(f)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(f)))
Expand All @@ -803,6 +803,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}
}
if !passthrough {
// println("\033[1;31m--- DEBUG:", len(c.globContextStack), "\033[m")
gctx.appliedFields[ks] = struct{}{}
}
}
Expand All @@ -820,6 +821,8 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
if ok && us.Pattern != nil {
fa2, ok := m.multiGlob(us.Pattern)
if ok {
// b, _ := json.MarshalIndent(kp.Path, "", " ")
// println("\033[1;31m--- DEBUG:", string(b), "\033[m")
if i == len(kp.Path)-1 {
faAppend(fa2...)
} else {
Expand Down Expand Up @@ -937,7 +940,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}
for _, grefctx := range c.globRefContextStack {
var ks string
if grefctx.Key.HasTripleGlob() {
if grefctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(f)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(f)))
Expand Down Expand Up @@ -1134,7 +1137,7 @@ func (m *Map) getEdges(eid *EdgeID, refctx *RefContext, gctx *globContext, ea *[
for _, e := range ea2 {
if gctx != nil {
var ks string
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(e)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e)))
Expand Down Expand Up @@ -1352,7 +1355,7 @@ func (m *Map) createEdge2(eid *EdgeID, refctx *RefContext, gctx *globContext, c
e2 := e.Copy(e.Parent()).(*Edge)
e2.ID = e2.ID.Copy()
e2.ID.Index = nil
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(e2)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e2)))
Expand Down
Loading

0 comments on commit 7fc9069

Please sign in to comment.