From 34ebc5d40420496b130b1a44e3737f85445a68ad Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 8 Jan 2025 11:33:00 -0700 Subject: [PATCH] d2oracle: fix setting nested import --- d2compiler/compile.go | 7 +- d2oracle/edit.go | 24 +- d2oracle/edit_test.go | 30 + testdata/d2oracle/TestSet/import/10.exp.json | 568 +++++++++++++++++++ 4 files changed, 624 insertions(+), 5 deletions(-) create mode 100644 testdata/d2oracle/TestSet/import/10.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 1fa28054e..d0b5f4591 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -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 { diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 25e53200e..107aa0137 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -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) diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 7b8d139be..72aef070d 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -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: _._ + } +} `, }, { diff --git a/testdata/d2oracle/TestSet/import/10.exp.json b/testdata/d2oracle/TestSet/import/10.exp.json new file mode 100644 index 000000000..def29474c --- /dev/null +++ b/testdata/d2oracle/TestSet/import/10.exp.json @@ -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": "" +}