pr changes

This commit is contained in:
Alexander Wang 2023-07-11 23:26:06 -07:00
parent 7091c580ef
commit 63dbaf0935
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 412 additions and 42 deletions

View file

@ -3428,6 +3428,23 @@ a: {
assert.Equal(t, "BlackSmith", g.Objects[1].Label.Value)
},
},
{
name: "recursive-var",
run: func(t *testing.T) {
g := assertCompile(t, `
vars: {
x: a
}
hi: {
vars: {
x: ${x}-b
}
yo: ${x}
}
`, "")
assert.Equal(t, "a-b", g.Objects[1].Label.Value)
},
},
}
for _, tc := range tca {
@ -3601,6 +3618,17 @@ hi: ${x}
`, `d2/testdata/d2compiler/TestCompile2/vars/errors/out-of-scope.d2:7:1: could not resolve variable "x"`)
},
},
{
name: "recursive-var",
run: func(t *testing.T) {
assertCompile(t, `
vars: {
x: ${x}
}
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) {

View file

@ -107,7 +107,13 @@ func (c *compiler) compileSubstitutions(m *Map, varsStack []*Map) {
c.resolveSubstitutions(varsStack, f.LastRef().AST(), f.Primary())
}
if f.Map() != nil {
c.compileSubstitutions(f.Map(), varsStack)
// this map could be the "vars"
// and if it is, then its already been compiled, so the varsStack can be truncated
if f.Name == "vars" {
c.compileSubstitutions(f.Map(), varsStack[1:])
} else {
c.compileSubstitutions(f.Map(), varsStack)
}
}
}
for _, e := range m.Edges {
@ -134,27 +140,25 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node d2ast.Node, scala
break
}
}
if resolvedField != nil {
if resolvedField.Composite != nil {
c.errorf(node, `cannot reference map variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
// If lone and unquoted, replace with value of sub
if len(s.Value) == 1 {
scalar.Value = resolvedField.Primary().Value
} else {
s.Value[i].String = go2.Pointer(resolvedField.Primary().String())
subbed = true
}
} else {
if resolvedField == nil {
c.errorf(node, `could not resolve variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
if resolvedField.Composite != nil {
c.errorf(node, `cannot reference map variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
// If lone and unquoted, replace with value of sub
if len(s.Value) == 1 {
scalar.Value = resolvedField.Primary().Value
} else {
s.Value[i].String = go2.Pointer(resolvedField.Primary().String())
subbed = true
}
}
}
if subbed {
s.Coalesce()
scalar.Value = d2ast.RawString(s.ScalarString(), false)
}
case *d2ast.DoubleQuotedString:
for i, box := range s.Value {
@ -165,17 +169,16 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node d2ast.Node, scala
break
}
}
if resolvedField != nil {
if resolvedField.Composite != nil {
c.errorf(node, `cannot reference map variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
s.Value[i].String = go2.Pointer(resolvedField.Primary().String())
subbed = true
} else {
if resolvedField == nil {
c.errorf(node, `could not resolve variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
if resolvedField.Composite != nil {
c.errorf(node, `cannot reference map variable "%s"`, strings.Join(box.Substitution.IDA(), "."))
return
}
s.Value[i].String = go2.Pointer(resolvedField.Primary().String())
subbed = true
}
}
if subbed {
@ -185,21 +188,21 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node d2ast.Node, scala
}
func (c *compiler) resolveSubstitution(vars *Map, substitution *d2ast.Substitution) *Field {
var resolved *Field
for _, p := range substitution.Path {
if vars == nil {
resolved = nil
break
}
r := vars.GetField(p.Unbox().ScalarString())
if r == nil {
resolved = nil
break
}
vars = r.Map()
resolved = r
if vars == nil {
return nil
}
return resolved
for i, p := range substitution.Path {
f := vars.GetField(p.Unbox().ScalarString())
if f == nil {
return nil
}
if i == len(substitution.Path)-1 {
return f
}
vars = f.Map()
}
return nil
}
func (c *compiler) overlayVars(base, overlay *Map) {
@ -208,14 +211,14 @@ func (c *compiler) overlayVars(base, overlay *Map) {
return
}
lVars := base.GetField("vars")
baseVars := base.GetField("vars")
if lVars == nil {
lVars = vars.Copy(base).(*Field)
base.Fields = append(base.Fields, lVars)
if baseVars == nil {
baseVars = vars.Copy(base).(*Field)
base.Fields = append(base.Fields, baseVars)
} else {
overlayed := vars.Copy(base).(*Field)
OverlayMap(overlayed.Map(), lVars.Map())
OverlayMap(overlayed.Map(), baseVars.Map())
base.DeleteField("vars")
base.Fields = append(base.Fields, overlayed)
}

View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/recursive-var.d2,2:2:11-2:3:12",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/recursive-var.d2:3:3: could not resolve variable \"x\""
}
]
}
}

View file

@ -0,0 +1,328 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,0:0:0-10:0:65",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,1:0:1-3:1:17",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,1:0:1-1:4:5",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,1:0:1-1:4:5",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,1:6:7-3:1:17",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,2:2:11-2:6:15",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,2:2:11-2:3:12",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,2:2:11-2:3:12",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,2:5:14-2:6:15",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:0:18-9:1:64",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:0:18-4:2:20",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:0:18-4:2:20",
"value": [
{
"string": "hi",
"raw_string": "hi"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:4:22-9:1:64",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,5:2:26-7:3:51",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,5:2:26-5:6:30",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,5:2:26-5:6:30",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,5:8:32-7:3:51",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,6:4:38-6:13:47",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,6:4:38-6:5:39",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,6:4:38-6:5:39",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,6:7:41-6:13:47",
"value": [
{
"string": "a-b"
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:2:54-8:10:62",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:2:54-8:4:56",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:2:54-8:4:56",
"value": [
{
"string": "yo",
"raw_string": "yo"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:6:58-8:7:59",
"value": [
{
"substitution": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:6:58-8:10:62",
"spread": false,
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:8:60-8:9:61",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
}
}
]
}
}
}
}
]
}
}
}
}
]
},
"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": "hi",
"id_val": "hi",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:0:18-4:2:20",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,4:0:18-4:2:20",
"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
},
{
"id": "yo",
"id_val": "yo",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:2:54-8:4:56",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/recursive-var.d2,8:2:54-8:4:56",
"value": [
{
"string": "yo",
"raw_string": "yo"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a-b"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}