Skip to content

Commit

Permalink
cue: remove MakeValue
Browse files Browse the repository at this point in the history
This is now less obviously hidden in Encode.

Change-Id: Ic3905f2f7b5b48dfed7b95e79ad9f0556ad96aff
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9424
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 21, 2021
1 parent 421ead3 commit d76e2cc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
4 changes: 4 additions & 0 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func NilIsAny(isAny bool) EncodeOption {
// The returned Value will represent an error, accessible through Err, if any
// error occurred.
func (c *Context) Encode(x interface{}, option ...EncodeOption) Value {
switch v := x.(type) {
case adt.Value:
return newValueRoot(c.index(), c.ctx(), v)
}
var options encodeOptions
options.process(option)

Expand Down
11 changes: 0 additions & 11 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,17 +632,6 @@ func Dereference(v Value) Value {
return makeValue(v.idx, n)
}

// MakeValue converts an adt.Value and given OpContext to a Value. The context
// must be directly or indirectly obtained from the NewRuntime defined in this
// package and it will panic if this is not the case.
//
// For internal use only.
func MakeValue(ctx *adt.OpContext, v adt.Value) Value {
index := ctx.Impl().(*runtime.Runtime)

return newValueRoot(index, newContext(index), v)
}

func makeValue(idx *runtime.Runtime, v *adt.Vertex) Value {
if v.Status() == 0 || v.BaseValue == nil {
panic(fmt.Sprintf("not properly initialized (state: %v, value: %T)",
Expand Down
4 changes: 2 additions & 2 deletions internal/core/dep/dep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestVisit(t *testing.T) {

t.Run(tc.name, func(sub *testing.T) {
tc.fn(ctxt, n, func(d dep.Dependency) error {
str := cue.MakeValue(ctxt, d.Node).Path().String()
str := value.Make(ctxt, d.Node).Path().String()
if i := d.Import(); i != nil {
path := i.ImportPath.StringValue(ctxt)
str = fmt.Sprintf("%q.%s", path, str)
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestX(t *testing.T) {
deps := []string{}

_ = dep.VisitFields(ctxt, n, func(d dep.Dependency) error {
str := cue.MakeValue(ctxt, d.Node).Path().String()
str := value.Make(ctxt, d.Node).Path().String()
if i := d.Import(); i != nil {
path := i.ImportPath.StringValue(ctxt)
str = fmt.Sprintf("%q.%s", path, str)
Expand Down
5 changes: 5 additions & 0 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) {
}

// TODO:
// Make wraps cue.MakeValue.
func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
return (*cue.Context)(ctx.Impl().(*runtime.Runtime)).Encode(v)
}

//
// func Make(r *runtime.Runtime, v *adt.Vertex) cue.Value {
// return cue.Value{}
Expand Down
29 changes: 15 additions & 14 deletions pkg/internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/value"
"github.com/cockroachdb/apd/v2"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (c *CallCtxt) Do() bool {
}

func (c *CallCtxt) Value(i int) cue.Value {
v := cue.MakeValue(c.ctx, c.args[i])
v := value.Make(c.ctx, c.args[i])
// TODO: remove default
// v, _ = v.Default()
if !v.IsConcrete() {
Expand All @@ -58,7 +59,7 @@ func (c *CallCtxt) Value(i int) cue.Value {
}

func (c *CallCtxt) Struct(i int) *cue.Struct {
v := cue.MakeValue(c.ctx, c.args[i])
v := value.Make(c.ctx, c.args[i])
s, err := v.Struct()
if err != nil {
c.invalidArgType(c.args[i], i, "struct", err)
Expand All @@ -76,7 +77,7 @@ func (c *CallCtxt) Int64(i int) int64 { return int64(c.intValue(i, 64, "int64"))

func (c *CallCtxt) intValue(i, bits int, typ string) int64 {
arg := c.args[i]
x := cue.MakeValue(c.ctx, arg)
x := value.Make(c.ctx, arg)
n, err := x.Int(nil)
if err != nil {
c.invalidArgType(arg, i, typ, err)
Expand All @@ -98,7 +99,7 @@ func (c *CallCtxt) Uint32(i int) uint32 { return uint32(c.uintValue(i, 32, "uint
func (c *CallCtxt) Uint64(i int) uint64 { return uint64(c.uintValue(i, 64, "uint64")) }

func (c *CallCtxt) uintValue(i, bits int, typ string) uint64 {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
n, err := x.Int(nil)
if err != nil || n.Sign() < 0 {
c.invalidArgType(c.args[i], i, typ, err)
Expand All @@ -113,7 +114,7 @@ func (c *CallCtxt) uintValue(i, bits int, typ string) uint64 {
}

func (c *CallCtxt) Decimal(i int) *apd.Decimal {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
if _, err := x.MantExp(nil); err != nil {
c.invalidArgType(c.args[i], i, "Decimal", err)
return nil
Expand All @@ -122,7 +123,7 @@ func (c *CallCtxt) Decimal(i int) *apd.Decimal {
}

func (c *CallCtxt) Float64(i int) float64 {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
res, err := x.Float64()
if err != nil {
c.invalidArgType(c.args[i], i, "float64", err)
Expand All @@ -132,7 +133,7 @@ func (c *CallCtxt) Float64(i int) float64 {
}

func (c *CallCtxt) BigInt(i int) *big.Int {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
n, err := x.Int(nil)
if err != nil {
c.invalidArgType(c.args[i], i, "int", err)
Expand All @@ -144,7 +145,7 @@ func (c *CallCtxt) BigInt(i int) *big.Int {
var ten = big.NewInt(10)

func (c *CallCtxt) BigFloat(i int) *big.Float {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
var mant big.Int
exp, err := x.MantExp(&mant)
if err != nil {
Expand All @@ -163,7 +164,7 @@ func (c *CallCtxt) BigFloat(i int) *big.Float {

func (c *CallCtxt) String(i int) string {
// TODO: use Evaluate instead.
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
v, err := x.String()
if err != nil {
c.invalidArgType(c.args[i], i, "string", err)
Expand All @@ -173,7 +174,7 @@ func (c *CallCtxt) String(i int) string {
}

func (c *CallCtxt) Bytes(i int) []byte {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
v, err := x.Bytes()
if err != nil {
c.invalidArgType(c.args[i], i, "bytes", err)
Expand All @@ -183,7 +184,7 @@ func (c *CallCtxt) Bytes(i int) []byte {
}

func (c *CallCtxt) Reader(i int) io.Reader {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
// TODO: optimize for string and bytes cases
r, err := x.Reader()
if err != nil {
Expand All @@ -194,7 +195,7 @@ func (c *CallCtxt) Reader(i int) io.Reader {
}

func (c *CallCtxt) Bool(i int) bool {
x := cue.MakeValue(c.ctx, c.args[i])
x := value.Make(c.ctx, c.args[i])
b, err := x.Bool()
if err != nil {
c.invalidArgType(c.args[i], i, "bool", err)
Expand All @@ -205,7 +206,7 @@ func (c *CallCtxt) Bool(i int) bool {

func (c *CallCtxt) List(i int) (a []cue.Value) {
arg := c.args[i]
x := cue.MakeValue(c.ctx, arg)
x := value.Make(c.ctx, arg)
v, err := x.List()
if err != nil {
c.invalidArgType(c.args[i], i, "list", err)
Expand All @@ -219,7 +220,7 @@ func (c *CallCtxt) List(i int) (a []cue.Value) {

func (c *CallCtxt) Iter(i int) (a cue.Iterator) {
arg := c.args[i]
x := cue.MakeValue(c.ctx, arg)
x := value.Make(c.ctx, arg)
v, err := x.List()
if err != nil {
c.invalidArgType(c.args[i], i, "list", err)
Expand Down
3 changes: 1 addition & 2 deletions tools/flow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package flow
// future tasks may be long running, as discussed above.

import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/eval"
Expand Down Expand Up @@ -153,7 +152,7 @@ func (c *Controller) updateValue() bool {
v := &adt.Vertex{Conjuncts: c.conjuncts}
v.Finalize(c.opCtx)

c.inst = cue.MakeValue(c.opCtx, v)
c.inst = value.Make(c.opCtx, v)
c.valueSeqNum = c.conjunctSeq
return true
}
Expand Down
6 changes: 3 additions & 3 deletions tools/flow/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *Controller) findImpliedTask(d dep.Dependency) *Task {
}

if !d.IsRoot() {
v := cue.MakeValue(c.opCtx, n)
v := value.Make(c.opCtx, n)

if t := c.getTask(nil, v); t != nil {
return t
Expand Down Expand Up @@ -225,7 +225,7 @@ func (c *Controller) markTaskDependencies(t *Task, n *adt.Vertex) {
depTask := c.findImpliedTask(d)
if depTask != nil {
if depTask != cycleMarker {
v := cue.MakeValue(c.opCtx, d.Node)
v := value.Make(c.opCtx, d.Node)
t.addDep(v.Path().String(), depTask)
}
return nil
Expand All @@ -245,7 +245,7 @@ func (c *Controller) markTaskDependencies(t *Task, n *adt.Vertex) {
}

func (c *Controller) inRoot(n *adt.Vertex) bool {
path := cue.MakeValue(c.opCtx, n).Path().Selectors()
path := value.Make(c.opCtx, n).Path().Selectors()
root := c.cfg.Root.Selectors()
if len(path) < len(root) {
return false
Expand Down

0 comments on commit d76e2cc

Please sign in to comment.