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

implement arrays for classes #1256

Merged
merged 1 commit into from
Apr 27, 2023
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
@@ -1,5 +1,6 @@
#### Features 🚀

- `class` field now accepts arrays. See [docs](TODO). [#1256](https://github.com/terrastruct/d2/pull/1256)
- Pill shape is implemented with rectangles of large border radius. Thanks @Poivey ! [#1006](https://github.com/terrastruct/d2/pull/1006)

#### Improvements 🧹
Expand Down
58 changes: 48 additions & 10 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,25 @@ func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) {
func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
class := m.GetField("class")
if class != nil {
className := class.Primary()
if className == nil {
c.errorf(class.LastRef().AST(), "class missing value")
var classNames []string
if class.Primary() != nil {
classNames = append(classNames, class.Primary().String())
} else if class.Composite != nil {
if arr, ok := class.Composite.(*d2ir.Array); ok {
for _, class := range arr.Values {
if scalar, ok := class.(*d2ir.Scalar); ok {
classNames = append(classNames, scalar.Value.ScalarString())
} else {
c.errorf(class.LastPrimaryKey(), "invalid value in array")
}
}
}
} else {
classMap := m.GetClassMap(className.String())
c.errorf(class.LastRef().AST(), "class missing value")
}

for _, className := range classNames {
classMap := m.GetClassMap(className)
if classMap != nil {
c.compileMap(obj, classMap)
}
Expand Down Expand Up @@ -292,7 +306,7 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {

func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
if f.Primary() == nil {
if f.Composite != nil {
if f.Composite != nil && !strings.EqualFold(f.Name, "class") {
c.errorf(f.LastPrimaryKey(), "reserved field %v does not accept composite", f.Name)
}
return
Expand Down Expand Up @@ -462,7 +476,17 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.HorizontalGap.Value = scalar.ScalarString()
attrs.HorizontalGap.MapKey = f.LastPrimaryKey()
case "class":
attrs.Classes = append(attrs.Classes, scalar.ScalarString())
if f.Primary() != nil {
attrs.Classes = append(attrs.Classes, scalar.ScalarString())
} else if f.Composite != nil {
if arr, ok := f.Composite.(*d2ir.Array); ok {
for _, class := range arr.Values {
if scalar, ok := class.(*d2ir.Scalar); ok {
attrs.Classes = append(attrs.Classes, scalar.Value.ScalarString())
}
}
}
}
case "classes":
}

Expand Down Expand Up @@ -577,11 +601,25 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
func (c *compiler) compileEdgeMap(edge *d2graph.Edge, m *d2ir.Map) {
class := m.GetField("class")
if class != nil {
className := class.Primary()
if className == nil {
c.errorf(class.LastRef().AST(), "class missing value")
var classNames []string
if class.Primary() != nil {
classNames = append(classNames, class.Primary().String())
} else if class.Composite != nil {
if arr, ok := class.Composite.(*d2ir.Array); ok {
for _, class := range arr.Values {
if scalar, ok := class.(*d2ir.Scalar); ok {
classNames = append(classNames, scalar.Value.ScalarString())
} else {
c.errorf(class.LastPrimaryKey(), "invalid value in array")
}
}
}
} else {
classMap := m.GetClassMap(className.String())
c.errorf(class.LastRef().AST(), "class missing value")
}

for _, className := range classNames {
classMap := m.GetClassMap(className)
if classMap != nil {
c.compileEdgeMap(edge, classMap)
}
Expand Down
29 changes: 29 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,35 @@ nostar -> 1star: { class: path }
tassert.Equal(t, "then", g.Edges[0].Label.Value)
},
},
{
name: "array-classes",
text: `classes: {
dragon_ball: {
label: ""
shape: circle
style.fill: orange
}
path: {
label: "then"
style.stroke-width: 4
}
path2: {
style.stroke-width: 2
}
}
nostar: { class: [dragon_ball; path] }
1star: { class: [path; dragon_ball] }

nostar -> 1star: { class: [path; path2] }
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "then", g.Objects[0].Label.Value)
tassert.Equal(t, "", g.Objects[1].Label.Value)
tassert.Equal(t, "circle", g.Objects[0].Shape.Value)
tassert.Equal(t, "circle", g.Objects[1].Shape.Value)
tassert.Equal(t, "2", g.Edges[0].Style.StrokeWidth.Value)
},
},
{
name: "reordered-classes",
text: `classes: {
Expand Down
18 changes: 18 additions & 0 deletions e2etests/stable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,24 @@ nostar: { class: dragon_ball }

nostar -> 1star: { class: path }
1star -> 2star: { class: path }
`,
},
{
name: "array-classes",
script: `classes: {
button: {
style.border-radius: 999
style.stroke: black
}
success: {
style.fill: "#90EE90"
}
error: {
style.fill: "#EA9999"
}
}
yay: Successful { class: [button; success] }
nay: Failure { class: [button; error] }
`,
},
{
Expand Down
130 changes: 130 additions & 0 deletions e2etests/testdata/stable/array-classes/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/stable/array-classes/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.
Loading