This commit is contained in:
Alexander Wang 2023-06-26 10:51:07 -07:00
parent 5be1f6c187
commit 905a9317a2
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 152 additions and 3 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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
}

View file

@ -179,7 +179,7 @@
},
"edges": [
{
"index": 0,
"index": 1,
"isCurve": false,
"src_arrow": false,
"dst_arrow": true,