change to error

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

View file

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

View file

@ -43,7 +43,7 @@ func Create(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
// TODO beter name // TODO beter name
baseAST = boardG.BaseAST baseAST = boardG.BaseAST
if baseAST == nil { 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 // TODO beter name
baseAST = boardG.BaseAST 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) 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 { if len(boardPath) > 0 {
// The baseAST may not correspond with the board path if the baseAST if the import replaced := ReplaceBoardNode(g.AST, baseAST, boardPath)
// In which case keep trying less nested board paths until it gets to the one with the import if !replaced {
// See test Set/import/10 return nil, fmt.Errorf("board %v AST not found", boardPath)
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) return recompile(g)
@ -152,7 +149,7 @@ func ReconnectEdge(g *d2graph.Graph, boardPath []string, edgeKey string, srcKey,
// TODO beter name // TODO beter name
baseAST = boardG.BaseAST baseAST = boardG.BaseAST
if baseAST == nil { 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 // TODO beter name
baseAST = boardG.BaseAST baseAST = boardG.BaseAST
if baseAST == nil { 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 // TODO beter name
baseAST = boardG.BaseAST baseAST = boardG.BaseAST
if baseAST == nil { 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)
} }
} }

View file

@ -2487,15 +2487,7 @@ layers: {
boardPath: []string{"man", "1"}, boardPath: []string{"man", "1"},
key: `asdf.link`, key: `asdf.link`,
value: go2.Pointer(`_._`), value: go2.Pointer(`_._`),
exp: `heyn expErr: `failed to set "asdf.link" to "\"_._\"": board [man 1] cannot be modified through this file`,
layers: {
man: {
...@meow
asdf.link: _._
}
}
`,
}, },
{ {
name: "label-near/1", name: "label-near/1",

View file

@ -1,568 +1,4 @@
{ {
"graph": { "graph": null,
"name": "", "err": "failed to set \"asdf.link\" to \"\\\"_._\\\"\": board [man 1] cannot be modified through this file"
"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>"
} }