fix deleting imported attributes

This commit is contained in:
Alexander Wang 2024-02-10 10:43:43 -08:00
parent 20c3598875
commit 994a67de14
No known key found for this signature in database
GPG key ID: 6A82C36A56162F42
5 changed files with 690 additions and 10 deletions

View file

@ -847,7 +847,7 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
baseAST = boardG.BaseAST
}
g2, err := deleteReserved(g, mk)
g2, err := deleteReserved(g, baseAST, mk)
if err != nil {
return nil, err
}
@ -1185,7 +1185,7 @@ func renameConflictsToParent(g *d2graph.Graph, key *d2ast.KeyPath) (*d2graph.Gra
return g, nil
}
func deleteReserved(g *d2graph.Graph, mk *d2ast.Key) (*d2graph.Graph, error) {
func deleteReserved(g *d2graph.Graph, baseAST *d2ast.Map, mk *d2ast.Key) (*d2graph.Graph, error) {
targetKey := mk.Key
if len(mk.Edges) == 1 {
if mk.EdgeKey == nil {
@ -1212,14 +1212,21 @@ func deleteReserved(g *d2graph.Graph, mk *d2ast.Key) (*d2graph.Graph, error) {
if !ok {
return g, nil
}
imported := IsImportedEdge(baseAST, e)
if err := deleteEdgeField(g, e, targetKey.Path[len(targetKey.Path)-1].Unbox().ScalarString()); err != nil {
return nil, err
if imported {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
} else {
if err := deleteEdgeField(g, e, targetKey.Path[len(targetKey.Path)-1].Unbox().ScalarString()); err != nil {
return nil, err
}
}
return recompile(g)
}
isStyleKey := false
imported := false
for _, id := range d2graph.Key(targetKey) {
_, ok := d2graph.ReservedKeywords[id]
if ok {
@ -1228,9 +1235,14 @@ func deleteReserved(g *d2graph.Graph, mk *d2ast.Key) (*d2graph.Graph, error) {
continue
}
if isStyleKey {
err := deleteObjField(g, obj, id)
if err != nil {
return nil, err
if imported {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
} else {
err := deleteObjField(g, obj, id)
if err != nil {
return nil, err
}
}
}
@ -1242,9 +1254,14 @@ func deleteReserved(g *d2graph.Graph, mk *d2ast.Key) (*d2graph.Graph, error) {
id == "left" ||
id == "top" ||
id == "link" {
err := deleteObjField(g, obj, id)
if err != nil {
return nil, err
if imported {
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
appendMapKey(baseAST, mk)
} else {
err := deleteObjField(g, obj, id)
if err != nil {
return nil, err
}
}
}
break
@ -1253,6 +1270,7 @@ func deleteReserved(g *d2graph.Graph, mk *d2ast.Key) (*d2graph.Graph, error) {
if !ok {
return nil, fmt.Errorf("object not found")
}
imported = IsImportedObj(baseAST, obj)
}
return recompile(g)

View file

@ -7233,6 +7233,50 @@ scenarios: {
key: `(a -> b)[0]`,
exp: `...@meow
(a -> b)[0]: null
`,
},
{
name: "import/4",
text: `...@meow
`,
fsTexts: map[string]string{
"meow.d2": `a.link: https://google.com
`,
},
key: `a.link`,
exp: `...@meow
a.link: null
`,
},
{
name: "import/5",
text: `...@meow
`,
fsTexts: map[string]string{
"meow.d2": `a -> b: {
target-arrowhead: 1
}
`,
},
key: `(a -> b)[0].target-arrowhead`,
exp: `...@meow
(a -> b)[0].target-arrowhead: null
`,
},
{
name: "import/6",
text: `...@meow
`,
fsTexts: map[string]string{
"meow.d2": `a.style.fill: red
`,
},
key: `a.style.fill`,
exp: `...@meow
a.style.fill: null
`,
},
}

183
testdata/d2oracle/TestDelete/import/4.exp.json generated vendored Normal file
View file

@ -0,0 +1,183 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "index.d2,0:0:0-2:0:22",
"nodes": [
{
"import": {
"range": "index.d2,0:0:0-0:8:8",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "index.d2,0:4:4-0:8:8",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
}
},
{
"map_key": {
"range": "index.d2,1:0:9-1:12:21",
"key": {
"range": "index.d2,1:0:9-1:6:15",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:0:9-1:1:10",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:2:11-1:6:15",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"primary": {},
"value": {
"null": {
"range": "index.d2,1:8:17-1:12:21"
}
}
}
}
]
},
"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": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "meow.d2,0:0:0-0:6:6",
"path": [
{
"unquoted_string": {
"range": "meow.d2,0:0:0-0:1:1",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "meow.d2,0:2:2-0:6:6",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "index.d2,1:0:9-1:6:15",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:0:9-1:1:10",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:2:11-1:6:15",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": "<nil>"
}

219
testdata/d2oracle/TestDelete/import/5.exp.json generated vendored Normal file
View file

@ -0,0 +1,219 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "index.d2,0:0:0-2:0:44",
"nodes": [
{
"import": {
"range": "index.d2,0:0:0-0:8:8",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "index.d2,0:4:4-0:8:8",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
}
},
{
"map_key": {
"range": "index.d2,1:0:9-1:34:43",
"edges": [
{
"range": "index.d2,1:1:10-1:7:16",
"src": {
"range": "index.d2,1:1:10-1:2:11",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:1:10-1:2:11",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"src_arrow": "",
"dst": {
"range": "index.d2,1:6:15-1:7:16",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:6:15-1:7:16",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
}
]
},
"dst_arrow": ">"
}
],
"edge_index": {
"range": "index.d2,1:8:17-1:11:20",
"int": 0,
"glob": false
},
"edge_key": {
"range": "index.d2,1:12:21-1:28:37",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:12:21-1:28:37",
"value": [
{
"string": "target-arrowhead",
"raw_string": "target-arrowhead"
}
]
}
}
]
},
"primary": {},
"value": {
"null": {
"range": "index.d2,1:30:39-1:34:43"
}
}
}
}
]
},
"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": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "meow.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "meow.d2,0:0:0-0:1:1",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "a"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "b",
"id_val": "b",
"references": [
{
"key": {
"range": "meow.d2,0:5:5-0:6:6",
"path": [
{
"unquoted_string": {
"range": "meow.d2,0:5:5-0:6:6",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "b"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": "<nil>"
}

216
testdata/d2oracle/TestDelete/import/6.exp.json generated vendored Normal file
View file

@ -0,0 +1,216 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "index.d2,0:0:0-2:0:28",
"nodes": [
{
"import": {
"range": "index.d2,0:0:0-0:8:8",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "index.d2,0:4:4-0:8:8",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
}
},
{
"map_key": {
"range": "index.d2,1:0:9-1:18:27",
"key": {
"range": "index.d2,1:0:9-1:12:21",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:0:9-1:1:10",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:2:11-1:7:16",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:8:17-1:12:21",
"value": [
{
"string": "fill",
"raw_string": "fill"
}
]
}
}
]
},
"primary": {},
"value": {
"null": {
"range": "index.d2,1:14:23-1:18:27"
}
}
}
}
]
},
"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": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "meow.d2,0:0:0-0:12:12",
"path": [
{
"unquoted_string": {
"range": "meow.d2,0:0:0-0:1:1",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "meow.d2,0:2:2-0:7:7",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "meow.d2,0:8:8-0:12:12",
"value": [
{
"string": "fill",
"raw_string": "fill"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "index.d2,1:0:9-1:12:21",
"path": [
{
"unquoted_string": {
"range": "index.d2,1:0:9-1:1:10",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:2:11-1:7:16",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "index.d2,1:8:17-1:12:21",
"value": [
{
"string": "fill",
"raw_string": "fill"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": "<nil>"
}