fix style compiling issue

This commit is contained in:
Alexander Wang 2022-12-22 23:41:15 -08:00
parent 48973ab330
commit 3c176ebfbc
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 60 additions and 121 deletions

View file

@ -13,3 +13,4 @@
- Fixed an issue with elk layouts accounting for edge labels as if they were placed on the side of the edge. [#483](https://github.com/terrastruct/d2/pull/483) - Fixed an issue with elk layouts accounting for edge labels as if they were placed on the side of the edge. [#483](https://github.com/terrastruct/d2/pull/483)
- Fixed an issue where dagre layouts may not have enough spacing for all edge labels. [#484](https://github.com/terrastruct/d2/pull/484) - Fixed an issue where dagre layouts may not have enough spacing for all edge labels. [#484](https://github.com/terrastruct/d2/pull/484)
- Fixed connections being clipped if they were at the very top or left edges of the diagram. [#493](https://github.com/terrastruct/d2/pull/493) - Fixed connections being clipped if they were at the very top or left edges of the diagram. [#493](https://github.com/terrastruct/d2/pull/493)
- Fixed edge case where style being defined in same scope as sql_table caused compiler to skip compiling sql_table. [#506](https://github.com/terrastruct/d2/issues/506)

View file

@ -583,16 +583,25 @@ func (c *compiler) compileShapes(obj *d2graph.Object) {
c.compileShapes(obj) c.compileShapes(obj)
} }
for _, obj := range obj.ChildrenArray { for i := 0; i < len(obj.ChildrenArray); i++ {
switch obj.Attributes.Shape.Value { ch := obj.ChildrenArray[i]
switch ch.Attributes.Shape.Value {
case d2target.ShapeClass, d2target.ShapeSQLTable: case d2target.ShapeClass, d2target.ShapeSQLTable:
flattenContainer(obj.Graph, obj) flattenContainer(obj.Graph, ch)
} }
if obj.IDVal == "style" { if ch.IDVal == "style" {
obj.Parent.Attributes.Style = obj.Attributes.Style obj.Attributes.Style = ch.Attributes.Style
if obj.Graph != nil { if obj.Graph != nil {
flattenContainer(obj.Graph, obj) flattenContainer(obj.Graph, ch)
removeObject(obj.Graph, obj) for i := 0; i < len(obj.Graph.Objects); i++ {
if obj.Graph.Objects[i] == ch {
obj.Graph.Objects = append(obj.Graph.Objects[:i], obj.Graph.Objects[i+1:]...)
break
}
}
delete(obj.Children, ch.ID)
obj.ChildrenArray = append(obj.ChildrenArray[:i], obj.ChildrenArray[i+1:]...)
i--
} }
} }
} }
@ -706,23 +715,6 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) {
} }
} }
// TODO too similar to flattenContainer, should reconcile in a refactor
func removeObject(g *d2graph.Graph, obj *d2graph.Object) {
for i := 0; i < len(obj.Graph.Objects); i++ {
if obj.Graph.Objects[i] == obj {
obj.Graph.Objects = append(obj.Graph.Objects[:i], obj.Graph.Objects[i+1:]...)
break
}
}
delete(obj.Parent.Children, obj.ID)
for i, child := range obj.Parent.ChildrenArray {
if obj == child {
obj.Parent.ChildrenArray = append(obj.Parent.ChildrenArray[:i], obj.Parent.ChildrenArray[i+1:]...)
break
}
}
}
func flattenContainer(g *d2graph.Graph, obj *d2graph.Object) { func flattenContainer(g *d2graph.Graph, obj *d2graph.Object) {
absID := obj.AbsID() absID := obj.AbsID()

View file

@ -1657,9 +1657,9 @@ choo: {
style: { style: {
fill: lemonchiffon fill: lemonchiffon
} }
e: { b: {
shape: sql_table shape: sql_table
b: c c
} }
d d
} }

View file

@ -1,11 +1,11 @@
{ {
"graph": { "graph": {
"ast": { "ast": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-12:0:124", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-10:0:87",
"nodes": [ "nodes": [
{ {
"map_key": { "map_key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-11:1:123", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-9:1:86",
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-0:1:1", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:0:0-0:1:1",
"path": [ "path": [
@ -25,7 +25,7 @@
"primary": {}, "primary": {},
"value": { "value": {
"map": { "map": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:3:3-11:0:122", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,0:3:3-9:0:85",
"nodes": [ "nodes": [
{ {
"map_key": { "map_key": {
@ -91,17 +91,17 @@
}, },
{ {
"map_key": { "map_key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:2:46-8:3:112", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:2:45-7:3:80",
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:2:46-5:8:52", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:2:45-4:3:46",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:2:46-5:8:52", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:2:45-4:3:46",
"value": [ "value": [
{ {
"string": "legend", "string": "b",
"raw_string": "legend" "raw_string": "b"
} }
] ]
} }
@ -111,17 +111,17 @@
"primary": {}, "primary": {},
"value": { "value": {
"map": { "map": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:10:54-8:2:111", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:5:48-7:2:79",
"nodes": [ "nodes": [
{ {
"map_key": { "map_key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:60-6:20:76", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:4:54-5:20:70",
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:60-6:9:65", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:4:54-5:9:59",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:60-6:9:65", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:4:54-5:9:59",
"value": [ "value": [
{ {
"string": "shape", "string": "shape",
@ -135,7 +135,7 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:11:67-6:20:76", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:11:61-5:20:70",
"value": [ "value": [
{ {
"string": "sql_table", "string": "sql_table",
@ -148,17 +148,17 @@
}, },
{ {
"map_key": { "map_key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:4:81-7:31:108", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:75-6:5:76",
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:4:81-7:9:86", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:75-6:5:76",
"path": [ "path": [
{ {
"double_quoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:4:81-7:9:86", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,6:4:75-6:5:76",
"value": [ "value": [
{ {
"string": "***", "string": "c",
"raw_string": "***" "raw_string": "c"
} }
] ]
} }
@ -166,17 +166,7 @@
] ]
}, },
"primary": {}, "primary": {},
"value": { "value": {}
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:11:88-7:31:108",
"value": [
{
"string": "not in terraform yet",
"raw_string": "not in terraform yet"
}
]
}
}
} }
} }
] ]
@ -186,17 +176,17 @@
}, },
{ {
"map_key": { "map_key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,10:2:116-10:7:121", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,8:2:83-8:3:84",
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,10:2:116-10:7:121", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,8:2:83-8:3:84",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,10:2:116-10:7:121", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,8:2:83-8:3:84",
"value": [ "value": [
{ {
"string": "thing", "string": "d",
"raw_string": "thing" "raw_string": "d"
} }
] ]
} }
@ -287,8 +277,8 @@
"zIndex": 0 "zIndex": 0
}, },
{ {
"id": "legend", "id": "b",
"id_val": "legend", "id_val": "b",
"label_dimensions": { "label_dimensions": {
"width": 0, "width": 0,
"height": 0 "height": 0
@ -296,15 +286,15 @@
"references": [ "references": [
{ {
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:2:46-5:8:52", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:2:45-4:3:46",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,5:2:46-5:8:52", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,4:2:45-4:3:46",
"value": [ "value": [
{ {
"string": "legend", "string": "b",
"raw_string": "legend" "raw_string": "b"
} }
] ]
} }
@ -319,7 +309,7 @@
"columns": [ "columns": [
{ {
"name": { "name": {
"label": "***", "label": "c",
"fontSize": 0, "fontSize": 0,
"fontFamily": "", "fontFamily": "",
"language": "", "language": "",
@ -331,7 +321,7 @@
"labelHeight": 0 "labelHeight": 0
}, },
"type": { "type": {
"label": "not in terraform yet", "label": "",
"fontSize": 0, "fontSize": 0,
"fontFamily": "", "fontFamily": "",
"language": "", "language": "",
@ -349,7 +339,7 @@
}, },
"attributes": { "attributes": {
"label": { "label": {
"value": "legend" "value": "b"
}, },
"style": {}, "style": {},
"near_key": null, "near_key": null,
@ -363,8 +353,8 @@
"zIndex": 0 "zIndex": 0
}, },
{ {
"id": "\"***\"", "id": "d",
"id_val": "***", "id_val": "d",
"label_dimensions": { "label_dimensions": {
"width": 0, "width": 0,
"height": 0 "height": 0
@ -372,59 +362,15 @@
"references": [ "references": [
{ {
"key": { "key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:4:81-7:9:86", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,8:2:83-8:3:84",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,7:4:81-7:9:86",
"value": [
{
"string": "***",
"raw_string": "***"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "not in terraform yet"
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
}
},
"zIndex": 0
},
{
"id": "thing",
"id_val": "thing",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,10:2:116-10:7:121",
"path": [ "path": [
{ {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,10:2:116-10:7:121", "range": "d2/testdata/d2compiler/TestCompile/sql-regression.d2,8:2:83-8:3:84",
"value": [ "value": [
{ {
"string": "thing", "string": "d",
"raw_string": "thing" "raw_string": "d"
} }
] ]
} }
@ -437,7 +383,7 @@
], ],
"attributes": { "attributes": {
"label": { "label": {
"value": "thing" "value": "d"
}, },
"style": {}, "style": {},
"near_key": null, "near_key": null,