Skip to content

Commit b9b2c54

Browse files
committed
internal/core/export: better handling of generated CUE
Generated CUE structs can organized somewhat different from the ones computed during evaluation. Most notably, conjuncts can be a Vertex. Handle this. Fixes #499 Change-Id: I752e2d09ab7f49476d331028a75c1a9c8f9d480a Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7123 Reviewed-by: Paul Jolly <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent db18b37 commit b9b2c54

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

internal/core/export/export_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import (
2222
"cuelang.org/go/cue/ast"
2323
"cuelang.org/go/cue/errors"
2424
"cuelang.org/go/cue/format"
25+
"cuelang.org/go/internal"
26+
"cuelang.org/go/internal/core/adt"
2527
"cuelang.org/go/internal/core/compile"
28+
"cuelang.org/go/internal/core/convert"
2629
"cuelang.org/go/internal/core/eval"
2730
"cuelang.org/go/internal/core/export"
2831
"cuelang.org/go/internal/core/runtime"
@@ -69,6 +72,75 @@ func formatNode(t *testing.T, n ast.Node) []byte {
6972
return b
7073
}
7174

75+
// TestGenerated tests conversions of generated Go structs, which may be
76+
// different from parsed or evaluated CUE, such as having Vertex values.
77+
func TestGenerated(t *testing.T) {
78+
testCases := []struct {
79+
in interface{}
80+
value string
81+
typ string
82+
}{{
83+
in: &C{
84+
Terminals: []*A{
85+
{Name: "Name", Description: "Desc"},
86+
},
87+
},
88+
value: `{Terminals: [{Description: "Desc", Name: "Name"}]}`,
89+
typ: `*null|{Terminals?: *null|[...*null|{Name: string, Description: string}]}`,
90+
}, {
91+
in: []*A{
92+
{Name: "Name", Description: "Desc"},
93+
},
94+
value: `[{Name: "Name", Description: "Desc"}]`,
95+
typ: `*null|[...*null|{Name: string, Description: string}]`,
96+
}}
97+
for _, tc := range testCases {
98+
t.Run("", func(t *testing.T) {
99+
r := runtime.New()
100+
e := eval.New(r)
101+
ctx := adt.NewContext(r, e, &adt.Vertex{})
102+
103+
v := convert.GoValueToValue(ctx, tc.in, false)
104+
105+
expr, err := export.Expr(ctx, v)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
got := internal.DebugStr(expr)
110+
if got != tc.value {
111+
t.Errorf("value: got: %s\nwant: %s", got, tc.value)
112+
}
113+
114+
x, err := convert.GoTypeToExpr(ctx, tc.in)
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
expr, err = export.Expr(ctx, x)
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
got = internal.DebugStr(expr)
123+
if got != tc.typ {
124+
t.Errorf("type: got: %s\nwant: %s", got, tc.typ)
125+
}
126+
127+
})
128+
}
129+
}
130+
131+
type A struct {
132+
Name string
133+
Description string
134+
}
135+
136+
type B struct {
137+
Image string
138+
}
139+
140+
type C struct {
141+
Terminals []*A
142+
}
143+
72144
// For debugging purposes. Do not delete.
73145
func TestX(t *testing.T) {
74146
t.Skip()

internal/core/export/expr.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,20 @@ func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr) {
263263

264264
case adt.Value: // other values.
265265
if v, ok := x.(*adt.Vertex); ok {
266-
// if !v.IsList() {
267-
// panic("what to do?") // TO
268-
// }
266+
if v.IsList() {
267+
a := []ast.Expr{}
268+
for _, x := range v.Elems() {
269+
a = append(a, e.expr(x))
270+
}
271+
if !v.IsClosed(e.ctx) {
272+
v := &adt.Vertex{}
273+
v.MatchAndInsert(e.ctx, v)
274+
a = append(a, &ast.Ellipsis{Type: e.expr(v)})
275+
}
276+
e.exprs = append(e.exprs, ast.NewList(a...))
277+
return
278+
}
279+
269280
e.structs = append(e.structs, v.Structs...)
270281

271282
// generated, only consider arcs.

0 commit comments

Comments
 (0)