From bd7f9e1c39affa58589f800e94af5c97b2998a1d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 25 Apr 2024 07:02:21 -0700 Subject: [PATCH] d2oracle: fix setting with comments --- d2oracle/edit.go | 6 +- d2oracle/edit_test.go | 16 ++ .../d2oracle/TestSet/edge-comment.exp.json | 265 ++++++++++++++++++ 3 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 testdata/d2oracle/TestSet/edge-comment.exp.json diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 17ea7caaea..827942d7ed 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -562,7 +562,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string) foundMap = true scope = ref.MapKey.Value.Map for _, n := range scope.Nodes { - if n.MapKey.Value.Map == nil { + if n.MapKey == nil || n.MapKey.Value.Map == nil { continue } if n.MapKey.Key == nil || len(n.MapKey.Key.Path) != 1 { @@ -1059,7 +1059,7 @@ func bumpChildrenUnderscores(m *d2ast.Map) { } func hoistRefChildren(g *d2graph.Graph, key *d2ast.KeyPath, ref d2graph.Reference) { - if ref.MapKey.Value.Map == nil { + if ref.MapKey == nil || ref.MapKey.Value.Map == nil { return } @@ -1115,7 +1115,7 @@ func renameConflictsToParent(g *d2graph.Graph, key *d2ast.KeyPath) (*d2graph.Gra var absKeys []*d2ast.KeyPath if len(ref.Key.Path)-1 == ref.KeyPathIndex { - if ref.MapKey.Value.Map == nil { + if ref.MapKey == nil || ref.MapKey.Value.Map == nil { continue } var mapKeys []*d2ast.KeyPath diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 51046b7535..072eac68d8 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -2502,6 +2502,22 @@ x -> a.b -> a.b.c x -> a.b -> a.b.c (a.b -> a.b.c)[0].style.stroke: green } +`, + }, + { + name: "edge-comment", + + text: `x -> y: { + # hi + style.stroke: blue +} +`, + key: `(x -> y)[0].style.stroke`, + value: go2.Pointer(`green`), + exp: `x -> y: { + # hi + style.stroke: green +} `, }, } diff --git a/testdata/d2oracle/TestSet/edge-comment.exp.json b/testdata/d2oracle/TestSet/edge-comment.exp.json new file mode 100644 index 0000000000..4458b4bc44 --- /dev/null +++ b/testdata/d2oracle/TestSet/edge-comment.exp.json @@ -0,0 +1,265 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-4:0:41", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-3:1:40", + "edges": [ + { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-0:6:6", + "src": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:5:5-0:6:6", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:8:8-3:1:40", + "nodes": [ + { + "comment": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,1:2:12-1:6:16", + "value": "hi" + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,2:2:19-2:21:38", + "key": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,2:2:19-2:14:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,2:2:19-2:7:24", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,2:8:25-2:14:31", + "value": [ + { + "string": "stroke", + "raw_string": "stroke" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,2:16:33-2:21:38", + "value": [ + { + "string": "green", + "raw_string": "green" + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "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": [ + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": { + "stroke": { + "value": "green" + } + }, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "objects": [ + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestSet/edge-comment.d2,0:5:5-0:6:6", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": "" +}