From 2546618ebe137100037d508c4d7d935d4588a501 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 29 Oct 2024 20:38:21 -0600 Subject: [PATCH] fix setting board label with primary --- ci/release/changelogs/next.md | 1 + d2compiler/compile.go | 3 + d2compiler/compile_test.go | 18 + .../boards/board-label-primary.exp.json | 520 ++++++++++++++++++ .../TestCompile2/boards/isFolderOnly.exp.json | 2 +- 5 files changed, 543 insertions(+), 1 deletion(-) create mode 100644 testdata/d2compiler/TestCompile2/boards/board-label-primary.exp.json diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 7b05b6262..d8e48cd66 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -12,3 +12,4 @@ #### Bugfixes ⛑️ - Render: fixes edge case of a 3d shape with outside label being cut off [#2132](https://github.com/terrastruct/d2/pull/2132) +- Composition: labels for boards set with shorthand `x: y` was not applied [#2182](https://github.com/terrastruct/d2/pull/2182) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 426427833..517b226eb 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -129,6 +129,9 @@ func (c *compiler) compileBoardsField(g *d2graph.Graph, ir *d2ir.Map, fieldName g2.BaseAST = findFieldAST(g.BaseAST, f) } c.compileBoard(g2, m) + if f.Primary() != nil { + c.compileLabel(&g2.Root.Attributes, f) + } g2.Name = f.Name switch fieldName { case "layers": diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index adf16c22c..97a2c5f42 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3511,6 +3511,24 @@ steps: { assert.True(t, g.IsFolderOnly) }, }, + { + name: "board-label-primary", + run: func(t *testing.T) { + g, _ := assertCompile(t, `hi +layers: { + 1: one { + RJ + } + 2: { + label: two + RJ + } +} +`, "") + assert.Equal(t, "one", g.Layers[0].Root.Label.Value) + assert.Equal(t, "two", g.Layers[1].Root.Label.Value) + }, + }, { name: "no-inherit-label", run: func(t *testing.T) { diff --git a/testdata/d2compiler/TestCompile2/boards/board-label-primary.exp.json b/testdata/d2compiler/TestCompile2/boards/board-label-primary.exp.json new file mode 100644 index 000000000..b1ef6ce86 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/boards/board-label-primary.exp.json @@ -0,0 +1,520 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-10:0:70", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-0:2:2", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-0:2:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-0:2:2", + "value": [ + { + "string": "hi", + "raw_string": "hi" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,1:0:3-9:1:69", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,1:0:3-1:6:9", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,1:0:3-1:6:9", + "value": [ + { + "string": "layers", + "raw_string": "layers" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,1:8:11-9:1:69", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,2:2:15-4:3:34", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,2:2:15-2:3:16", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,2:2:15-2:3:16", + "value": [ + { + "string": "1", + "raw_string": "1" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,2:5:18-2:8:21", + "value": [ + { + "string": "one", + "raw_string": "one" + } + ] + } + }, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,2:9:22-4:3:34", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,3:4:28-3:6:30", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,3:4:28-3:6:30", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,3:4:28-3:6:30", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,5:2:37-8:3:67", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,5:2:37-5:3:38", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,5:2:37-5:3:38", + "value": [ + { + "string": "2", + "raw_string": "2" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,5:5:40-8:3:67", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,6:4:46-6:14:56", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,6:4:46-6:9:51", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,6:4:46-6:9:51", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,6:11:53-6:14:56", + "value": [ + { + "string": "two", + "raw_string": "two" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,7:4:61-7:6:63", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,7:4:61-7:6:63", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,7:4:61-7:6:63", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "hi", + "id_val": "hi", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-0:2:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,0:0:0-0:2:2", + "value": [ + { + "string": "hi", + "raw_string": "hi" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "hi" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "layers": [ + { + "name": "1", + "isFolderOnly": false, + "ast": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "one" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "RJ", + "id_val": "RJ", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,3:4:28-3:6:30", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,3:4:28-3:6:30", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "RJ" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + { + "name": "2", + "isFolderOnly": false, + "ast": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "label" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,6:11:53-6:14:56", + "value": [ + { + "string": "two", + "raw_string": "two" + } + ] + } + }, + "value": {} + } + }, + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "two" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "RJ", + "id_val": "RJ", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,7:4:61-7:6:63", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/board-label-primary.d2,7:4:61-7:6:63", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "RJ" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + } + ] + }, + "err": null +} diff --git a/testdata/d2compiler/TestCompile2/boards/isFolderOnly.exp.json b/testdata/d2compiler/TestCompile2/boards/isFolderOnly.exp.json index 8cdf54d08..8e093a0f6 100644 --- a/testdata/d2compiler/TestCompile2/boards/isFolderOnly.exp.json +++ b/testdata/d2compiler/TestCompile2/boards/isFolderOnly.exp.json @@ -986,7 +986,7 @@ "id_val": "", "attributes": { "label": { - "value": "" + "value": "one two three" }, "labelDimensions": { "width": 0,