Skip to content

Commit

Permalink
Merge pull request #2021 from alixander/double-glob-scenario
Browse files Browse the repository at this point in the history
d2ir: fix glob applications to multiple scenarios
  • Loading branch information
alixander authored Jul 27, 2024
2 parents 4ff04cd + e0d3938 commit bca14f7
Show file tree
Hide file tree
Showing 8 changed files with 2,612 additions and 2 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
- Fix importing a file with nested boards [#1998](https://github.com/terrastruct/d2/pull/1998)
- Fix importing a file with underscores in links [#1999](https://github.com/terrastruct/d2/pull/1999)
- Replace a panic with an error message resulting from invalid `link` usage [#2011](https://github.com/terrastruct/d2/pull/2011)
- Fix globs not applying to scenarios on keys that were applied in earlier scenarios [#2021](https://github.com/terrastruct/d2/pull/2021)
44 changes: 44 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4699,6 +4699,50 @@ z.link: https://yahoo.com
assert.Equal(t, (*d2graph.Scalar)(nil), g.Objects[2].Attributes.Style.Underline)
},
},
{
name: "reapply-scenario",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
*.b*.shape: circle
x: {
b
}
scenarios: {
k: {
x: {
b
}
}
}
`, ``)
assert.Equal(t, "circle", g.Objects[1].Attributes.Shape.Value)
assert.Equal(t, "circle", g.Scenarios[0].Objects[1].Attributes.Shape.Value)
},
},
{
name: "second-scenario",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
*.b*.shape: circle
scenarios: {
k: {
x: {
b
}
}
z: {
x: {
b
}
}
}
`, ``)
assert.Equal(t, "circle", g.Scenarios[0].Objects[1].Attributes.Shape.Value)
assert.Equal(t, "circle", g.Scenarios[1].Objects[1].Attributes.Shape.Value)
},
},
}

for _, tc := range tca {
Expand Down
26 changes: 24 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@ func (g *globContext) copy() *globContext {
return &g2
}

func (g *globContext) copyApplied(from *globContext) {
g.appliedFields = make(map[string]struct{})
for k, v := range from.appliedFields {
g.appliedFields[k] = v
}
g.appliedEdges = make(map[string]struct{})
for k, v := range from.appliedEdges {
g.appliedEdges[k] = v
}
}

func (g *globContext) prefixed(dst *Map) *globContext {
g2 := g.copy()
prefix := d2ast.MakeKeyPath(RelIDA(g2.refctx.ScopeMap, dst))
Expand Down Expand Up @@ -447,8 +458,19 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
globs = append(globs, g.prefixed(dst))
}
}
} else if NodeBoardKind(dst) != "" {
// Make all globs relative to the scenario or step.
} else if NodeBoardKind(dst) == BoardScenario {
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() {
// Triple globs already apply independently to each board
g2.copyApplied(g)
}
globs = append(globs, g2)
}
} else if NodeBoardKind(dst) == BoardStep {
for _, g := range previousGlobs {
globs = append(globs, g.prefixed(dst))
}
Expand Down
Loading

0 comments on commit bca14f7

Please sign in to comment.