Skip to content

Commit

Permalink
internal/core/export: bug fixes for exporting API-generated values
Browse files Browse the repository at this point in the history
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 <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 23, 2021
1 parent 14ec6e2 commit b5b0429
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 32 additions & 2 deletions internal/core/export/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
Expand All @@ -32,6 +33,7 @@ import (
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuetest"
"cuelang.org/go/internal/cuetxtar"
"cuelang.org/go/internal/value"
"github.com/rogpeppe/go-internal/txtar"
)

Expand Down Expand Up @@ -78,6 +80,7 @@ func TestGenerated(t *testing.T) {
testCases := []struct {
in func(ctx *adt.OpContext) (adt.Expr, error)
out string
p *export.Profile
}{{
in: func(ctx *adt.OpContext) (adt.Expr, error) {
in := &C{
Expand Down Expand Up @@ -131,7 +134,28 @@ func TestGenerated(t *testing.T) {

return n, nil
},
out: `<[l2// x: undefined field #Terminal] _|_>`,
// TODO: should probably print path.
out: `<[l2// undefined field #Terminal] _|_>`,
p: export.Final,
}, {
in: func(ctx *adt.OpContext) (adt.Expr, error) {
c := cuecontext.New()
v := c.CompileString(`
#Provider: {
ID: string
notConcrete: bool
a: int
b: a + 1
}`)

spec := v.LookupPath(cue.ParsePath("#Provider"))
spec2 := spec.FillPath(cue.ParsePath("ID"), "12345")
root := v.FillPath(cue.ParsePath("providers.foo"), spec2)
_, n := value.ToInternal(root)

return n, nil
},
out: `{#Provider: {ID: string, notConcrete: bool, a: int, b: a+1}, providers: {foo: {ID: "12345", notConcrete: bool, a: int, b: a+1}}}`,
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
Expand All @@ -142,7 +166,13 @@ func TestGenerated(t *testing.T) {
if err != nil {
t.Fatal("failed test case: ", err)
}
expr, err := export.Expr(ctx, "", v)

p := tc.p
if p == nil {
p = export.Simplified
}

expr, err := p.Expr(ctx, "", v)
if err != nil {
t.Fatal("failed export: ", err)
}
Expand Down
12 changes: 11 additions & 1 deletion internal/core/export/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr, isEmbed bool) {
if isComplexStruct(x) {
_, saved := e.pushFrame(nil)
e.embed = append(e.embed, e.adt(x, nil))
e.top().upCount-- // not necessary, but for proper form
e.popFrame(saved)
return
}
Expand Down Expand Up @@ -331,7 +332,13 @@ func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr, isEmbed bool) {
x = nil

case adt.Value:
e.values.AddConjunct(adt.MakeRootConjunct(env, y)) // GOBBLE TOP
if v.IsData() {
e.values.AddConjunct(adt.MakeRootConjunct(env, y))
break
}
for _, c := range v.Conjuncts {
e.values.AddConjunct(c)
}
}

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

0 comments on commit b5b0429

Please sign in to comment.