Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vars #1473

Merged
merged 40 commits into from
Jul 13, 2023
Merged

Vars #1473

Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c9793b9
label works
alixander Jul 11, 2023
05802ed
edge label working
alixander Jul 11, 2023
fd70b5e
layers work
alixander Jul 11, 2023
7269ee1
add overlay tests
alixander Jul 11, 2023
acdbfdd
add replace tests
alixander Jul 11, 2023
0347dff
more tests
alixander Jul 11, 2023
08cc907
more test
alixander Jul 11, 2023
e7e2566
save
alixander Jul 11, 2023
090d10e
handle interpolation
alixander Jul 11, 2023
741a9aa
cleanup
alixander Jul 11, 2023
b193b9b
cleanup
alixander Jul 11, 2023
22bd50a
tests
alixander Jul 11, 2023
78e9e45
only coalesce if subbed
alixander Jul 11, 2023
a6125c4
cleanup
alixander Jul 11, 2023
85cf491
quotes
alixander Jul 11, 2023
4a9327e
non-root err check
alixander Jul 11, 2023
92d87b5
new implementation
alixander Jul 12, 2023
af69e6f
edge cases
alixander Jul 12, 2023
5d4d9c5
import tests
alixander Jul 12, 2023
67a06b6
var-in-var
alixander Jul 12, 2023
7091c58
changelog
alixander Jul 12, 2023
63dbaf0
pr changes
alixander Jul 12, 2023
d06e395
overlayVars refactor
alixander Jul 12, 2023
85bfad1
compile composites
alixander Jul 12, 2023
4b7b636
spread
alixander Jul 12, 2023
98f79e2
arrays
alixander Jul 13, 2023
301bc8d
add precedent test
alixander Jul 13, 2023
ef11d47
null
alixander Jul 13, 2023
06ed41e
remove dead code
alixander Jul 13, 2023
ffdb512
err msg
alixander Jul 13, 2023
780345b
fix double quote primary
alixander Jul 13, 2023
eba41af
use scalarstring
alixander Jul 13, 2023
a54907e
remove overlayVars
alixander Jul 13, 2023
fb0c0d2
nested null
alixander Jul 13, 2023
4847714
fix compiler creating vars
alixander Jul 13, 2023
29af923
fix no-primary-composite
alixander Jul 13, 2023
f12f9ca
move edge check
alixander Jul 13, 2023
e95f619
fix import test
alixander Jul 13, 2023
d275a45
allow edges in vars
alixander Jul 13, 2023
da0d245
rm commented code
alixander Jul 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 9 additions & 2 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
}
}
return
} else if f.Name == "vars" {
// if f.Map() != nil {
alixander marked this conversation as resolved.
Show resolved Hide resolved
// if len(f.Map().Edges) > 0 {
// c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge")
// }
// }
return
} else if isReserved {
c.compileReserved(&obj.Attributes, f)
return
Expand Down Expand Up @@ -329,7 +336,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) {
nhooyr marked this conversation as resolved.
Show resolved Hide resolved
scopeObjIDA := d2graphIDA(d2ir.BoardIDA(fr.Context.ScopeMap))
r.ScopeObj = obj.Graph.Root.EnsureChild(scopeObjIDA)
}
Expand Down Expand Up @@ -725,7 +732,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