Skip to content

Commit 5608a83

Browse files
committed
cuego: use Runtime to simplify code
This helps implementing the Go encoder down the line. Change-Id: I3497af993f108085fe438a2c56eb61c08e887f8e Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2701 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 9811321 commit 5608a83

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

cue/go.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ import (
3737
// optimized.
3838

3939
func init() {
40-
internal.FromGoValue = func(instance, x interface{}) interface{} {
41-
return convertValue(instance.(*Instance), x)
40+
internal.FromGoValue = func(runtime, x interface{}) interface{} {
41+
return convertValue(runtime.(*Runtime), x)
4242
}
4343

44-
internal.FromGoType = func(instance, x interface{}) interface{} {
45-
return convertType(instance.(*Instance), x)
44+
internal.FromGoType = func(runtime, x interface{}) interface{} {
45+
return convertType(runtime.(*Runtime), x)
4646
}
4747
}
4848

49-
func convertValue(inst *Instance, x interface{}) Value {
50-
ctx := inst.index.newContext()
49+
func convertValue(r *Runtime, x interface{}) Value {
50+
ctx := r.index().newContext()
5151
v := convert(ctx, baseValue{}, x)
5252
return newValueRoot(ctx, v)
5353
}
5454

55-
func convertType(inst *Instance, x interface{}) Value {
56-
ctx := inst.index.newContext()
57-
v := convertGoType(inst, reflect.TypeOf(x))
55+
func convertType(r *Runtime, x interface{}) Value {
56+
ctx := r.index().newContext()
57+
v := convertGoType(r, reflect.TypeOf(x))
5858
return newValueRoot(ctx, v)
5959

6060
}
@@ -386,8 +386,8 @@ var (
386386
mutex sync.Mutex
387387
)
388388

389-
func convertGoType(inst *Instance, t reflect.Type) value {
390-
ctx := inst.newContext()
389+
func convertGoType(r *Runtime, t reflect.Type) value {
390+
ctx := r.index().newContext()
391391
// TODO: this can be much more efficient.
392392
mutex.Lock()
393393
defer mutex.Unlock()

cuego/cuego.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"sync"
2121

2222
"cuelang.org/go/cue"
23-
"cuelang.org/go/cue/build"
2423
"cuelang.org/go/cue/parser"
2524
"cuelang.org/go/internal"
2625
)
@@ -160,16 +159,13 @@ func (c *Context) Constrain(x interface{}, constraints string) error {
160159
var (
161160
mutex sync.Mutex
162161
instance *cue.Instance
162+
runtime = &cue.Runtime{}
163163
)
164164

165165
func init() {
166-
context := build.NewContext()
167-
inst := context.NewInstance("<cuego>", nil)
168-
if err := inst.AddFile("<cuego>", "{}"); err != nil {
169-
panic(err)
170-
}
171-
instance = cue.Build([]*build.Instance{inst})[0]
172-
if err := instance.Err; err != nil {
166+
var err error
167+
instance, err = runtime.Compile("<cuego>", "{}")
168+
if err != nil {
173169
panic(err)
174170
}
175171
}
@@ -180,7 +176,7 @@ func fromGoValue(x interface{}) (v cue.Value, err error) {
180176
// Instance) here as any previously unrecognized field can never match an
181177
// existing one and can only be merged.
182178
mutex.Lock()
183-
v = internal.FromGoValue(instance, x).(cue.Value)
179+
v = internal.FromGoValue(runtime, x).(cue.Value)
184180
mutex.Unlock()
185181
return v, nil
186182

@@ -205,7 +201,7 @@ func fromGoType(x interface{}) cue.Value {
205201
// Instance) here as any previously unrecognized field can never match an
206202
// existing one and can only be merged.
207203
mutex.Lock()
208-
v := internal.FromGoType(instance, x).(cue.Value)
204+
v := internal.FromGoType(runtime, x).(cue.Value)
209205
mutex.Unlock()
210206
return v
211207
}

0 commit comments

Comments
 (0)