diff --git a/d2compiler/compile.go b/d2compiler/compile.go index d0b5f4591..1fa28054e 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -126,11 +126,8 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName g2 := d2graph.NewGraph() g2.Parent = g g2.AST = m.AST().(*d2ast.Map) - 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 + if g.BaseAST != nil { + g2.BaseAST = findFieldAST(g.BaseAST, f) } c.compileBoard(g2, m) if f.Primary() != nil { diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 107aa0137..ff0403d6e 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -43,7 +43,7 @@ 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) + return nil, "", fmt.Errorf("board %v cannot be modified through this file", boardPath) } } @@ -101,6 +101,9 @@ func Set(g *d2graph.Graph, boardPath []string, key string, tag, value *string) ( } // TODO beter name baseAST = boardG.BaseAST + if baseAST == nil { + return nil, fmt.Errorf("board %v cannot be modified through this file", boardPath) + } } err = _set(boardG, baseAST, key, tag, value) @@ -109,16 +112,10 @@ func Set(g *d2graph.Graph, boardPath []string, key string, tag, value *string) ( } if len(boardPath) > 0 { - // 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) - } + replaced := ReplaceBoardNode(g.AST, baseAST, boardPath) + if !replaced { + return nil, fmt.Errorf("board %v AST not found", boardPath) } - return nil, fmt.Errorf("board %v AST not found", boardPath) } return recompile(g) @@ -152,7 +149,7 @@ 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) + return nil, fmt.Errorf("board %v cannot be modified through this file", boardPath) } } @@ -959,7 +956,7 @@ 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) + return nil, fmt.Errorf("board %v cannot be modified through this file", boardPath) } } @@ -1777,7 +1774,7 @@ 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) + return nil, fmt.Errorf("board %v cannot be modified through this file", boardPath) } } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 72aef070d..3d37814f0 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -2487,15 +2487,7 @@ layers: { boardPath: []string{"man", "1"}, key: `asdf.link`, value: go2.Pointer(`_._`), - exp: `heyn - -layers: { - man: { - ...@meow - asdf.link: _._ - } -} -`, + expErr: `failed to set "asdf.link" to "\"_._\"": board [man 1] cannot be modified through this file`, }, { name: "label-near/1", diff --git a/testdata/d2oracle/TestSet/import/10.exp.json b/testdata/d2oracle/TestSet/import/10.exp.json index def29474c..909ef237e 100644 --- a/testdata/d2oracle/TestSet/import/10.exp.json +++ b/testdata/d2oracle/TestSet/import/10.exp.json @@ -1,568 +1,4 @@ { - "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": "" + "graph": null, + "err": "failed to set \"asdf.link\" to \"\\\"_._\\\"\": board [man 1] cannot be modified through this file" }