Skip to content

Commit c290772

Browse files
committed
internal/value: implement interface type that is both value and instance
Change-Id: I2afe782eebb766a04aaaeaf1a43f02b25a99beb7 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9441 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 1f78a8d commit c290772

File tree

6 files changed

+33
-11
lines changed

6 files changed

+33
-11
lines changed

cue/instance.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ import (
2626
"cuelang.org/go/internal/core/runtime"
2727
)
2828

29+
// An InstanceOrValue is implemented by Value and *Instance.
30+
//
31+
// This is a placeholder type that is used to allow Instance-based APIs to
32+
// transition to Value-based APIs. The goals is to get rid of the Instance
33+
// type before v1.0.0.
34+
type InstanceOrValue interface {
35+
Value() Value
36+
37+
internal()
38+
}
39+
40+
func (Value) internal() {}
41+
func (*Instance) internal() {}
42+
43+
// Value implements value.Instance.
44+
func (v hiddenValue) Value() Value { return v }
45+
2946
// An Instance defines a single configuration based on a collection of
3047
// underlying CUE files.
3148
type Instance struct {

encoding/jsonschema/jsonschema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
//
4141
// The generated CUE schema is guaranteed to deem valid any value that is
4242
// a valid instance of the source JSON schema.
43-
func Extract(data *cue.Instance, cfg *Config) (f *ast.File, err error) {
43+
func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {
4444
d := &decoder{cfg: cfg}
4545

4646
f = d.decode(data.Value())

tools/flow/flow.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ func (c *Controller) addErr(err error, msg string) {
194194
}
195195

196196
// New creates a Controller for a given Instance and TaskFunc.
197-
func New(cfg *Config, inst *cue.Instance, f TaskFunc) *Controller {
197+
//
198+
// The instance value can either be a *cue.Instance or a cue.Value.
199+
func New(cfg *Config, inst cue.InstanceOrValue, f TaskFunc) *Controller {
198200
v := inst.Value()
199201
ctx := eval.NewContext(value.ToInternal(v))
200202

tools/flow/flow_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525

2626
"cuelang.org/go/cue"
27+
"cuelang.org/go/cue/cuecontext"
2728
"cuelang.org/go/cue/errors"
2829
"cuelang.org/go/cue/format"
2930
"cuelang.org/go/internal/cuetest"
@@ -43,10 +44,11 @@ func TestFlow(t *testing.T) {
4344
test.Run(t, func(t *cuetxtar.Test) {
4445
a := t.ValidInstances()
4546

46-
inst := cue.Build(a)[0]
47-
if inst.Err != nil {
48-
t.Fatal(inst.Err)
47+
insts, err := cuecontext.New().BuildInstances(a)
48+
if err != nil {
49+
t.Fatal(err)
4950
}
51+
v := insts[0]
5052

5153
seqNum = 0
5254

@@ -76,7 +78,7 @@ func TestFlow(t *testing.T) {
7678
UpdateFunc: updateFunc,
7779
}
7880

79-
c := flow.New(cfg, inst, taskFunc)
81+
c := flow.New(cfg, v, taskFunc)
8082

8183
w := t.Writer("errors")
8284
if err := c.Run(context.Background()); err != nil {

tools/trim/trim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type Config struct {
7171
// as can be derived from the evaluated values in inst.
7272
// Trimming is done on a best-effort basis and only when the removed field
7373
// is clearly implied by another field, rather than equal sibling fields.
74-
func Files(files []*ast.File, inst *cue.Instance, cfg *Config) error {
74+
func Files(files []*ast.File, inst cue.InstanceOrValue, cfg *Config) error {
7575
r, v := value.ToInternal(inst.Value())
7676

7777
t := &trimmer{

tools/trim/trim_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"cuelang.org/go/cue"
2121
"cuelang.org/go/cue/ast"
22+
"cuelang.org/go/cue/cuecontext"
2223
"cuelang.org/go/cue/errors"
2324
"cuelang.org/go/cue/format"
2425
"cuelang.org/go/cue/parser"
@@ -255,12 +256,12 @@ foo: entry: {
255256
if err != nil {
256257
t.Fatal(err)
257258
}
258-
r := cue.Runtime{}
259-
inst, err := r.CompileFile(f)
260-
if err != nil {
259+
r := cuecontext.New()
260+
v := r.BuildFile(f)
261+
if err := v.Err(); err != nil {
261262
t.Fatal(err)
262263
}
263-
err = Files([]*ast.File{f}, inst, &Config{Trace: false})
264+
err = Files([]*ast.File{f}, v, &Config{Trace: false})
264265
if err != nil {
265266
t.Fatal(err)
266267
}

0 commit comments

Comments
 (0)