allow edges in vars

This commit is contained in:
Alexander Wang 2023-07-13 14:47:32 -07:00
parent e95f61985e
commit d275a45033
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
6 changed files with 391 additions and 75 deletions

View file

@ -3501,6 +3501,23 @@ custom-disclaimer: DRAFT DISCLAIMER {
assert.Equal(t, 2, len(g.Objects))
},
},
{
name: "spread-edge",
run: func(t *testing.T) {
g := assertCompile(t, `
vars: {
connections: {
x -> a
}
}
hi: {
...${connections}
}
`, "")
assert.Equal(t, 3, len(g.Objects))
assert.Equal(t, 1, len(g.Edges))
},
},
}
for _, tc := range tca {
@ -3860,17 +3877,7 @@ hi: ${x}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/recursive-var.d2:3:3: could not resolve variable "x"`)
},
},
{
name: "edge",
run: func(t *testing.T) {
assertCompile(t, `
vars: {
x -> a
}
hi
`, "d2/testdata/d2compiler/TestCompile2/vars/errors/edge.d2:3:3: vars cannot contain an edge")
},
},
{
name: "spread-non-map",
run: func(t *testing.T) {

View file

@ -523,10 +523,6 @@ func (c *compiler) compileLink(refctx *RefContext) {
}
func (c *compiler) compileEdges(refctx *RefContext) {
if IsVar(refctx.ScopeMap) {
c.errorf(refctx.Key, "vars cannot contain an edge")
return
}
if refctx.Key.Key != nil {
f, err := refctx.ScopeMap.EnsureField(refctx.Key.Key, refctx)
if err != nil {

View file

@ -0,0 +1,356 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,0:0:0-9:0:71",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,1:0:1-5:1:42",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,1:0:1-1:4:5",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,1:0:1-1:4:5",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,1:6:7-5:1:42",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,2:2:11-4:3:40",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,2:2:11-2:13:22",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,2:2:11-2:13:22",
"value": [
{
"string": "connections",
"raw_string": "connections"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,2:15:24-4:3:40",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:10:36",
"edges": [
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:10:36",
"src": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:5:31",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:5:31",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"src_arrow": "",
"dst": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:9:35-3:10:36",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:9:35-3:10:36",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"dst_arrow": ">"
}
],
"primary": {},
"value": {}
}
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:0:43-8:1:70",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:0:43-6:2:45",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:0:43-6:2:45",
"value": [
{
"string": "hi",
"raw_string": "hi"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:4:47-8:1:70",
"nodes": [
{
"substitution": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,7:2:51-7:19:68",
"spread": true,
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,7:7:56-7:18:67",
"value": [
{
"string": "connections",
"raw_string": "connections"
}
]
}
}
]
}
}
]
}
}
}
}
]
},
"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": "x",
"id_val": "x",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:5:31",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:4:30-3:5:31",
"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": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:9:35-3:10:36",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,3:9:35-3:10:36",
"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": "hi",
"id_val": "hi",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:0:43-6:2:45",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-edge.d2,6:0:43-6:2:45",
"value": [
{
"string": "hi",
"raw_string": "hi"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}

View file

@ -168,11 +168,10 @@
"name": "q",
"primary": {
"value": {
"range": "x.d2,0:6:6-0:18:18",
"range": "index.d2,0:20:20-0:21:21",
"value": [
{
"string": "var replaced",
"raw_string": "var replaced"
"string": "var replaced"
}
]
}
@ -230,23 +229,7 @@
"range": "index.d2,0:20:20-0:21:21",
"value": [
{
"substitution": {
"range": "index.d2,0:20:20-0:27:27",
"spread": false,
"path": [
{
"unquoted_string": {
"range": "index.d2,0:22:22-0:26:26",
"value": [
{
"string": "meow",
"raw_string": "meow"
}
]
}
}
]
}
"string": "var replaced"
}
]
}

View file

@ -312,9 +312,12 @@
"name": "hi",
"primary": {
"value": {
"range": "a.d2,0:11:11-0:12:12",
"raw": "2",
"value": "2"
"range": "a.d2,0:20:20-0:21:21",
"value": [
{
"string": "2"
}
]
}
},
"references": [
@ -370,23 +373,7 @@
"range": "a.d2,0:20:20-0:21:21",
"value": [
{
"substitution": {
"range": "a.d2,0:20:20-0:24:24",
"spread": false,
"path": [
{
"unquoted_string": {
"range": "a.d2,0:22:22-0:23:23",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
}
"string": "2"
}
]
}

View file

@ -312,9 +312,12 @@
"name": "hi",
"primary": {
"value": {
"range": "index.d2,0:18:18-0:19:19",
"raw": "1",
"value": "1"
"range": "index.d2,0:27:27-0:28:28",
"value": [
{
"string": "1"
}
]
}
},
"references": [
@ -370,23 +373,7 @@
"range": "index.d2,0:27:27-0:28:28",
"value": [
{
"substitution": {
"range": "index.d2,0:27:27-0:31:31",
"spread": false,
"path": [
{
"unquoted_string": {
"range": "index.d2,0:29:29-0:30:30",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
}
"string": "1"
}
]
}