Skip to content

Commit b5b0429

Browse files
committed
internal/core/export: bug fixes for exporting API-generated values
1) if a field had an error, it could be incorrectly marked as an optional field. 2) sometimes, incomplete errors were incorrectly shown as actual errors. Change-Id: I67c1a59c09f27f30f508d9110484ae7f08b138fa Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9484 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 14ec6e2 commit b5b0429

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

internal/core/export/export_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"cuelang.org/go/cue"
2121
"cuelang.org/go/cue/ast"
22+
"cuelang.org/go/cue/cuecontext"
2223
"cuelang.org/go/cue/errors"
2324
"cuelang.org/go/cue/format"
2425
"cuelang.org/go/cue/parser"
@@ -32,6 +33,7 @@ import (
3233
"cuelang.org/go/internal/core/runtime"
3334
"cuelang.org/go/internal/cuetest"
3435
"cuelang.org/go/internal/cuetxtar"
36+
"cuelang.org/go/internal/value"
3537
"github.com/rogpeppe/go-internal/txtar"
3638
)
3739

@@ -78,6 +80,7 @@ func TestGenerated(t *testing.T) {
7880
testCases := []struct {
7981
in func(ctx *adt.OpContext) (adt.Expr, error)
8082
out string
83+
p *export.Profile
8184
}{{
8285
in: func(ctx *adt.OpContext) (adt.Expr, error) {
8386
in := &C{
@@ -131,7 +134,28 @@ func TestGenerated(t *testing.T) {
131134

132135
return n, nil
133136
},
134-
out: `<[l2// x: undefined field #Terminal] _|_>`,
137+
// TODO: should probably print path.
138+
out: `<[l2// undefined field #Terminal] _|_>`,
139+
p: export.Final,
140+
}, {
141+
in: func(ctx *adt.OpContext) (adt.Expr, error) {
142+
c := cuecontext.New()
143+
v := c.CompileString(`
144+
#Provider: {
145+
ID: string
146+
notConcrete: bool
147+
a: int
148+
b: a + 1
149+
}`)
150+
151+
spec := v.LookupPath(cue.ParsePath("#Provider"))
152+
spec2 := spec.FillPath(cue.ParsePath("ID"), "12345")
153+
root := v.FillPath(cue.ParsePath("providers.foo"), spec2)
154+
_, n := value.ToInternal(root)
155+
156+
return n, nil
157+
},
158+
out: `{#Provider: {ID: string, notConcrete: bool, a: int, b: a+1}, providers: {foo: {ID: "12345", notConcrete: bool, a: int, b: a+1}}}`,
135159
}}
136160
for _, tc := range testCases {
137161
t.Run("", func(t *testing.T) {
@@ -142,7 +166,13 @@ func TestGenerated(t *testing.T) {
142166
if err != nil {
143167
t.Fatal("failed test case: ", err)
144168
}
145-
expr, err := export.Expr(ctx, "", v)
169+
170+
p := tc.p
171+
if p == nil {
172+
p = export.Simplified
173+
}
174+
175+
expr, err := p.Expr(ctx, "", v)
146176
if err != nil {
147177
t.Fatal("failed export: ", err)
148178
}

internal/core/export/expr.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr, isEmbed bool) {
275275
if isComplexStruct(x) {
276276
_, saved := e.pushFrame(nil)
277277
e.embed = append(e.embed, e.adt(x, nil))
278+
e.top().upCount-- // not necessary, but for proper form
278279
e.popFrame(saved)
279280
return
280281
}
@@ -331,7 +332,13 @@ func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr, isEmbed bool) {
331332
x = nil
332333

333334
case adt.Value:
334-
e.values.AddConjunct(adt.MakeRootConjunct(env, y)) // GOBBLE TOP
335+
if v.IsData() {
336+
e.values.AddConjunct(adt.MakeRootConjunct(env, y))
337+
break
338+
}
339+
for _, c := range v.Conjuncts {
340+
e.values.AddConjunct(c)
341+
}
335342
}
336343

337344
// generated, only consider arcs.
@@ -377,6 +384,9 @@ func isOptional(a []adt.Conjunct) bool {
377384
return false
378385
}
379386
for _, c := range a {
387+
if v, ok := c.Expr().(*adt.Vertex); ok && !v.IsData() && len(v.Conjuncts) > 0 {
388+
return isOptional(v.Conjuncts)
389+
}
380390
switch f := c.Source().(type) {
381391
case nil:
382392
return false

0 commit comments

Comments
 (0)