Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/value: implement interface type that is both value and instance
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed Apr 21, 2021
1 parent 1f78a8d commit c290772
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
17 changes: 17 additions & 0 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ import (
"cuelang.org/go/internal/core/runtime"
)

// An InstanceOrValue is implemented by Value and *Instance.
//
// This is a placeholder type that is used to allow Instance-based APIs to
// transition to Value-based APIs. The goals is to get rid of the Instance
// type before v1.0.0.
type InstanceOrValue interface {
Value() Value

internal()
}

func (Value) internal() {}
func (*Instance) internal() {}

// Value implements value.Instance.
func (v hiddenValue) Value() Value { return v }

// An Instance defines a single configuration based on a collection of
// underlying CUE files.
type Instance struct {
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
//
// The generated CUE schema is guaranteed to deem valid any value that is
// a valid instance of the source JSON schema.
func Extract(data *cue.Instance, cfg *Config) (f *ast.File, err error) {
func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error) {
d := &decoder{cfg: cfg}

f = d.decode(data.Value())
Expand Down
4 changes: 3 additions & 1 deletion tools/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ func (c *Controller) addErr(err error, msg string) {
}

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

Expand Down
10 changes: 6 additions & 4 deletions tools/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/internal/cuetest"
Expand All @@ -43,10 +44,11 @@ func TestFlow(t *testing.T) {
test.Run(t, func(t *cuetxtar.Test) {
a := t.ValidInstances()

inst := cue.Build(a)[0]
if inst.Err != nil {
t.Fatal(inst.Err)
insts, err := cuecontext.New().BuildInstances(a)
if err != nil {
t.Fatal(err)
}
v := insts[0]

seqNum = 0

Expand Down Expand Up @@ -76,7 +78,7 @@ func TestFlow(t *testing.T) {
UpdateFunc: updateFunc,
}

c := flow.New(cfg, inst, taskFunc)
c := flow.New(cfg, v, taskFunc)

w := t.Writer("errors")
if err := c.Run(context.Background()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tools/trim/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Config struct {
// as can be derived from the evaluated values in inst.
// Trimming is done on a best-effort basis and only when the removed field
// is clearly implied by another field, rather than equal sibling fields.
func Files(files []*ast.File, inst *cue.Instance, cfg *Config) error {
func Files(files []*ast.File, inst cue.InstanceOrValue, cfg *Config) error {
r, v := value.ToInternal(inst.Value())

t := &trimmer{
Expand Down
9 changes: 5 additions & 4 deletions tools/trim/trim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
Expand Down Expand Up @@ -255,12 +256,12 @@ foo: entry: {
if err != nil {
t.Fatal(err)
}
r := cue.Runtime{}
inst, err := r.CompileFile(f)
if err != nil {
r := cuecontext.New()
v := r.BuildFile(f)
if err := v.Err(); err != nil {
t.Fatal(err)
}
err = Files([]*ast.File{f}, inst, &Config{Trace: false})
err = Files([]*ast.File{f}, v, &Config{Trace: false})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c290772

Please sign in to comment.