cue: return an ast.Expr instead of Instance
Fixes cuelang/cue#21.
Change-Id: I214a7bf96db69d02deedc6db06c3b5945298d9dc
diff --git a/cue/ast.go b/cue/ast.go
index e1303ea..9dbed65 100644
--- a/cue/ast.go
+++ b/cue/ast.go
@@ -85,6 +85,10 @@
func newVisitor(idx *index, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
ctx := idx.newContext()
+ return newVisitorCtx(ctx, inst, obj, resolveRoot)
+}
+
+func newVisitorCtx(ctx *context, inst *build.Instance, obj, resolveRoot *structLit) *astVisitor {
v := &astVisitor{
object: obj,
}
diff --git a/cue/builtin.go b/cue/builtin.go
index daaaae0..b6fdd17 100644
--- a/cue/builtin.go
+++ b/cue/builtin.go
@@ -13,6 +13,7 @@
// limitations under the License.
//go:generate go run gen.go
+//go:generate goimports -w builtins.go
package cue
@@ -366,8 +367,9 @@
return v
case nil:
return &nullLit{src.base()}
- case *Instance:
- return v.eval(ctx)
+ case ast.Expr:
+ x := newVisitorCtx(ctx, nil, nil, nil)
+ return ctx.manifest(x.walk(v))
case error:
return ctx.mkErr(src, v.Error())
case bool:
diff --git a/cue/builtins.go b/cue/builtins.go
index 62a0ed0..f16d702 100644
--- a/cue/builtins.go
+++ b/cue/builtins.go
@@ -371,7 +371,7 @@
return nil, fmt.Errorf("json: could not parse JSON: %v", err)
}
- return FromExpr(fset, expr)
+ return expr, nil
}()
},
}},
@@ -420,11 +420,7 @@
data := c.bytes(0)
c.ret, c.err = func() (interface{}, error) {
fset := token.NewFileSet()
- expr, err := yaml.Unmarshal(fset, "", data)
- if err != nil {
- return nil, err
- }
- return FromExpr(fset, expr)
+ return yaml.Unmarshal(fset, "", data)
}()
},
}},
diff --git a/pkg/encoding/json/manual.go b/pkg/encoding/json/manual.go
index 9b2d324..3af3d37 100644
--- a/pkg/encoding/json/manual.go
+++ b/pkg/encoding/json/manual.go
@@ -20,6 +20,7 @@
"fmt"
"cuelang.org/go/cue"
+ "cuelang.org/go/cue/ast"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
)
@@ -90,7 +91,7 @@
}
// Unmarshal parses the JSON-encoded data.
-func Unmarshal(b []byte) (*cue.Instance, error) {
+func Unmarshal(b []byte) (ast.Expr, error) {
if !json.Valid(b) {
return nil, fmt.Errorf("json: invalid JSON")
}
@@ -100,5 +101,5 @@
// NOTE: should never happen.
return nil, fmt.Errorf("json: could not parse JSON: %v", err)
}
- return cue.FromExpr(fset, expr)
+ return expr, nil
}
diff --git a/pkg/encoding/yaml/manual.go b/pkg/encoding/yaml/manual.go
index c4e609a..646e7aa 100644
--- a/pkg/encoding/yaml/manual.go
+++ b/pkg/encoding/yaml/manual.go
@@ -18,6 +18,7 @@
"bytes"
"cuelang.org/go/cue"
+ "cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/third_party/yaml"
goyaml "github.com/ghodss/yaml"
@@ -51,11 +52,7 @@
}
// Unmarshal parses the YAML to a CUE instance.
-func Unmarshal(data []byte) (*cue.Instance, error) {
+func Unmarshal(data []byte) (ast.Expr, error) {
fset := token.NewFileSet()
- expr, err := yaml.Unmarshal(fset, "", data)
- if err != nil {
- return nil, err
- }
- return cue.FromExpr(fset, expr)
+ return yaml.Unmarshal(fset, "", data)
}