diff --git a/d2ir/compile.go b/d2ir/compile.go index 0f9587eed..358c4ba93 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -167,5 +167,28 @@ func (c *compiler) compileEdges(dst *Map, k *d2ast.Key) { } func (c *compiler) compileArray(dst *Array, a *d2ast.Array) { - panic(fmt.Sprintf("TODO")) + for _, an := range a.Nodes { + var irv Value + switch v := an.Unbox().(type) { + case *d2ast.Array: + ira := &Array{ + parent: dst, + } + c.compileArray(ira, v) + irv = ira + case *d2ast.Map: + irm := &Map{ + parent: dst, + } + c.compileMap(irm, v) + irv = irm + case d2ast.Scalar: + irv = &Scalar{ + parent: dst, + Value: v, + } + } + + dst.Values = append(dst.Values, irv) + } } diff --git a/d2ir/compile_test.go b/d2ir/compile_test.go index 7cb3d1df4..e1174d46c 100644 --- a/d2ir/compile_test.go +++ b/d2ir/compile_test.go @@ -15,119 +15,16 @@ import ( "oss.terrastruct.com/d2/d2parser" ) -type testCase struct { - name string - run func(testing.TB, *d2ir.Map) -} - func TestCompile(t *testing.T) { t.Parallel() - t.Run("roots", testCompileRoots) + t.Run("field", testCompileField) + t.Run("edge", testCompileEdge) } -func testCompileRoots(t *testing.T) { - t.Parallel() - - tca := []testCase{ - { - name: "field", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x`) - assert.Success(t, err) - assertField(t, m, 1, 0, nil) - - assertField(t, m, 0, 0, nil, "x") - }, - }, - { - name: "field/label", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x: yes`) - assert.Success(t, err) - assertField(t, m, 1, 0, nil) - - assertField(t, m, 0, 0, "yes", "x") - }, - }, - { - name: "field/label/nested", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x.y: yes`) - assert.Success(t, err) - assertField(t, m, 2, 0, nil) - - assertField(t, m, 1, 0, nil, "x") - assertField(t, m, 0, 0, "yes", "x", "y") - }, - }, - { - name: "primary", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x: yes { pqrs }`) - assert.Success(t, err) - assertField(t, m, 2, 0, nil) - - assertField(t, m, 1, 0, "yes", "x") - assertField(t, m, 0, 0, nil, "x", "pqrs") - }, - }, - { - name: "primary/nested", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x.y: yes { pqrs }`) - assert.Success(t, err) - assertField(t, m, 3, 0, nil) - - assertField(t, m, 2, 0, nil, "x") - assertField(t, m, 1, 0, "yes", "x", "y") - assertField(t, m, 0, 0, nil, "x", "y", "pqrs") - }, - }, - { - name: "edge", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x -> y`) - assert.Success(t, err) - assertField(t, m, 2, 1, nil) - assertEdge(t, m, 0, nil, `(x -> y)[0]`) - - assertField(t, m, 0, 0, nil, "x") - assertField(t, m, 0, 0, nil, "y") - }, - }, - { - name: "nested", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x.y -> z.p`) - assert.Success(t, err) - assertField(t, m, 4, 1, nil) - - assertField(t, m, 1, 0, nil, "x") - assertField(t, m, 0, 0, nil, "x", "y") - - assertField(t, m, 1, 0, nil, "z") - assertField(t, m, 0, 0, nil, "z", "p") - - assertEdge(t, m, 0, nil, "(x.y -> z.p)[0]") - }, - }, - { - name: "underscore_parent", - run: func(t testing.TB, m *d2ir.Map) { - err := parse(t, m, `x._ -> z`) - assert.Success(t, err) - assertField(t, m, 3, 1, nil) - - assertField(t, m, 0, 0, nil, "x") - assertField(t, m, 0, 0, nil, "z") - - assertEdge(t, m, 0, nil, "(x -> z)[0]") - }, - }, - } - - runa(t, tca) +type testCase struct { + name string + run func(testing.TB, *d2ir.Map) } func runa(t *testing.T, tca []testCase) { @@ -273,3 +170,136 @@ func makeScalar(v interface{}) *d2ir.Scalar { } return s } + +func testCompileField(t *testing.T) { + t.Parallel() + t.Run("primary", testCompileFieldPrimary) + tca := []testCase{ + { + name: "root", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x`) + assert.Success(t, err) + assertField(t, m, 1, 0, nil) + + assertField(t, m, 0, 0, nil, "x") + }, + }, + { + name: "label", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x: yes`) + assert.Success(t, err) + assertField(t, m, 1, 0, nil) + + assertField(t, m, 0, 0, "yes", "x") + }, + }, + { + name: "nested", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x.y: yes`) + assert.Success(t, err) + assertField(t, m, 2, 0, nil) + + assertField(t, m, 1, 0, nil, "x") + assertField(t, m, 0, 0, "yes", "x", "y") + }, + }, + { + name: "array", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x: [1;2;3;4]`) + assert.Success(t, err) + assertField(t, m, 1, 0, nil) + + f := assertField(t, m, 0, 0, nil, "x") + f_a, ok := f.Composite.(*d2ir.Array) + if !ok { + t.Fatalf("unexpected type: %T", f.Composite) + } else { + assert.Equal(t, 4, len(f_a.Values)) + } + }, + }, + } + runa(t, tca) +} + +func testCompileFieldPrimary(t *testing.T) { + t.Parallel() + tca := []testCase{ + { + name: "root", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x: yes { pqrs }`) + assert.Success(t, err) + assertField(t, m, 2, 0, nil) + + assertField(t, m, 1, 0, "yes", "x") + assertField(t, m, 0, 0, nil, "x", "pqrs") + }, + }, + { + name: "nested", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x.y: yes { pqrs }`) + assert.Success(t, err) + assertField(t, m, 3, 0, nil) + + assertField(t, m, 2, 0, nil, "x") + assertField(t, m, 1, 0, "yes", "x", "y") + assertField(t, m, 0, 0, nil, "x", "y", "pqrs") + }, + }, + } + runa(t, tca) +} + +func testCompileEdge(t *testing.T) { + t.Parallel() + tca := []testCase{ + { + name: "edge", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x -> y`) + assert.Success(t, err) + assertField(t, m, 2, 1, nil) + assertEdge(t, m, 0, nil, `(x -> y)[0]`) + + assertField(t, m, 0, 0, nil, "x") + assertField(t, m, 0, 0, nil, "y") + }, + }, + { + name: "nested", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x.y -> z.p`) + assert.Success(t, err) + assertField(t, m, 4, 1, nil) + + assertField(t, m, 1, 0, nil, "x") + assertField(t, m, 0, 0, nil, "x", "y") + + assertField(t, m, 1, 0, nil, "z") + assertField(t, m, 0, 0, nil, "z", "p") + + assertEdge(t, m, 0, nil, "(x.y -> z.p)[0]") + }, + }, + { + name: "underscore", + run: func(t testing.TB, m *d2ir.Map) { + err := parse(t, m, `x._ -> z`) + assert.Success(t, err) + assertField(t, m, 3, 1, nil) + + assertField(t, m, 0, 0, nil, "x") + assertField(t, m, 0, 0, nil, "z") + + assertEdge(t, m, 0, nil, "(x -> z)[0]") + }, + }, + } + runa(t, tca) +} diff --git a/testdata/d2ir/TestApply/simple/edge.exp.json b/testdata/d2ir/TestApply/simple/edge.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestApply/simple/edge.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/field/label.exp.json b/testdata/d2ir/TestApply/simple/field/label.exp.json deleted file mode 100644 index 0e70b7b57..000000000 --- a/testdata/d2ir/TestApply/simple/field/label.exp.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestApply/simple/field/label.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/nested.exp.json b/testdata/d2ir/TestApply/simple/nested.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestApply/simple/nested.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/one.exp.json b/testdata/d2ir/TestApply/simple/one.exp.json deleted file mode 100644 index 2dcf9ab44..000000000 --- a/testdata/d2ir/TestApply/simple/one.exp.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "fields": [ - { - "name": "x" - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/primary.exp.json b/testdata/d2ir/TestApply/simple/primary.exp.json deleted file mode 100644 index 8f512307c..000000000 --- a/testdata/d2ir/TestApply/simple/primary.exp.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestApply/simple/primary.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - }, - "composite": { - "fields": [ - { - "name": "pqrs" - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/primary/nested.exp.json b/testdata/d2ir/TestApply/simple/primary/nested.exp.json deleted file mode 100644 index ba3dcf560..000000000 --- a/testdata/d2ir/TestApply/simple/primary/nested.exp.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "composite": { - "fields": [ - { - "name": "y", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestApply/simple/primary/nested.d2,0:5:5-0:8:8", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - }, - "composite": { - "fields": [ - { - "name": "pqrs" - } - ], - "edges": null - } - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestApply/simple/underscore_parent.exp.json b/testdata/d2ir/TestApply/simple/underscore_parent.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestApply/simple/underscore_parent.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -} diff --git a/testdata/d2ir/TestCompile/roots/edge.exp.json b/testdata/d2ir/TestCompile/edge/edge.exp.json similarity index 100% rename from testdata/d2ir/TestCompile/roots/edge.exp.json rename to testdata/d2ir/TestCompile/edge/edge.exp.json diff --git a/testdata/d2ir/TestCompile/roots/nested.exp.json b/testdata/d2ir/TestCompile/edge/nested.exp.json similarity index 100% rename from testdata/d2ir/TestCompile/roots/nested.exp.json rename to testdata/d2ir/TestCompile/edge/nested.exp.json diff --git a/testdata/d2ir/TestCompile/roots/underscore_parent.exp.json b/testdata/d2ir/TestCompile/edge/underscore.exp.json similarity index 100% rename from testdata/d2ir/TestCompile/roots/underscore_parent.exp.json rename to testdata/d2ir/TestCompile/edge/underscore.exp.json diff --git a/testdata/d2ir/TestCompile/field/array.exp.json b/testdata/d2ir/TestCompile/field/array.exp.json new file mode 100644 index 000000000..4f45a4827 --- /dev/null +++ b/testdata/d2ir/TestCompile/field/array.exp.json @@ -0,0 +1,40 @@ +{ + "fields": [ + { + "name": "x", + "composite": { + "values": [ + { + "value": { + "range": "d2/testdata/d2ir/TestCompile/field/array.d2,0:4:4-0:5:5", + "raw": "1", + "value": "1" + } + }, + { + "value": { + "range": "d2/testdata/d2ir/TestCompile/field/array.d2,0:6:6-0:7:7", + "raw": "2", + "value": "2" + } + }, + { + "value": { + "range": "d2/testdata/d2ir/TestCompile/field/array.d2,0:8:8-0:9:9", + "raw": "3", + "value": "3" + } + }, + { + "value": { + "range": "d2/testdata/d2ir/TestCompile/field/array.d2,0:10:10-0:11:11", + "raw": "4", + "value": "4" + } + } + ] + } + } + ], + "edges": null +} diff --git a/testdata/d2ir/TestApply/simple/label.exp.json b/testdata/d2ir/TestCompile/field/label.exp.json similarity index 76% rename from testdata/d2ir/TestApply/simple/label.exp.json rename to testdata/d2ir/TestCompile/field/label.exp.json index fd2267e5a..9378defd0 100644 --- a/testdata/d2ir/TestApply/simple/label.exp.json +++ b/testdata/d2ir/TestCompile/field/label.exp.json @@ -4,7 +4,7 @@ "name": "x", "primary": { "value": { - "range": "d2/testdata/d2ir/TestApply/simple/label.d2,0:3:3-0:6:6", + "range": "d2/testdata/d2ir/TestCompile/field/label.d2,0:3:3-0:6:6", "value": [ { "string": "yes", diff --git a/testdata/d2ir/TestApply/simple/field/label/nested.exp.json b/testdata/d2ir/TestCompile/field/nested.exp.json similarity index 82% rename from testdata/d2ir/TestApply/simple/field/label/nested.exp.json rename to testdata/d2ir/TestCompile/field/nested.exp.json index fec6ca0c6..638232665 100644 --- a/testdata/d2ir/TestApply/simple/field/label/nested.exp.json +++ b/testdata/d2ir/TestCompile/field/nested.exp.json @@ -8,7 +8,7 @@ "name": "y", "primary": { "value": { - "range": "d2/testdata/d2ir/TestApply/simple/field/label/nested.d2,0:5:5-0:8:8", + "range": "d2/testdata/d2ir/TestCompile/field/nested.d2,0:5:5-0:8:8", "value": [ { "string": "yes", diff --git a/testdata/d2ir/TestCompile/roots/primary/nested.exp.json b/testdata/d2ir/TestCompile/field/primary/nested.exp.json similarity index 91% rename from testdata/d2ir/TestCompile/roots/primary/nested.exp.json rename to testdata/d2ir/TestCompile/field/primary/nested.exp.json index 8ae714eba..79063ae09 100644 --- a/testdata/d2ir/TestCompile/roots/primary/nested.exp.json +++ b/testdata/d2ir/TestCompile/field/primary/nested.exp.json @@ -8,7 +8,7 @@ "name": "y", "primary": { "value": { - "range": "d2/testdata/d2ir/TestCompile/roots/primary/nested.d2,0:5:5-0:8:8", + "range": "d2/testdata/d2ir/TestCompile/field/primary/nested.d2,0:5:5-0:8:8", "value": [ { "string": "yes", diff --git a/testdata/d2ir/TestApply/simple/primary#01.exp.json b/testdata/d2ir/TestCompile/field/primary/root.exp.json similarity index 81% rename from testdata/d2ir/TestApply/simple/primary#01.exp.json rename to testdata/d2ir/TestCompile/field/primary/root.exp.json index c1e536d08..cd2bb026c 100644 --- a/testdata/d2ir/TestApply/simple/primary#01.exp.json +++ b/testdata/d2ir/TestCompile/field/primary/root.exp.json @@ -4,7 +4,7 @@ "name": "x", "primary": { "value": { - "range": "d2/testdata/d2ir/TestApply/simple/primary#01.d2,0:3:3-0:6:6", + "range": "d2/testdata/d2ir/TestCompile/field/primary/root.d2,0:3:3-0:6:6", "value": [ { "string": "yes", diff --git a/testdata/d2ir/TestApply/simple/field.exp.json b/testdata/d2ir/TestCompile/field/root.exp.json similarity index 100% rename from testdata/d2ir/TestApply/simple/field.exp.json rename to testdata/d2ir/TestCompile/field/root.exp.json diff --git a/testdata/d2ir/TestCompile/roots/field.exp.json b/testdata/d2ir/TestCompile/roots/field.exp.json deleted file mode 100644 index 2dcf9ab44..000000000 --- a/testdata/d2ir/TestCompile/roots/field.exp.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "fields": [ - { - "name": "x" - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/roots/field/label.exp.json b/testdata/d2ir/TestCompile/roots/field/label.exp.json deleted file mode 100644 index 723e59be4..000000000 --- a/testdata/d2ir/TestCompile/roots/field/label.exp.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/roots/field/label.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/roots/field/label/nested.exp.json b/testdata/d2ir/TestCompile/roots/field/label/nested.exp.json deleted file mode 100644 index ce902b9a6..000000000 --- a/testdata/d2ir/TestCompile/roots/field/label/nested.exp.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "composite": { - "fields": [ - { - "name": "y", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/roots/field/label/nested.d2,0:5:5-0:8:8", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - } - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/roots/primary.exp.json b/testdata/d2ir/TestCompile/roots/primary.exp.json deleted file mode 100644 index 4e3e9b221..000000000 --- a/testdata/d2ir/TestCompile/roots/primary.exp.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/roots/primary.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - }, - "composite": { - "fields": [ - { - "name": "pqrs" - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/edge.exp.json b/testdata/d2ir/TestCompile/simple/edge.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestCompile/simple/edge.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/field.exp.json b/testdata/d2ir/TestCompile/simple/field.exp.json deleted file mode 100644 index 2dcf9ab44..000000000 --- a/testdata/d2ir/TestCompile/simple/field.exp.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "fields": [ - { - "name": "x" - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/field/label.exp.json b/testdata/d2ir/TestCompile/simple/field/label.exp.json deleted file mode 100644 index 17ebacf95..000000000 --- a/testdata/d2ir/TestCompile/simple/field/label.exp.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/simple/field/label.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/field/label/nested.exp.json b/testdata/d2ir/TestCompile/simple/field/label/nested.exp.json deleted file mode 100644 index 520e48db0..000000000 --- a/testdata/d2ir/TestCompile/simple/field/label/nested.exp.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "composite": { - "fields": [ - { - "name": "y", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/simple/field/label/nested.d2,0:5:5-0:8:8", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - } - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/nested.exp.json b/testdata/d2ir/TestCompile/simple/nested.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestCompile/simple/nested.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/primary.exp.json b/testdata/d2ir/TestCompile/simple/primary.exp.json deleted file mode 100644 index d104b375d..000000000 --- a/testdata/d2ir/TestCompile/simple/primary.exp.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/simple/primary.d2,0:3:3-0:6:6", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - }, - "composite": { - "fields": [ - { - "name": "pqrs" - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/primary/nested.exp.json b/testdata/d2ir/TestCompile/simple/primary/nested.exp.json deleted file mode 100644 index ebac9bea8..000000000 --- a/testdata/d2ir/TestCompile/simple/primary/nested.exp.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "fields": [ - { - "name": "x", - "composite": { - "fields": [ - { - "name": "y", - "primary": { - "value": { - "range": "d2/testdata/d2ir/TestCompile/simple/primary/nested.d2,0:5:5-0:8:8", - "value": [ - { - "string": "yes", - "raw_string": "yes" - } - ] - } - }, - "composite": { - "fields": [ - { - "name": "pqrs" - } - ], - "edges": null - } - } - ], - "edges": null - } - } - ], - "edges": null -} diff --git a/testdata/d2ir/TestCompile/simple/underscore_parent.exp.json b/testdata/d2ir/TestCompile/simple/underscore_parent.exp.json deleted file mode 100644 index fbfc21849..000000000 --- a/testdata/d2ir/TestCompile/simple/underscore_parent.exp.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "fields": null, - "edges": null -}