Skip to content

Commit d76e2cc

Browse files
committed
cue: remove MakeValue
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 <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 421ead3 commit d76e2cc

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

cue/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func NilIsAny(isAny bool) EncodeOption {
212212
// The returned Value will represent an error, accessible through Err, if any
213213
// error occurred.
214214
func (c *Context) Encode(x interface{}, option ...EncodeOption) Value {
215+
switch v := x.(type) {
216+
case adt.Value:
217+
return newValueRoot(c.index(), c.ctx(), v)
218+
}
215219
var options encodeOptions
216220
options.process(option)
217221

cue/types.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,6 @@ func Dereference(v Value) Value {
632632
return makeValue(v.idx, n)
633633
}
634634

635-
// MakeValue converts an adt.Value and given OpContext to a Value. The context
636-
// must be directly or indirectly obtained from the NewRuntime defined in this
637-
// package and it will panic if this is not the case.
638-
//
639-
// For internal use only.
640-
func MakeValue(ctx *adt.OpContext, v adt.Value) Value {
641-
index := ctx.Impl().(*runtime.Runtime)
642-
643-
return newValueRoot(index, newContext(index), v)
644-
}
645-
646635
func makeValue(idx *runtime.Runtime, v *adt.Vertex) Value {
647636
if v.Status() == 0 || v.BaseValue == nil {
648637
panic(fmt.Sprintf("not properly initialized (state: %v, value: %T)",

internal/core/dep/dep_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestVisit(t *testing.T) {
7272

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

116116
_ = dep.VisitFields(ctxt, n, func(d dep.Dependency) error {
117-
str := cue.MakeValue(ctxt, d.Node).Path().String()
117+
str := value.Make(ctxt, d.Node).Path().String()
118118
if i := d.Import(); i != nil {
119119
path := i.ImportPath.StringValue(ctxt)
120120
str = fmt.Sprintf("%q.%s", path, str)

internal/value/value.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) {
3939
}
4040

4141
// TODO:
42+
// Make wraps cue.MakeValue.
43+
func Make(ctx *adt.OpContext, v adt.Value) cue.Value {
44+
return (*cue.Context)(ctx.Impl().(*runtime.Runtime)).Encode(v)
45+
}
46+
4247
//
4348
// func Make(r *runtime.Runtime, v *adt.Vertex) cue.Value {
4449
// return cue.Value{}

pkg/internal/context.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"cuelang.org/go/cue"
2222
"cuelang.org/go/cue/token"
2323
"cuelang.org/go/internal/core/adt"
24+
"cuelang.org/go/internal/value"
2425
"github.com/cockroachdb/apd/v2"
2526
)
2627

@@ -48,7 +49,7 @@ func (c *CallCtxt) Do() bool {
4849
}
4950

5051
func (c *CallCtxt) Value(i int) cue.Value {
51-
v := cue.MakeValue(c.ctx, c.args[i])
52+
v := value.Make(c.ctx, c.args[i])
5253
// TODO: remove default
5354
// v, _ = v.Default()
5455
if !v.IsConcrete() {
@@ -58,7 +59,7 @@ func (c *CallCtxt) Value(i int) cue.Value {
5859
}
5960

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

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

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

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

124125
func (c *CallCtxt) Float64(i int) float64 {
125-
x := cue.MakeValue(c.ctx, c.args[i])
126+
x := value.Make(c.ctx, c.args[i])
126127
res, err := x.Float64()
127128
if err != nil {
128129
c.invalidArgType(c.args[i], i, "float64", err)
@@ -132,7 +133,7 @@ func (c *CallCtxt) Float64(i int) float64 {
132133
}
133134

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

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

164165
func (c *CallCtxt) String(i int) string {
165166
// TODO: use Evaluate instead.
166-
x := cue.MakeValue(c.ctx, c.args[i])
167+
x := value.Make(c.ctx, c.args[i])
167168
v, err := x.String()
168169
if err != nil {
169170
c.invalidArgType(c.args[i], i, "string", err)
@@ -173,7 +174,7 @@ func (c *CallCtxt) String(i int) string {
173174
}
174175

175176
func (c *CallCtxt) Bytes(i int) []byte {
176-
x := cue.MakeValue(c.ctx, c.args[i])
177+
x := value.Make(c.ctx, c.args[i])
177178
v, err := x.Bytes()
178179
if err != nil {
179180
c.invalidArgType(c.args[i], i, "bytes", err)
@@ -183,7 +184,7 @@ func (c *CallCtxt) Bytes(i int) []byte {
183184
}
184185

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

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

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

220221
func (c *CallCtxt) Iter(i int) (a cue.Iterator) {
221222
arg := c.args[i]
222-
x := cue.MakeValue(c.ctx, arg)
223+
x := value.Make(c.ctx, arg)
223224
v, err := x.List()
224225
if err != nil {
225226
c.invalidArgType(c.args[i], i, "list", err)

tools/flow/run.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ package flow
2626
// future tasks may be long running, as discussed above.
2727

2828
import (
29-
"cuelang.org/go/cue"
3029
"cuelang.org/go/cue/errors"
3130
"cuelang.org/go/internal/core/adt"
3231
"cuelang.org/go/internal/core/eval"
@@ -153,7 +152,7 @@ func (c *Controller) updateValue() bool {
153152
v := &adt.Vertex{Conjuncts: c.conjuncts}
154153
v.Finalize(c.opCtx)
155154

156-
c.inst = cue.MakeValue(c.opCtx, v)
155+
c.inst = value.Make(c.opCtx, v)
157156
c.valueSeqNum = c.conjunctSeq
158157
return true
159158
}

tools/flow/tasks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (c *Controller) findImpliedTask(d dep.Dependency) *Task {
192192
}
193193

194194
if !d.IsRoot() {
195-
v := cue.MakeValue(c.opCtx, n)
195+
v := value.Make(c.opCtx, n)
196196

197197
if t := c.getTask(nil, v); t != nil {
198198
return t
@@ -225,7 +225,7 @@ func (c *Controller) markTaskDependencies(t *Task, n *adt.Vertex) {
225225
depTask := c.findImpliedTask(d)
226226
if depTask != nil {
227227
if depTask != cycleMarker {
228-
v := cue.MakeValue(c.opCtx, d.Node)
228+
v := value.Make(c.opCtx, d.Node)
229229
t.addDep(v.Path().String(), depTask)
230230
}
231231
return nil
@@ -245,7 +245,7 @@ func (c *Controller) markTaskDependencies(t *Task, n *adt.Vertex) {
245245
}
246246

247247
func (c *Controller) inRoot(n *adt.Vertex) bool {
248-
path := cue.MakeValue(c.opCtx, n).Path().Selectors()
248+
path := value.Make(c.opCtx, n).Path().Selectors()
249249
root := c.cfg.Root.Selectors()
250250
if len(path) < len(root) {
251251
return false

0 commit comments

Comments
 (0)