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

d2ir: fix key quoting issues #2390

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -22,4 +22,5 @@
- fixes panic when `sql_shape` shape value had mixed casing [#2349](https://github.com/terrastruct/d2/pull/2349)
- fixes support for `center` in `d2-config` [#2360](https://github.com/terrastruct/d2/pull/2360)
- fixes panic when comment lines appear in arrays [#2378](https://github.com/terrastruct/d2/pull/2378)
- fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390)
- CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370)
21 changes: 21 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5450,6 +5450,27 @@ a -> b: {
assert.Equal(t, "This is a->b", g.Edges[0].LegendLabel.Value)
},
},
{
name: "merge-glob-values",
run: func(t *testing.T) {
assertCompile(t, `
"a"
*.style.stroke-width: 2
*.style.font-size: 14
a.width: 339
`, ``)
},
},
{
name: "mixed-edge-quoting",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
"a"."b"."c"."z1" -> "a"."b"."c"."z2"
`, ``)
assert.Equal(t, 5, len(g.Objects))
},
},
}

for _, tc := range tca {
Expand Down
13 changes: 10 additions & 3 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,10 @@ func (m *Map) getField(ida []d2ast.String) *Field {
if !strings.EqualFold(f.Name.ScalarString(), s.ScalarString()) {
continue
}
if f.Name.IsUnquoted() != s.IsUnquoted() {
continue
if _, isReserved := d2ast.ReservedKeywords[strings.ToLower(s.ScalarString())]; isReserved {
if f.Name.IsUnquoted() != s.IsUnquoted() {
continue
}
}
if len(rest) == 0 {
return f
Expand Down Expand Up @@ -912,9 +914,14 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}

for _, f := range m.Fields {
if !(f.Name != nil && strings.EqualFold(f.Name.ScalarString(), head.ScalarString()) && f.Name.IsUnquoted() == head.IsUnquoted()) {
if !(f.Name != nil && strings.EqualFold(f.Name.ScalarString(), head.ScalarString())) {
continue
}
if _, isReserved := d2ast.ReservedKeywords[strings.ToLower(f.Name.ScalarString())]; isReserved {
if f.Name.IsUnquoted() != head.IsUnquoted() {
continue
}
}

// Don't add references for fake common KeyPath from trimCommon in CreateEdge.
if refctx != nil {
Expand Down
295 changes: 295 additions & 0 deletions testdata/d2compiler/TestCompile2/globs/merge-glob-values.exp.json

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

Loading