Skip to content

Commit

Permalink
compiler: override composite substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Dec 8, 2023
1 parent 7a85946 commit d956083
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 1 deletion.
15 changes: 15 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3715,6 +3715,21 @@ a: {
assert.Equal(t, "im nested var", g.Objects[1].Label.Value)
},
},
{
name: "label-map",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
vars: {
x: {
label: hi
}
}
a: ${x}
a.label: hello
`, "")
assert.Equal(t, "hello", g.Objects[0].Label.Value)
},
},
{
name: "var-in-var",
run: func(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node Node) {
if resolvedField.Composite != nil {
switch n := node.(type) {
case *Field:
n.Composite = resolvedField.Composite
if n.Composite != nil {
n.Composite = n.Composite.Copy(resolvedField.Composite).(Composite)
} else {
n.Composite = resolvedField.Composite
}
case *Edge:
if resolvedField.Composite.Map() == nil {
c.errorf(node.LastRef().AST(), `cannot substitute array variable "%s" to an edge`, strings.Join(box.Substitution.IDA(), "."))
Expand Down
297 changes: 297 additions & 0 deletions testdata/d2compiler/TestCompile2/vars/override/label-map.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d956083

Please sign in to comment.