diff --git a/d2ir/import_test.go b/d2ir/import_test.go index e53f4daed..856975af4 100644 --- a/d2ir/import_test.go +++ b/d2ir/import_test.go @@ -3,6 +3,7 @@ package d2ir_test import ( "testing" + "oss.terrastruct.com/d2/d2ir" "oss.terrastruct.com/util-go/assert" ) @@ -26,7 +27,7 @@ label: meow`, }, }, { - name: "nested", + name: "nested/map", run: func(t testing.TB) { m, err := compileFS(t, "index.d2", map[string]string{ "index.d2": "x: @x.y", @@ -42,6 +43,33 @@ label: meow`, assertQuery(t, m, 0, 0, "meow", "x.label") }, }, + { + name: "nested/array", + run: func(t testing.TB) { + m, err := compileFS(t, "index.d2", map[string]string{ + "index.d2": "x: @x.y", + "x.d2": `y: [1, 2]`, + }) + assert.Success(t, err) + assertQuery(t, m, 1, 0, nil, "") + x := assertQuery(t, m, 0, 0, nil, "x") + xf, ok := x.(*d2ir.Field) + assert.True(t, ok) + assert.Equal(t, `[1, 2]`, xf.Composite.String()) + }, + }, + { + name: "nested/scalar", + run: func(t testing.TB) { + m, err := compileFS(t, "index.d2", map[string]string{ + "index.d2": "x: @x.y", + "x.d2": `y: meow`, + }) + assert.Success(t, err) + assertQuery(t, m, 1, 0, nil, "") + assertQuery(t, m, 0, 0, "meow", "x") + }, + }, { name: "spread", run: func(t testing.TB) { diff --git a/testdata/d2ir/TestCompile/imports/nested/array.exp.json b/testdata/d2ir/TestCompile/imports/nested/array.exp.json new file mode 100644 index 000000000..02624ba49 --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/nested/array.exp.json @@ -0,0 +1,105 @@ +{ + "fields": [ + { + "name": "x", + "composite": { + "values": [ + { + "value": { + "range": "x.d2,0:4:4-0:8:8", + "value": [ + { + "string": "1, 2", + "raw_string": "1, 2" + } + ] + } + } + ] + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:7:7", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "import": { + "range": "index.d2,0:3:3-0:7:7", + "spread": false, + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:4:4-0:5:5", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:6:6-0:7:7", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + } + } + } + } + } + ] + } + ], + "edges": null +} diff --git a/testdata/d2ir/TestCompile/imports/nested/map.exp.json b/testdata/d2ir/TestCompile/imports/nested/map.exp.json new file mode 100644 index 000000000..d6a74f941 --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/nested/map.exp.json @@ -0,0 +1,249 @@ +{ + "fields": [ + { + "name": "x", + "composite": { + "fields": [ + { + "name": "shape", + "primary": { + "value": { + "range": "x.d2,1:8:13-1:14:19", + "value": [ + { + "string": "circle", + "raw_string": "circle" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,1:1:6-1:6:11", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + }, + "key_path": { + "range": "x.d2,1:1:6-1:6:11", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:1:6-1:6:11", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,1:1:6-1:14:19", + "key": { + "range": "x.d2,1:1:6-1:6:11", + "path": [ + { + "unquoted_string": { + "range": "x.d2,1:1:6-1:6:11", + "value": [ + { + "string": "shape", + "raw_string": "shape" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,1:8:13-1:14:19", + "value": [ + { + "string": "circle", + "raw_string": "circle" + } + ] + } + } + } + } + } + ] + }, + { + "name": "label", + "primary": { + "value": { + "range": "x.d2,2:8:28-2:12:32", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + }, + "references": [ + { + "string": { + "range": "x.d2,2:1:21-2:6:26", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + }, + "key_path": { + "range": "x.d2,2:1:21-2:6:26", + "path": [ + { + "unquoted_string": { + "range": "x.d2,2:1:21-2:6:26", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "x.d2,2:1:21-2:12:32", + "key": { + "range": "x.d2,2:1:21-2:6:26", + "path": [ + { + "unquoted_string": { + "range": "x.d2,2:1:21-2:6:26", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "x.d2,2:8:28-2:12:32", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + } + } + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:7:7", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "import": { + "range": "index.d2,0:3:3-0:7:7", + "spread": false, + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:4:4-0:5:5", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:6:6-0:7:7", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + } + } + } + } + } + ] + } + ], + "edges": null +} diff --git a/testdata/d2ir/TestCompile/imports/nested/scalar.exp.json b/testdata/d2ir/TestCompile/imports/nested/scalar.exp.json new file mode 100644 index 000000000..5ab8f8f40 --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/nested/scalar.exp.json @@ -0,0 +1,101 @@ +{ + "fields": [ + { + "name": "x", + "primary": { + "value": { + "range": "x.d2,0:3:3-0:7:7", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:7:7", + "key": { + "range": "index.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "import": { + "range": "index.d2,0:3:3-0:7:7", + "spread": false, + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:4:4-0:5:5", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:6:6-0:7:7", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + } + } + } + } + } + ] + } + ], + "edges": null +}