From 366a36df6776fefbad3f7978da7712869e6082e5 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 13 Mar 2023 20:07:13 -0700 Subject: [PATCH] compiler --- d2compiler/compile.go | 2 + d2compiler/compile_test.go | 19 ++ d2graph/d2graph.go | 15 ++ .../TestCompile/fill-pattern.exp.json | 178 ++++++++++++++++++ .../TestCompile/invalid-fill-pattern.exp.json | 12 ++ 5 files changed, 226 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/fill-pattern.exp.json create mode 100644 testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 95efdfaad..0654e1567 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -392,6 +392,8 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) { attrs.Style.Stroke = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} case "fill": attrs.Style.Fill = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} + case "fill-pattern": + attrs.Style.FillPattern = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} case "stroke-width": attrs.Style.StrokeWidth = &d2graph.Scalar{MapKey: f.LastPrimaryKey()} case "stroke-dash": diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 159e64289..f28e8d3f9 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -242,6 +242,25 @@ containers: { } }, }, + { + name: "fill-pattern", + text: `x: { + style: { + fill-pattern: dots + } +} +`, + }, + { + name: "invalid-fill-pattern", + text: `x: { + style: { + fill-pattern: ddots + } +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected "fill-pattern" to be one of: dots`, + }, { name: "shape_unquoted_hex", diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 670d45b93..b8955a878 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -150,6 +150,7 @@ type Style struct { Opacity *Scalar `json:"opacity,omitempty"` Stroke *Scalar `json:"stroke,omitempty"` Fill *Scalar `json:"fill,omitempty"` + FillPattern *Scalar `json:"fillPattern,omitempty"` StrokeWidth *Scalar `json:"strokeWidth,omitempty"` StrokeDash *Scalar `json:"strokeDash,omitempty"` BorderRadius *Scalar `json:"borderRadius,omitempty"` @@ -194,6 +195,14 @@ func (s *Style) Apply(key, value string) error { return errors.New(`expected "fill" to be a valid named color ("orange") or a hex code ("#f0ff3a")`) } s.Fill.Value = value + case "fill-pattern": + if s.FillPattern == nil { + break + } + if !go2.Contains(FillPatterns, strings.ToLower(value)) { + return fmt.Errorf(`expected "fill-pattern" to be one of: %s`, strings.Join(FillPatterns, ",")) + } + s.FillPattern.Value = value case "stroke-width": if s.StrokeWidth == nil { break @@ -1499,6 +1508,7 @@ var StyleKeywords = map[string]struct{}{ "opacity": {}, "stroke": {}, "fill": {}, + "fill-pattern": {}, "stroke-width": {}, "stroke-dash": {}, "border-radius": {}, @@ -1540,6 +1550,11 @@ var NearConstantsArray = []string{ } var NearConstants map[string]struct{} +var FillPatterns = []string{ + // TODO add lined later + "dots", +} + // BoardKeywords contains the keywords that create new boards. var BoardKeywords = map[string]struct{}{ "layers": {}, diff --git a/testdata/d2compiler/TestCompile/fill-pattern.exp.json b/testdata/d2compiler/TestCompile/fill-pattern.exp.json new file mode 100644 index 000000000..e25121afe --- /dev/null +++ b/testdata/d2compiler/TestCompile/fill-pattern.exp.json @@ -0,0 +1,178 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-5:0:44", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-4:1:43", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:3:3-4:0:42", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-3:3:41", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11", + "value": [ + { + "string": "style", + "raw_string": "style" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:8:13-3:2:40", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:22:37", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31", + "value": [ + { + "string": "fill-pattern", + "raw_string": "fill-pattern" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:18:33-2:22:37", + "value": [ + { + "string": "dots", + "raw_string": "dots" + } + ] + } + } + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "x", + "id_val": "x", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +} diff --git a/testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json b/testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json new file mode 100644 index 000000000..3bd358bf9 --- /dev/null +++ b/testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2,2:18:33-2:23:38", + "errmsg": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected \"fill-pattern\" to be one of: dots" + } + ] + } +}