Skip to content

Commit

Permalink
Merge pull request #1473 from alixander/vars
Browse files Browse the repository at this point in the history
Vars
  • Loading branch information
alixander authored Jul 13, 2023
2 parents 05154ad + da0d245 commit 4adfb22
Show file tree
Hide file tree
Showing 65 changed files with 13,427 additions and 14 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#### Features 🚀

- Variables and substitutions are implemented. See [docs](https://d2lang.com/tour/vars). [#1473](https://github.com/terrastruct/d2/pull/1473)
- Configure timeout value with D2_TIMEOUT env var [#1392](https://github.com/terrastruct/d2/pull/1392)
- Scale renders and disable fit to screen with `--scale` flag [#1413](https://github.com/terrastruct/d2/pull/1413)
- `null` keyword can be used to un-declare. See [docs](https://d2lang.com/tour/TODO) [#1446](https://github.com/terrastruct/d2/pull/1446)
Expand Down
22 changes: 22 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,17 @@ type UnquotedString struct {
Value []InterpolationBox `json:"value"`
}

func (s *UnquotedString) Coalesce() {
var b strings.Builder
for _, box := range s.Value {
if box.String == nil {
break
}
b.WriteString(*box.String)
}
s.SetString(b.String())
}

func FlatUnquotedString(s string) *UnquotedString {
return &UnquotedString{
Value: []InterpolationBox{{String: &s}},
Expand All @@ -513,6 +524,17 @@ type DoubleQuotedString struct {
Value []InterpolationBox `json:"value"`
}

func (s *DoubleQuotedString) Coalesce() {
var b strings.Builder
for _, box := range s.Value {
if box.String == nil {
break
}
b.WriteString(*box.String)
}
s.SetString(b.String())
}

func FlatDoubleQuotedString(s string) *DoubleQuotedString {
return &DoubleQuotedString{
Value: []InterpolationBox{{String: &s}},
Expand Down
6 changes: 4 additions & 2 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
}
}
return
} else if f.Name == "vars" {
return
} else if isReserved {
c.compileReserved(&obj.Attributes, f)
return
Expand Down Expand Up @@ -329,7 +331,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
Scope: fr.Context.Scope,
ScopeAST: fr.Context.ScopeAST,
}
if fr.Context.ScopeMap != nil {
if fr.Context.ScopeMap != nil && !d2ir.IsVar(fr.Context.ScopeMap) {
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(fr.Context.ScopeMap))
r.ScopeObj = obj.Graph.Root.EnsureChild(scopeObjIDA)
}
Expand Down Expand Up @@ -725,7 +727,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
Scope: er.Context.Scope,
ScopeAST: er.Context.ScopeAST,
}
if er.Context.ScopeMap != nil {
if er.Context.ScopeMap != nil && !d2ir.IsVar(er.Context.ScopeMap) {
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(er.Context.ScopeMap))
r.ScopeObj = edge.Src.Graph.Root.EnsureChild(scopeObjIDA)
}
Expand Down
Loading

0 comments on commit 4adfb22

Please sign in to comment.