Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2compiler: fix importing nested boards #1998

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
- Globs to null connections work [#1965](https://github.com/terrastruct/d2/pull/1965)
- Edge globs setting styles inherit correctly in child boards [#1967](https://github.com/terrastruct/d2/pull/1967)
- Board links imported with spread imports work [#1972](https://github.com/terrastruct/d2/pull/1972)
- Fix importing a file with nested boards [#1998](https://github.com/terrastruct/d2/pull/1998)
12 changes: 7 additions & 5 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph {
}

func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName string) {
layers := ir.GetField(fieldName)
if layers.Map() == nil {
boards := ir.GetField(fieldName)
if boards.Map() == nil {
return
}
for _, f := range layers.Map().Fields {
for _, f := range boards.Map().Fields {
if f.Map() == nil {
continue
}
Expand All @@ -123,7 +123,9 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName
g2 := d2graph.NewGraph()
g2.Parent = g
g2.AST = f.Map().AST().(*d2ast.Map)
g2.BaseAST = findFieldAST(g.BaseAST, f)
if g.BaseAST != nil {
g2.BaseAST = findFieldAST(g.BaseAST, f)
}
c.compileBoard(g2, f.Map())
g2.Name = f.Name
switch fieldName {
Expand All @@ -139,7 +141,7 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName

func findFieldAST(ast *d2ast.Map, f *d2ir.Field) *d2ast.Map {
path := []string{}
var curr *d2ir.Field = f
curr := f
for {
path = append([]string{curr.Name}, path...)
boardKind := d2ir.NodeBoardKind(curr)
Expand Down
22 changes: 22 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,28 @@ layers: {
b: {
d
}
}`,
},
},
{
name: "import-nested-layers",
text: `k

layers: {
x: {...@x}
}`,
files: map[string]string{
"x.d2": `a
layers: {
b: {
d

layers: {
c: {
c
}
}
}
}`,
},
},
Expand Down
Loading
Loading