This commit is contained in:
Alexander Wang 2023-03-01 11:27:18 -08:00
parent 8e074070d8
commit 5c96ffe6a1
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 1147 additions and 10 deletions

View file

@ -56,7 +56,7 @@ func compileIR(ast *d2ast.Map, m *d2ir.Map) (*d2graph.Graph, error) {
if len(c.err.Errors) > 0 { if len(c.err.Errors) > 0 {
return nil, c.err return nil, c.err
} }
c.compileBoardLink(g, m) c.compileBoardLink(g)
if len(c.err.Errors) > 0 { if len(c.err.Errors) > 0 {
return nil, c.err return nil, c.err
} }
@ -722,7 +722,7 @@ func (c *compiler) validateNear(g *d2graph.Graph) {
} }
} }
func (c *compiler) compileBoardLink(g *d2graph.Graph, ir *d2ir.Map) { func (c *compiler) compileBoardLink(g *d2graph.Graph) {
for _, obj := range g.Objects { for _, obj := range g.Objects {
if obj.Attributes.Link == nil { if obj.Attributes.Link == nil {
continue continue
@ -735,24 +735,34 @@ func (c *compiler) compileBoardLink(g *d2graph.Graph, ir *d2ir.Map) {
} }
switch linkKey.Path[0].Unbox().ScalarString() { switch linkKey.Path[0].Unbox().ScalarString() {
// TODO underscore // Starting a link with one of the following means it is targeting a board
case "layers", "scenarios", "steps": case "layers", "scenarios", "steps", "_":
default: default:
continue continue
} }
obj.LinkedBoard = c.findBoard(g, ir, linkKey.IDA()) obj.LinkedBoard = c.findBoard(g, linkKey.IDA())
if obj.LinkedBoard == nil { if obj.LinkedBoard == nil {
c.errorf(obj.Attributes.Link.MapKey, "link key %#v to board not found", obj.Attributes.Link.Value) c.errorf(obj.Attributes.Link.MapKey, "link key %#v to board not found", obj.Attributes.Link.Value)
continue continue
} }
} }
for _, b := range append(append(g.Layers, g.Scenarios...), g.Steps...) {
c.compileBoardLink(b)
}
} }
func (c *compiler) findBoard(g *d2graph.Graph, ir *d2ir.Map, ida []string) *d2graph.Graph { func (c *compiler) findBoard(g *d2graph.Graph, ida []string) *d2graph.Graph {
var currType string var currType string
for _, p := range ida { for _, p := range ida {
if g == nil {
return nil
}
if p == "_" {
g = g.Parent
continue
}
switch p { switch p {
case "layers", "scenarios", "steps": case "layers", "scenarios", "steps":
currType = p currType = p
@ -779,10 +789,6 @@ func (c *compiler) findBoard(g *d2graph.Graph, ir *d2ir.Map, ida []string) *d2gr
} }
} }
if board == nil {
return nil
}
g = board g = board
} }

View file

@ -2092,6 +2092,41 @@ layers: {
tassert.Equal(t, ".layers.x.layers.x", g.Objects[0].LinkedBoard.AbsID()) tassert.Equal(t, ".layers.x.layers.x", g.Objects[0].LinkedBoard.AbsID())
}, },
}, },
{
name: "link-board-underscore",
text: `x
layers: {
x: {
yo
layers: {
x: {
hello.link: _._.layers.x
hey.link: _
}
}
}
}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.NotNil(t, g.Layers[0].Layers[0].Objects[0].LinkedBoard)
tassert.Equal(t, ".layers.x", g.Layers[0].Layers[0].Objects[0].LinkedBoard.AbsID())
tassert.Equal(t, ".layers.x", g.Layers[0].Layers[0].Objects[1].LinkedBoard.AbsID())
},
},
{
name: "link-board-underscore-not-found",
text: `x
layers: {
x: {
yo
layers: {
x: {
hello.link: _._._
}
}
}
}`,
expErr: `d2/testdata/d2compiler/TestCompile/link-board-underscore-not-found.d2:7:17: link key "_._._" to board not found`,
},
} }
for _, tc := range testCases { for _, tc := range testCases {

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/link-board-underscore-not-found.d2,6:16:62-6:21:67",
"errmsg": "d2/testdata/d2compiler/TestCompile/link-board-underscore-not-found.d2:7:17: link key \"_._._\" to board not found"
}
]
}
}

File diff suppressed because it is too large Load diff