Skip to content

Commit 1e1fe6f

Browse files
committed
internal/core/export: handle scalar values in adt.Vertex
Fixes #523 Change-Id: I9662bb76cebb326e012575fbfc8c6ca497d2c278 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7141 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent b9b2c54 commit 1e1fe6f

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

internal/core/export/export_test.go

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"cuelang.org/go/cue/ast"
2323
"cuelang.org/go/cue/errors"
2424
"cuelang.org/go/cue/format"
25+
"cuelang.org/go/cue/parser"
2526
"cuelang.org/go/internal"
2627
"cuelang.org/go/internal/core/adt"
2728
"cuelang.org/go/internal/core/compile"
@@ -76,54 +77,81 @@ func formatNode(t *testing.T, n ast.Node) []byte {
7677
// different from parsed or evaluated CUE, such as having Vertex values.
7778
func TestGenerated(t *testing.T) {
7879
testCases := []struct {
79-
in interface{}
80-
value string
81-
typ string
80+
in func(ctx *adt.OpContext) (adt.Expr, error)
81+
out string
8282
}{{
83-
in: &C{
84-
Terminals: []*A{
85-
{Name: "Name", Description: "Desc"},
86-
},
83+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
84+
in := &C{
85+
Terminals: []*A{{Name: "Name", Description: "Desc"}},
86+
}
87+
return convert.GoValueToValue(ctx, in, false), nil
88+
},
89+
out: `{Terminals: [{Description: "Desc", Name: "Name"}]}`,
90+
}, {
91+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
92+
in := &C{
93+
Terminals: []*A{{Name: "Name", Description: "Desc"}},
94+
}
95+
return convert.GoTypeToExpr(ctx, in)
96+
},
97+
out: `*null|{Terminals?: *null|[...*null|{Name: string, Description: string}]}`,
98+
}, {
99+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
100+
in := []*A{{Name: "Name", Description: "Desc"}}
101+
return convert.GoValueToValue(ctx, in, false), nil
87102
},
88-
value: `{Terminals: [{Description: "Desc", Name: "Name"}]}`,
89-
typ: `*null|{Terminals?: *null|[...*null|{Name: string, Description: string}]}`,
103+
out: `[{Name: "Name", Description: "Desc"}]`,
90104
}, {
91-
in: []*A{
92-
{Name: "Name", Description: "Desc"},
105+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
106+
in := []*A{{Name: "Name", Description: "Desc"}}
107+
return convert.GoTypeToExpr(ctx, in)
108+
},
109+
out: `*null|[...*null|{Name: string, Description: string}]`,
110+
}, {
111+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
112+
expr, err := parser.ParseExpr("test", `{
113+
x: Guide.#Terminal
114+
Guide: {}
115+
}`)
116+
if err != nil {
117+
return nil, err
118+
}
119+
c, err := compile.Expr(nil, ctx, expr)
120+
if err != nil {
121+
return nil, err
122+
}
123+
root := &adt.Vertex{}
124+
root.AddConjunct(c)
125+
root.Finalize(ctx)
126+
127+
// Simulate Value.Unify of Lookup("x") and Lookup("Guide").
128+
n := &adt.Vertex{}
129+
n.AddConjunct(adt.MakeRootConjunct(nil, root.Arcs[0]))
130+
n.AddConjunct(adt.MakeRootConjunct(nil, root.Arcs[1]))
131+
n.Finalize(ctx)
132+
133+
return n, nil
93134
},
94-
value: `[{Name: "Name", Description: "Desc"}]`,
95-
typ: `*null|[...*null|{Name: string, Description: string}]`,
135+
out: `<[l2// x: undefined field #Terminal] _|_>`,
96136
}}
97137
for _, tc := range testCases {
98138
t.Run("", func(t *testing.T) {
99139
r := runtime.New()
100140
e := eval.New(r)
101141
ctx := adt.NewContext(r, e, &adt.Vertex{})
102142

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)
143+
v, err := tc.in(ctx)
115144
if err != nil {
116-
t.Fatal(err)
145+
t.Fatal("failed test case: ", err)
117146
}
118-
expr, err = export.Expr(ctx, x)
147+
expr, err := export.Expr(ctx, v)
119148
if err != nil {
120-
t.Fatal(err)
149+
t.Fatal("failed export: ", err)
121150
}
122-
got = internal.DebugStr(expr)
123-
if got != tc.typ {
124-
t.Errorf("type: got: %s\nwant: %s", got, tc.typ)
151+
got := internal.DebugStr(expr)
152+
if got != tc.out {
153+
t.Errorf("got: %s\nwant: %s", got, tc.out)
125154
}
126-
127155
})
128156
}
129157
}

internal/core/export/value.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,17 @@ func (e *exporter) vertex(n *adt.Vertex) (result ast.Expr) {
5050
result = e.listComposite(n)
5151

5252
case *adt.Bottom:
53-
if x.IsIncomplete() {
54-
// fall back to expression mode
55-
result = stripRefs(e.expr(n))
53+
if !x.IsIncomplete() || len(n.Conjuncts) == 0 {
54+
result = e.bottom(x)
5655
break
5756
}
58-
result = e.bottom(x)
57+
58+
// fall back to expression mode
59+
a := []ast.Expr{}
60+
for _, c := range n.Conjuncts {
61+
a = append(a, e.expr(c.Expr()))
62+
}
63+
result = ast.NewBinExpr(token.AND, a...)
5964

6065
default:
6166
result = e.value(n.Value, n.Conjuncts...)

0 commit comments

Comments
 (0)