Merge pull request #2390 from alixander/fix-keyword-merging

d2ir: fix key quoting issues
This commit is contained in:
Alexander Wang 2025-02-27 18:07:36 -08:00 committed by GitHub
commit 80f560a844
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1057 additions and 3 deletions

View file

@ -25,4 +25,5 @@
- fixes panic when `sql_shape` shape value had mixed casing [#2349](https://github.com/terrastruct/d2/pull/2349)
- fixes support for `center` in `d2-config` [#2360](https://github.com/terrastruct/d2/pull/2360)
- fixes panic when comment lines appear in arrays [#2378](https://github.com/terrastruct/d2/pull/2378)
- fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390)
- CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370)

View file

@ -5450,6 +5450,27 @@ a -> b: {
assert.Equal(t, "This is a->b", g.Edges[0].LegendLabel.Value)
},
},
{
name: "merge-glob-values",
run: func(t *testing.T) {
assertCompile(t, `
"a"
*.style.stroke-width: 2
*.style.font-size: 14
a.width: 339
`, ``)
},
},
{
name: "mixed-edge-quoting",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
"a"."b"."c"."z1" -> "a"."b"."c"."z2"
`, ``)
assert.Equal(t, 5, len(g.Objects))
},
},
}
for _, tc := range tca {

View file

@ -758,8 +758,10 @@ func (m *Map) getField(ida []d2ast.String) *Field {
if !strings.EqualFold(f.Name.ScalarString(), s.ScalarString()) {
continue
}
if f.Name.IsUnquoted() != s.IsUnquoted() {
continue
if _, isReserved := d2ast.ReservedKeywords[strings.ToLower(s.ScalarString())]; isReserved {
if f.Name.IsUnquoted() != s.IsUnquoted() {
continue
}
}
if len(rest) == 0 {
return f
@ -912,9 +914,14 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}
for _, f := range m.Fields {
if !(f.Name != nil && strings.EqualFold(f.Name.ScalarString(), head.ScalarString()) && f.Name.IsUnquoted() == head.IsUnquoted()) {
if !(f.Name != nil && strings.EqualFold(f.Name.ScalarString(), head.ScalarString())) {
continue
}
if _, isReserved := d2ast.ReservedKeywords[strings.ToLower(f.Name.ScalarString())]; isReserved {
if f.Name.IsUnquoted() != head.IsUnquoted() {
continue
}
}
// Don't add references for fake common KeyPath from trimCommon in CreateEdge.
if refctx != nil {

View file

@ -0,0 +1,295 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,0:0:0-6:0:65",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,1:0:1-1:3:4",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,1:0:1-1:3:4",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"primary": {},
"value": {}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:0:5-2:23:28",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:0:5-2:20:25",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:0:5-2:1:6",
"value": [
{
"string": "*",
"raw_string": "*"
}
],
"pattern": [
"*"
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:2:7-2:7:12",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:8:13-2:20:25",
"value": [
{
"string": "stroke-width",
"raw_string": "stroke-width"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,2:22:27-2:23:28",
"raw": "2",
"value": "2"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:0:29-3:21:50",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:0:29-3:17:46",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:0:29-3:1:30",
"value": [
{
"string": "*",
"raw_string": "*"
}
],
"pattern": [
"*"
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:2:31-3:7:36",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:8:37-3:17:46",
"value": [
{
"string": "font-size",
"raw_string": "font-size"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,3:19:48-3:21:50",
"raw": "14",
"value": "14"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:0:52-5:12:64",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:0:52-5:7:59",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:0:52-5:1:53",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:2:54-5:7:59",
"value": [
{
"string": "width",
"raw_string": "width"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:9:61-5:12:64",
"raw": "339",
"value": "339"
}
}
}
}
]
},
"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": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,1:0:1-1:3:4",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:0:52-5:7:59",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:0:52-5:1:53",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/merge-glob-values.d2,5:2:54-5:7:59",
"value": [
{
"string": "width",
"raw_string": "width"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {
"strokeWidth": {
"value": "2"
},
"fontSize": {
"value": "14"
}
},
"width": {
"value": "339"
},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}

View file

@ -0,0 +1,730 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,0:0:0-2:0:38",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:36:37",
"edges": [
{
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:36:37",
"src": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:16:17",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:4:5-1:7:8",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:8:9-1:11:12",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:12:13-1:16:17",
"value": [
{
"string": "z1",
"raw_string": "z1"
}
]
}
}
]
},
"src_arrow": "",
"dst": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:36:37",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:23:24",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:24:25-1:27:28",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:28:29-1:31:32",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:32:33-1:36:37",
"value": [
{
"string": "z2",
"raw_string": "z2"
}
]
}
}
]
},
"dst_arrow": ">"
}
],
"primary": {},
"value": {}
}
}
]
},
"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": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
],
"objects": [
{
"id": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:16:17",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:4:5-1:7:8",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:8:9-1:11:12",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:12:13-1:16:17",
"value": [
{
"string": "z1",
"raw_string": "z1"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:36:37",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:23:24",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:24:25-1:27:28",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:28:29-1:31:32",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:32:33-1:36:37",
"value": [
{
"string": "z2",
"raw_string": "z2"
}
]
}
}
]
},
"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/globs/mixed-edge-quoting.d2,1:0:1-1:16:17",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:4:5-1:7:8",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:8:9-1:11:12",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:12:13-1:16:17",
"value": [
{
"string": "z1",
"raw_string": "z1"
}
]
}
}
]
},
"key_path_index": 1,
"map_key_edge_index": 0
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:36:37",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:23:24",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:24:25-1:27:28",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:28:29-1:31:32",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:32:33-1:36:37",
"value": [
{
"string": "z2",
"raw_string": "z2"
}
]
}
}
]
},
"key_path_index": 1,
"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
},
{
"id": "c",
"id_val": "c",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:16:17",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:4:5-1:7:8",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:8:9-1:11:12",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:12:13-1:16:17",
"value": [
{
"string": "z1",
"raw_string": "z1"
}
]
}
}
]
},
"key_path_index": 2,
"map_key_edge_index": 0
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:36:37",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:23:24",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:24:25-1:27:28",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:28:29-1:31:32",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:32:33-1:36:37",
"value": [
{
"string": "z2",
"raw_string": "z2"
}
]
}
}
]
},
"key_path_index": 2,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "c"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "z1",
"id_val": "z1",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:16:17",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:0:1-1:3:4",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:4:5-1:7:8",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:8:9-1:11:12",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:12:13-1:16:17",
"value": [
{
"string": "z1",
"raw_string": "z1"
}
]
}
}
]
},
"key_path_index": 3,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "z1"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "z2",
"id_val": "z2",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:36:37",
"path": [
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:20:21-1:23:24",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:24:25-1:27:28",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:28:29-1:31:32",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/mixed-edge-quoting.d2,1:32:33-1:36:37",
"value": [
{
"string": "z2",
"raw_string": "z2"
}
]
}
}
]
},
"key_path_index": 3,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "z2"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}