diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 8a3224aab..bd245c08c 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -6005,6 +6005,32 @@ c assert.Equal(t, "hello", g.Edges[0].Label.Value) }, }, + { + name: "glob-edge-filter", + run: func(t *testing.T) { + g, _ := assertCompile(t, ` +x -> y: {class: foo} +a -> b + + +(** -> **)[*]: { + &class: foo + source-arrowhead: 1 + target-arrowhead: * { + shape: diamond + } +} +`, ``) + assert.Equal(t, 2, len(g.Edges)) + assert.Equal(t, "(x -> y)[0]", g.Edges[0].AbsID()) + assert.Equal(t, "(a -> b)[0]", g.Edges[1].AbsID()) + assert.Equal(t, "1", g.Edges[0].SrcArrowhead.Label.Value) + assert.Equal(t, (*d2graph.Attributes)(nil), g.Edges[1].SrcArrowhead) + assert.Equal(t, "diamond", g.Edges[0].DstArrowhead.Shape.Value) + assert.Equal(t, "*", g.Edges[0].DstArrowhead.Label.Value) + assert.Equal(t, (*d2graph.Attributes)(nil), g.Edges[1].DstArrowhead) + }, + }, { name: "unsuspend-edge-filter", run: func(t *testing.T) { diff --git a/d2ir/compile.go b/d2ir/compile.go index 6467fef1a..eef3e16ec 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -939,10 +939,13 @@ func (c *compiler) ampersandFilter(refctx *RefContext) bool { return dstPath == filterValue default: - f := refctx.ScopeMap.Parent().(*Field) - propName := refctx.Key.Key.Last().ScalarString() - value := refctx.Key.Value.ScalarBox().Unbox().ScalarString() - return c._ampersandPropertyFilter(propName, value, f, refctx.Key) + parent := refctx.ScopeMap.Parent() + if field, ok := parent.(*Field); ok { + propName := refctx.Key.Key.Last().ScalarString() + value := refctx.Key.Value.ScalarBox().Unbox().ScalarString() + return c._ampersandPropertyFilter(propName, value, field, refctx.Key) + } + return false } } for _, f := range fa { diff --git a/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.exp.json b/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.exp.json new file mode 100644 index 000000000..fdd95107c --- /dev/null +++ b/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.exp.json @@ -0,0 +1,666 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,0:0:0-12:0:133", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:20:21", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:6:7", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:1:2", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:5:6-1:6:7", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:5:6-1:6:7", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:8:9-1:20:21", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:9:10-1:19:20", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:9:10-1:14:15", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:9:10-1:14:15", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:16:17-1:19:20", + "value": [ + { + "string": "foo", + "raw_string": "foo" + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:6:28", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:6:28", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:1:23", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:1:23", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:5:27-2:6:28", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:5:27-2:6:28", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:0:31-11:1:132", + "edges": [ + { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:1:32-5:9:40", + "src": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:1:32-5:3:34", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:1:32-5:3:34", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:7:38-5:9:40", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:7:38-5:9:40", + "value": [ + { + "string": "**", + "raw_string": "**" + } + ], + "pattern": [ + "*", + "", + "*" + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "edge_index": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:10:41-5:13:44", + "int": null, + "glob": true + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,5:15:46-11:1:132", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,6:2:50-6:13:61", + "ampersand": true, + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,6:3:51-6:8:56", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,6:3:51-6:8:56", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,6:10:58-6:13:61", + "value": [ + { + "string": "foo", + "raw_string": "foo" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,7:2:64-7:21:83", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,7:2:64-7:18:80", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,7:2:64-7:18:80", + "value": [ + { + "string": "source-arrowhead", + "raw_string": "source-arrowhead" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "number": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,7:20:82-7:21:83", + "raw": "1", + "value": "1" + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,8:2:86-10:3:130", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,8:2:86-8:18:102", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,8:2:86-8:18:102", + "value": [ + { + "string": "target-arrowhead", + "raw_string": "target-arrowhead" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,8:20:104-8:21:105", + "value": [ + { + "string": "*", + "raw_string": "*" + } + ], + "pattern": [ + "*" + ] + } + }, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,8:22:106-10:3:130", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,9:4:112-9:18:126", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,9:4:112-9:9:117", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,9:4:112-9:9:117", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,9:11:119-9:18:126", + "value": [ + { + "string": "diamond", + "raw_string": "diamond" + } + ] + } + } + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": [ + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "srcArrowhead": { + "label": { + "value": "1" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "dst_arrow": true, + "dstArrowhead": { + "label": { + "value": "*" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "diamond" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "references": [ + { + "map_key_edge_index": 0 + }, + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null, + "classes": [ + "foo" + ] + }, + "zIndex": 0 + }, + { + "index": 0, + "isCurve": false, + "src_arrow": false, + "dst_arrow": true, + "references": [ + { + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "objects": [ + { + "id": "x", + "id_val": "x", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:0:1-1:1:2", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:5:6-1:6:7", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,1:5:6-1:6:7", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:1:23", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:0:22-2:1:23", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:5:27-2:6:28", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/globs/glob-edge-filter.d2,2:5:27-2:6:28", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "iconStyle": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +}