From 12b166b3981437cc210664b28a20fc2b3d211bc3 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 5 Dec 2023 13:12:36 -0800 Subject: [PATCH] globs: fix imported case --- ci/release/changelogs/next.md | 1 + d2ir/compile.go | 6 +- d2ir/import_test.go | 15 + .../imports/pattern-value.exp.json | 447 ++++++++++++++++++ 4 files changed, 467 insertions(+), 2 deletions(-) create mode 100644 testdata/d2ir/TestCompile/imports/pattern-value.exp.json diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 46f2e9d092..eb9a486553 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -33,3 +33,4 @@ - Fixes incorrect grid cell positioning when the grid has a shape set and fixes content sometimes escaping circle shapes. [#1734](https://github.com/terrastruct/d2/pull/1734) - Fixes content sometimes escaping cloud shapes. [#1736](https://github.com/terrastruct/d2/pull/1736) - Fixes panic using a glob filter (e.g. `&a`) outside globs. [#1748](https://github.com/terrastruct/d2/pull/1748) +- Fixes glob keys with import values (e.g. `user*: @lib/user`). [#1755](https://github.com/terrastruct/d2/pull/1755) diff --git a/d2ir/compile.go b/d2ir/compile.go index 7fce0c828e..6e3f9b2496 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -419,8 +419,10 @@ func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool { func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) { var globs []*globContext if len(c.globContextStack) > 0 { - previousGlobs := c.globContextStack[len(c.globContextStack)-1] - if NodeBoardKind(dst) == BoardLayer { + previousGlobs := c.globContexts() + // A root layer with existing glob context stack implies it's an import + // In which case, the previous globs should be inherited (the else block) + if NodeBoardKind(dst) == BoardLayer && !dst.Root() { for _, g := range previousGlobs { if g.refctx.Key.HasTripleGlob() { globs = append(globs, g.prefixed(dst)) diff --git a/d2ir/import_test.go b/d2ir/import_test.go index 8e94c5a5b1..072abbf515 100644 --- a/d2ir/import_test.go +++ b/d2ir/import_test.go @@ -184,6 +184,21 @@ label: meow`, assertQuery(t, m, 0, 0, 1, "hi") }, }, + { + name: "pattern-value", + run: func(t testing.TB) { + _, err := compileFS(t, "index.d2", map[string]string{ + "index.d2": `userWebsite +userMobile + +user*: @x +`, + "x.d2": `shape: person +label: meow`, + }) + assert.Success(t, err) + }, + }, } runa(t, tca) diff --git a/testdata/d2ir/TestCompile/imports/pattern-value.exp.json b/testdata/d2ir/TestCompile/imports/pattern-value.exp.json new file mode 100644 index 0000000000..d5f4c5203a --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/pattern-value.exp.json @@ -0,0 +1,447 @@ +{ + "fields": [ + { + "name": "userWebsite", + "composite": { + "fields": [ + { + "name": "shape", + "primary": { + "value": { + "range": "x.d2,0:7:7-0:13:13", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + }, + "key_path": { + "range": "x.d2,0:0:0-0:5:5", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,0:0:0-0:13:13", + "key": { + "range": "x.d2,0:0:0-0:5:5", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,0:7:7-0:13:13", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + } + } + }, + "due_to_glob": true, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "label", + "primary": { + "value": { + "range": "x.d2,1:7:21-1:11:25", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + }, + "key_path": { + "range": "x.d2,1:0:14-1:5:19", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,1:0:14-1:11:25", + "key": { + "range": "x.d2,1:0:14-1:5:19", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,1:7:21-1:11:25", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + } + }, + "due_to_glob": true, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:11:11", + "value": [ + { + "string": "userWebsite", + "raw_string": "userWebsite" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:11:11", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:11:11", + "value": [ + { + "string": "userWebsite", + "raw_string": "userWebsite" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:11:11", + "key": { + "range": "index.d2,0:0:0-0:11:11", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:11:11", + "value": [ + { + "string": "userWebsite", + "raw_string": "userWebsite" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "userMobile", + "composite": { + "fields": [ + { + "name": "shape", + "primary": { + "value": { + "range": "x.d2,0:7:7-0:13:13", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + }, + "key_path": { + "range": "x.d2,0:0:0-0:5:5", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,0:0:0-0:13:13", + "key": { + "range": "x.d2,0:0:0-0:5:5", + "path": [ + { + "unquoted_string": { + "range": "x.d2,0:0:0-0:5:5", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,0:7:7-0:13:13", + "value": [ + { + "string": "person", + "raw_string": "person" + } + ] + } + } + } + }, + "due_to_glob": true, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "label", + "primary": { + "value": { + "range": "x.d2,1:7:21-1:11:25", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + }, + "key_path": { + "range": "x.d2,1:0:14-1:5:19", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,1:0:14-1:11:25", + "key": { + "range": "x.d2,1:0:14-1:5:19", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:0:14-1:5:19", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,1:7:21-1:11:25", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + } + }, + "due_to_glob": true, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "index.d2,1:0:12-1:10:22", + "value": [ + { + "string": "userMobile", + "raw_string": "userMobile" + } + ] + }, + "key_path": { + "range": "index.d2,1:0:12-1:10:22", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:12-1:10:22", + "value": [ + { + "string": "userMobile", + "raw_string": "userMobile" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,1:0:12-1:10:22", + "key": { + "range": "index.d2,1:0:12-1:10:22", + "path": [ + { + "unquoted_string": { + "range": "index.d2,1:0:12-1:10:22", + "value": [ + { + "string": "userMobile", + "raw_string": "userMobile" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": null +}