From 905a9317a2432beac987c6dba97c714fe67b0c42 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 26 Jun 2023 10:51:07 -0700 Subject: [PATCH] fix --- d2compiler/compile.go | 5 + d2graph/d2graph.go | 15 +- .../TestCompile2/nulls/basic/edge.exp.json | 133 +++++++++++++++++- .../TestCompile2/nulls/reappear/edge.exp.json | 2 +- 4 files changed, 152 insertions(+), 3 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 27f12ed28..d75ceb2f9 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -840,6 +840,11 @@ func (c *compiler) nullify(g *d2graph.Graph) { g.DeleteObject(obj) } } + for _, e := range g.Edges { + if len(e.References) > 0 && e.References[len(e.References)-1].Nulled() { + g.DeleteEdge(e) + } + } } // TODO add more, e.g. C, bash diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index f86eb8f4e..e140542f1 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -95,6 +95,15 @@ func (g *Graph) DeleteObject(obj *Object) { } } +func (g *Graph) DeleteEdge(e *Edge) { + for i, e2 := range g.Edges { + if e == e2 { + g.Edges = append(g.Edges[:i], g.Edges[i+1:]...) + i-- + } + } +} + type LayoutGraph func(context.Context, *Graph) error // TODO consider having different Scalar types @@ -216,7 +225,7 @@ type Reference struct { } func (r *Reference) Nulled() bool { - return r.MapKey != nil && r.MapKey.Value.Null != nil + return r.MapKey != nil && len(r.MapKey.Edges) == 0 && r.MapKey.Value.Null != nil } func (r Reference) MapKeyEdgeDest() bool { @@ -1146,6 +1155,10 @@ type EdgeReference struct { ScopeAST *d2ast.Map `json:"-"` } +func (er *EdgeReference) Nulled() bool { + return er.MapKey != nil && er.MapKey.Value.Null != nil +} + func (e *Edge) GetAstEdge() *d2ast.Edge { return e.References[0].Edge } diff --git a/testdata/d2compiler/TestCompile2/nulls/basic/edge.exp.json b/testdata/d2compiler/TestCompile2/nulls/basic/edge.exp.json index 842a64237..9f83b57fc 100644 --- a/testdata/d2compiler/TestCompile2/nulls/basic/edge.exp.json +++ b/testdata/d2compiler/TestCompile2/nulls/basic/edge.exp.json @@ -132,7 +132,138 @@ "zIndex": 0 }, "edges": null, - "objects": null + "objects": [ + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,1:0:1-1:1:2", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,2:1:9-2:2:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,2:1:9-2:2:10", + "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": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,1:5:6-1:6:7", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,1:5:6-1:6:7", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + }, + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,2:6:14-2:7:15", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/nulls/basic/edge.d2,2:6:14-2:7:15", + "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": null } diff --git a/testdata/d2compiler/TestCompile2/nulls/reappear/edge.exp.json b/testdata/d2compiler/TestCompile2/nulls/reappear/edge.exp.json index 21b584215..726082f0e 100644 --- a/testdata/d2compiler/TestCompile2/nulls/reappear/edge.exp.json +++ b/testdata/d2compiler/TestCompile2/nulls/reappear/edge.exp.json @@ -179,7 +179,7 @@ }, "edges": [ { - "index": 0, + "index": 1, "isCurve": false, "src_arrow": false, "dst_arrow": true,