Skip to content

Commit

Permalink
Merge pull request #834 from alixander/forbid-illegal-connections
Browse files Browse the repository at this point in the history
Forbid illegal children in sql_table and class
  • Loading branch information
alixander authored Feb 17, 2023
2 parents 7c08f51 + 91ee64f commit d369371
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 377 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
- Fixes edge case where layouts with dagre show a connection from the bottom side of shapes being slightly disconnected from the shape. [#820](https://github.com/terrastruct/d2/pull/820)
- Fixes rare compiler bug when using underscores in edges to create objects across containers. [#824](https://github.com/terrastruct/d2/pull/824)
- Fixes rare possibility of rendered connections being hidden or cut off. [#828](https://github.com/terrastruct/d2/pull/828)
- Creating nested children within `sql_table` and `class` shapes are now prevented (caused confusion when accidentally done). [#834](https://github.com/terrastruct/d2/pull/834)
11 changes: 11 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
return
}

if obj.Parent != nil {
if obj.Parent.Attributes.Shape.Value == d2target.ShapeSQLTable {
c.errorf(f.LastRef().AST(), "sql_table columns cannot have children")
return
}
if obj.Parent.Attributes.Shape.Value == d2target.ShapeClass {
c.errorf(f.LastRef().AST(), "class fields cannot have children")
return
}
}

obj = obj.EnsureChild(d2graphIDA([]string{f.Name}))
if f.Primary() != nil {
c.compileLabel(obj.Attributes, f)
Expand Down
30 changes: 30 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,36 @@ d2/testdata/d2compiler/TestCompile/errors/reserved_icon_style.d2:2:9: near key "
shape: sql_table
x: {p -> q}
}`,
expErr: `d2/testdata/d2compiler/TestCompile/edge_in_column.d2:3:7: sql_table columns cannot have children
d2/testdata/d2compiler/TestCompile/edge_in_column.d2:3:12: sql_table columns cannot have children`,
},
{
name: "no-nested-columns-sql",

text: `x: {
shape: sql_table
a -- b.b
}`,
expErr: `d2/testdata/d2compiler/TestCompile/no-nested-columns-sql.d2:3:10: sql_table columns cannot have children`,
},
{
name: "no-nested-columns-sql-2",

text: `x: {
shape: sql_table
a
}
x.a.b`,
expErr: `d2/testdata/d2compiler/TestCompile/no-nested-columns-sql-2.d2:5:5: sql_table columns cannot have children`,
},
{
name: "no-nested-columns-class",

text: `x: {
shape: class
a.a
}`,
expErr: `d2/testdata/d2compiler/TestCompile/no-nested-columns-class.d2:3:5: class fields cannot have children`,
},
{
name: "edge_to_style",
Expand Down
Loading

0 comments on commit d369371

Please sign in to comment.