Skip to content

Commit

Permalink
Merge pull request #1998 from alixander/fix-import-nested
Browse files Browse the repository at this point in the history
d2compiler: fix importing nested boards
  • Loading branch information
alixander authored Jul 13, 2024
2 parents 108cf56 + 2c98730 commit 7ee1715
Show file tree
Hide file tree
Showing 4 changed files with 760 additions and 5 deletions.
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

0 comments on commit 7ee1715

Please sign in to comment.