d2oracle: fix setting nested import

This commit is contained in:
Alexander Wang 2025-01-08 11:33:00 -07:00
parent ebeb8baf08
commit 34ebc5d404
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
4 changed files with 624 additions and 5 deletions

View file

@ -126,8 +126,11 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName
g2 := d2graph.NewGraph()
g2.Parent = g
g2.AST = m.AST().(*d2ast.Map)
if g.BaseAST != nil {
g2.BaseAST = findFieldAST(g.BaseAST, f)
g2.BaseAST = findFieldAST(g.BaseAST, f)
// We cannot find the AST from the graph's base ast, which means it must be imported
// Then the base AST continues to be the one with the import
if g2.BaseAST == nil {
g2.BaseAST = g.BaseAST
}
c.compileBoard(g2, m)
if f.Primary() != nil {

View file

@ -42,6 +42,9 @@ func Create(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
}
// TODO beter name
baseAST = boardG.BaseAST
if baseAST == nil {
return nil, "", fmt.Errorf("board %v missing base AST", boardPath)
}
}
newKey, edge, err := generateUniqueKey(boardG, key, nil, nil)
@ -106,10 +109,16 @@ func Set(g *d2graph.Graph, boardPath []string, key string, tag, value *string) (
}
if len(boardPath) > 0 {
replaced := ReplaceBoardNode(g.AST, baseAST, boardPath)
if !replaced {
return nil, fmt.Errorf("board %v AST not found", boardPath)
// The baseAST may not correspond with the board path if the baseAST if the import
// In which case keep trying less nested board paths until it gets to the one with the import
// See test Set/import/10
for i := len(boardPath); i > 0; i-- {
replaced := ReplaceBoardNode(g.AST, baseAST, boardPath[:i])
if replaced {
return recompile(g)
}
}
return nil, fmt.Errorf("board %v AST not found", boardPath)
}
return recompile(g)
@ -142,6 +151,9 @@ func ReconnectEdge(g *d2graph.Graph, boardPath []string, edgeKey string, srcKey,
}
// TODO beter name
baseAST = boardG.BaseAST
if baseAST == nil {
return nil, fmt.Errorf("board %v missing base AST", boardPath)
}
}
obj := boardG.Root
@ -946,6 +958,9 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
}
// TODO beter name
baseAST = boardG.BaseAST
if baseAST == nil {
return nil, fmt.Errorf("board %v missing base AST", boardPath)
}
}
g2, err := deleteReserved(g, boardPath, baseAST, mk)
@ -1761,6 +1776,9 @@ func move(g *d2graph.Graph, boardPath []string, key, newKey string, includeDesce
}
// TODO beter name
baseAST = boardG.BaseAST
if baseAST == nil {
return nil, fmt.Errorf("board %v missing base AST", boardPath)
}
}
newKey, _, err := generateUniqueKey(boardG, newKey, nil, nil)

View file

@ -2465,6 +2465,36 @@ layers: {
value: go2.Pointer(`red`),
exp: `...@yo
(a -> b)[0].style.stroke: red
`,
},
{
name: "import/10",
text: `heyn
layers: {
man: {...@meow}
}
`,
fsTexts: map[string]string{
"meow.d2": `layers: {
1: {
asdf
}
}
`,
},
boardPath: []string{"man", "1"},
key: `asdf.link`,
value: go2.Pointer(`_._`),
exp: `heyn
layers: {
man: {
...@meow
asdf.link: _._
}
}
`,
},
{

568
testdata/d2oracle/TestSet/import/10.exp.json generated vendored Normal file
View file

@ -0,0 +1,568 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "index.d2,0:0:0-8:0:63",
"nodes": [
{
"map_key": {
"range": "index.d2,0:0:0-0:4:4",
"key": {
"range": "index.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "index.d2,0:0:0-0:4:4",
"value": [
{
"string": "heyn",
"raw_string": "heyn"
}
]
}
}
]
},
"primary": {},
"value": {}
}
},
{
"map_key": {
"range": "index.d2,2:0:6-7:1:62",
"key": {
"range": "index.d2,2:0:6-2:6:12",
"path": [
{
"unquoted_string": {
"range": "index.d2,2:0:6-2:6:12",
"value": [
{
"string": "layers",
"raw_string": "layers"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "index.d2,2:8:14-7:1:62",
"nodes": [
{
"map_key": {
"range": "index.d2,3:2:18-6:3:60",
"key": {
"range": "index.d2,3:2:18-3:5:21",
"path": [
{
"unquoted_string": {
"range": "index.d2,3:2:18-3:5:21",
"value": [
{
"string": "man",
"raw_string": "man"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "index.d2,3:7:23-6:3:60",
"nodes": [
{
"import": {
"range": "index.d2,4:4:29-4:12:37",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "index.d2,4:8:33-4:12:37",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
}
},
{
"map_key": {
"range": "index.d2,5:4:42-5:18:56",
"key": {
"range": "index.d2,5:4:42-5:13:51",
"path": [
{
"unquoted_string": {
"range": "index.d2,5:4:42-5:8:46",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,5:9:47-5:13:51",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "index.d2,5:15:53-5:18:56",
"value": [
{
"string": "_._",
"raw_string": "_._"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"attributes": {
"label": {
"value": ""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "heyn",
"id_val": "heyn",
"references": [
{
"key": {
"range": "index.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "index.d2,0:0:0-0:4:4",
"value": [
{
"string": "heyn",
"raw_string": "heyn"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "heyn"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
],
"layers": [
{
"name": "man",
"isFolderOnly": false,
"ast": {
"range": ",0:0:0-1:0:0",
"nodes": [
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "meow.d2,0:0:0-0:6:6",
"value": [
{
"string": "layers",
"raw_string": "layers"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",0:0:0-1:0:0",
"nodes": [
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "meow.d2,1:2:12-1:3:13",
"value": [
{
"string": "1",
"raw_string": "1"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",0:0:0-1:0:0",
"nodes": [
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "meow.d2,2:4:21-2:8:25",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
}
]
},
"primary": {},
"value": {}
}
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "index.d2,5:4:42-5:8:46",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",0:0:0-1:0:0",
"nodes": [
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "index.d2,5:9:47-5:13:51",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"primary": {
"unquoted_string": {
"range": ",0:0:0-0:0:0",
"value": [
{
"string": "root._"
}
]
}
},
"value": {}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"attributes": {
"label": {
"value": ""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "asdf",
"id_val": "asdf",
"references": [
{
"key": {
"range": "index.d2,5:4:42-5:13:51",
"path": [
{
"unquoted_string": {
"range": "index.d2,5:4:42-5:8:46",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,5:9:47-5:13:51",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "asdf"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
],
"layers": [
{
"name": "1",
"isFolderOnly": false,
"ast": {
"range": ",0:0:0-1:0:0",
"nodes": [
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": "meow.d2,2:4:21-2:8:25",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
}
]
},
"primary": {},
"value": {}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"attributes": {
"label": {
"value": ""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "asdf",
"id_val": "asdf",
"references": [
{
"key": {
"range": "meow.d2,2:4:21-2:8:25",
"path": [
{
"unquoted_string": {
"range": "meow.d2,2:4:21-2:8:25",
"value": [
{
"string": "asdf",
"raw_string": "asdf"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "asdf"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
}
]
}
]
},
"err": "<nil>"
}