Skip to content

Commit

Permalink
Merge pull request #2390 from alixander/fix-keyword-merging
Browse files Browse the repository at this point in the history
d2ir: fix key quoting issues
  • Loading branch information
alixander authored Feb 28, 2025
2 parents bf543c3 + 82b3336 commit 80f560a
Show file tree
Hide file tree
Showing 5 changed files with 1,057 additions and 3 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,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

0 comments on commit 80f560a

Please sign in to comment.