Skip to content

Commit

Permalink
Merge pull request #2247 from alixander/glob-layer
Browse files Browse the repository at this point in the history
d2compiler: fix globs with board imports
  • Loading branch information
alixander authored Dec 13, 2024
2 parents c88354c + 3484619 commit d826be0
Show file tree
Hide file tree
Showing 16 changed files with 11,481 additions and 35,846 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

- Imports: fixes using substitutions in `icon` values [#2207](https://github.com/terrastruct/d2/pull/2207)
- Markdown: fixes ampersands in URLs in markdown [#2219](https://github.com/terrastruct/d2/pull/2219)
- Globs: fixes edge case where globs with imported boards would create empty boards [#2247](https://github.com/terrastruct/d2/pull/2247)
24 changes: 24 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,30 @@ x -> y: {
}
},
},
{
name: "glob-connection-steps",

text: `*.style.stroke: black
layers: {
ok: @ok
}
`,
files: map[string]string{
"ok.d2": `
steps: {
1: {
step1
}
}
`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
assert.Equal(t, 0, len(g.Steps))
assert.Equal(t, 1, len(g.Layers))
assert.Equal(t, 1, len(g.Layers[0].Steps))
},
},
{
name: "import_url_link",

Expand Down
36 changes: 17 additions & 19 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,22 +442,6 @@ func (g *globContext) copyApplied(from *globContext) {
}
}

func (g *globContext) prefixed(dst *Map) *globContext {
g2 := g.copy()
prefix := d2ast.MakeKeyPathString(RelIDA(g2.refctx.ScopeMap, dst))
g2.refctx.Key = g2.refctx.Key.Copy()
if g2.refctx.Key.Key != nil {
prefix.Path = append(prefix.Path, g2.refctx.Key.Key.Path...)
}
if len(prefix.Path) > 0 {
g2.refctx.Key.Key = prefix
}
if !g2.refctx.Key.HasTripleGlob() && g2.refctx.Key.EdgeKey != nil {
prefix.Path = append(prefix.Path, g2.refctx.Key.EdgeKey.Path...)
}
return g2
}

func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
for _, n := range ast.Nodes {
switch {
Expand Down Expand Up @@ -504,12 +488,24 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
if NodeBoardKind(dst) == BoardLayer && !dst.Root() {
for _, g := range previousGlobs {
if g.refctx.Key.HasTripleGlob() {
globs = append(globs, g.prefixed(dst))
gctx2 := g.copy()
gctx2.refctx.ScopeMap = dst
globs = append(globs, gctx2)
}
}
} else if NodeBoardKind(dst) == BoardScenario {
for _, g := range previousGlobs {
g2 := g.prefixed(dst)
gctx2 := g.copy()
gctx2.refctx.ScopeMap = dst
if !g.refctx.Key.HasMultiGlob() {
// Triple globs already apply independently to each board
gctx2.copyApplied(g)
}
globs = append(globs, gctx2)
}
for _, g := range previousGlobs {
g2 := g.copy()
g2.refctx.ScopeMap = 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
Expand All @@ -521,7 +517,9 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
} else if NodeBoardKind(dst) == BoardStep {
for _, g := range previousGlobs {
globs = append(globs, g.prefixed(dst))
gctx2 := g.copy()
gctx2.refctx.ScopeMap = dst
globs = append(globs, gctx2)
}
} else {
globs = append(globs, previousGlobs...)
Expand Down
Loading

0 comments on commit d826be0

Please sign in to comment.