Skip to content

Commit

Permalink
Merge pull request #2349 from alixander/sql-shape-panic
Browse files Browse the repository at this point in the history
d2compiler: fix panic when mixing casing on shape value
  • Loading branch information
alixander authored Feb 13, 2025
2 parents ee06089 + 68bdf03 commit e4a8fdc
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
#### Improvements 🧹

#### Bugfixes ⛑️

- Compiler: fixes panic when `sql_shape` shape value had mixed casing [#2349](https://github.com/terrastruct/d2/pull/2349)
12 changes: 7 additions & 5 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,14 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
c.compileLabel(attrs, f)
c.compilePosition(attrs, f)
case "shape":
in := d2target.IsShape(scalar.ScalarString())
_, isArrowhead := d2target.Arrowheads[scalar.ScalarString()]
shapeVal := strings.ToLower(scalar.ScalarString())
in := d2target.IsShape(shapeVal)
_, isArrowhead := d2target.Arrowheads[shapeVal]
if !in && !isArrowhead {
c.errorf(scalar, "unknown shape %q", scalar.ScalarString())
return
}
attrs.Shape.Value = scalar.ScalarString()
attrs.Shape.Value = shapeVal
if strings.EqualFold(attrs.Shape.Value, d2target.ShapeCode) {
// Explicit code shape is plaintext.
attrs.Language = d2target.ShapeText
Expand Down Expand Up @@ -596,11 +597,12 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Link.MapKey = f.LastPrimaryKey()
case "direction":
dirs := []string{"up", "down", "right", "left"}
if !go2.Contains(dirs, scalar.ScalarString()) {
val := strings.ToLower(scalar.ScalarString())
if !go2.Contains(dirs, val) {
c.errorf(scalar, `direction must be one of %v, got %q`, strings.Join(dirs, ", "), scalar.ScalarString())
return
}
attrs.Direction.Value = scalar.ScalarString()
attrs.Direction.Value = val
attrs.Direction.MapKey = f.LastPrimaryKey()
case "constraint":
if _, ok := scalar.(d2ast.String); !ok {
Expand Down
130 changes: 130 additions & 0 deletions e2etests/testdata/txtar/sql-casing-panic/dagre/board.exp.json

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

95 changes: 95 additions & 0 deletions e2etests/testdata/txtar/sql-casing-panic/dagre/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions e2etests/testdata/txtar/sql-casing-panic/elk/board.exp.json

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

Loading

0 comments on commit e4a8fdc

Please sign in to comment.