From 82b08b7d8543ff96df131f4d8779768d6fdf32fb Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Fri, 30 Apr 2021 18:05:37 -0700 Subject: [PATCH] refactor for cue 0.4 cue deprecated the use of instances in favor of values. Refactored the code to accomodate the new changes. Signed-off-by: Andrea Luzzardi --- dagger/compiler/build.go | 9 +++- dagger/compiler/compiler.go | 91 ++++++++++++-------------------- dagger/compiler/compiler_test.go | 6 +-- dagger/compiler/json.go | 33 ------------ dagger/compiler/value.go | 49 ++++++++--------- dagger/deployment.go | 35 ++++++------ dagger/pipeline.go | 8 +-- 7 files changed, 89 insertions(+), 142 deletions(-) diff --git a/dagger/compiler/build.go b/dagger/compiler/build.go index eec050ad..f20da154 100644 --- a/dagger/compiler/build.go +++ b/dagger/compiler/build.go @@ -13,6 +13,8 @@ import ( // Build a cue configuration tree from the files in fs. func Build(sources map[string]fs.FS, args ...string) (*Value, error) { + c := DefaultCompiler + buildConfig := &cueload.Config{ // The CUE overlay needs to be prefixed by a non-conflicting path with the // local filesystem, otherwise Cue will merge the Overlay with whatever Cue @@ -55,9 +57,12 @@ func Build(sources map[string]fs.FS, args ...string) (*Value, error) { if len(instances) != 1 { return nil, errors.New("only one package is supported at a time") } - inst, err := Cue().Build(instances[0]) + v, err := c.Context.BuildInstances(instances) if err != nil { return nil, errors.New(cueerrors.Details(err, &cueerrors.Config{})) } - return Wrap(inst.Value(), inst), nil + if len(v) != 1 { + return nil, errors.New("internal: wrong number of values") + } + return Wrap(v[0]), nil } diff --git a/dagger/compiler/compiler.go b/dagger/compiler/compiler.go index f750136c..0f3c5784 100644 --- a/dagger/compiler/compiler.go +++ b/dagger/compiler/compiler.go @@ -2,10 +2,10 @@ package compiler import ( "errors" - "fmt" "sync" "cuelang.org/go/cue" + "cuelang.org/go/cue/cuecontext" cueerrors "cuelang.org/go/cue/errors" cuejson "cuelang.org/go/encoding/json" cueyaml "cuelang.org/go/encoding/yaml" @@ -13,10 +13,10 @@ import ( var ( // DefaultCompiler is the default Compiler and is used by Compile - DefaultCompiler = &Compiler{} + DefaultCompiler = New() ) -func Compile(name string, src interface{}) (*Value, error) { +func Compile(name string, src string) (*Value, error) { return DefaultCompiler.Compile(name, src) } @@ -25,16 +25,8 @@ func NewValue() *Value { } // FIXME can be refactored away now? -func Wrap(v cue.Value, inst *cue.Instance) *Value { - return DefaultCompiler.Wrap(v, inst) -} - -func InstanceMerge(src ...*Value) (*Value, error) { - return DefaultCompiler.InstanceMerge(src...) -} - -func Cue() *cue.Runtime { - return DefaultCompiler.Cue() +func Wrap(v cue.Value) *Value { + return DefaultCompiler.Wrap(v) } func Err(err error) error { @@ -54,7 +46,13 @@ func DecodeYAML(path string, data []byte) (*Value, error) { // Use this instead of cue.Runtime type Compiler struct { l sync.RWMutex - cue.Runtime + *cue.Context +} + +func New() *Compiler { + return &Compiler{ + Context: cuecontext.New(), + } } func (c *Compiler) lock() { @@ -73,10 +71,6 @@ func (c *Compiler) runlock() { c.l.RUnlock() } -func (c *Compiler) Cue() *cue.Runtime { - return &(c.Runtime) -} - // Compile an empty value func (c *Compiler) NewValue() *Value { empty, err := c.Compile("", "_") @@ -86,64 +80,47 @@ func (c *Compiler) NewValue() *Value { return empty } -func (c *Compiler) Compile(name string, src interface{}) (*Value, error) { +func (c *Compiler) Compile(name string, src string) (*Value, error) { c.lock() defer c.unlock() - inst, err := c.Cue().Compile(name, src) - if err != nil { + v := c.Context.CompileString(src, cue.Filename(name)) + if v.Err() != nil { // FIXME: cleaner way to unwrap cue error details? - return nil, Err(err) + return nil, Err(v.Err()) } - return c.Wrap(inst.Value(), inst), nil -} - -// InstanceMerge merges multiple values and mirrors the value in the cue.Instance. -// FIXME: AVOID THIS AT ALL COST -// Special case: we must return an instance with the same -// contents as v, for the purposes of cueflow. -func (c *Compiler) InstanceMerge(src ...*Value) (*Value, error) { - var ( - v = c.NewValue() - inst = v.CueInst() - err error - ) - - c.lock() - defer c.unlock() - - for _, s := range src { - inst, err = inst.Fill(s.val) - if err != nil { - return nil, fmt.Errorf("merge failed: %w", err) - } - if err := inst.Value().Err(); err != nil { - return nil, err - } - } - - v = c.Wrap(inst.Value(), inst) - return v, nil + return c.Wrap(v), nil } func (c *Compiler) DecodeJSON(path string, data []byte) (*Value, error) { - inst, err := cuejson.Decode(c.Cue(), path, data) + expr, err := cuejson.Extract(path, data) if err != nil { return nil, Err(err) } - return c.Wrap(inst.Value(), inst), nil + v := c.Context.BuildExpr(expr, cue.Filename(path)) + if err := v.Err(); err != nil { + return nil, Err(err) + } + return c.Wrap(v), nil } func (c *Compiler) DecodeYAML(path string, data []byte) (*Value, error) { - inst, err := cueyaml.Decode(c.Cue(), path, data) + f, err := cueyaml.Extract(path, data) if err != nil { return nil, Err(err) } - return c.Wrap(inst.Value(), inst), nil + v := c.Context.BuildFile(f, cue.Filename(path)) + if err := v.Err(); err != nil { + return nil, Err(err) + } + return c.Wrap(v), nil } -func (c *Compiler) Wrap(v cue.Value, inst *cue.Instance) *Value { - return wrapValue(v, inst, c) +func (c *Compiler) Wrap(v cue.Value) *Value { + return &Value{ + val: v, + cc: c, + } } func (c *Compiler) Err(err error) error { diff --git a/dagger/compiler/compiler_test.go b/dagger/compiler/compiler_test.go index 7b21d947..344d5720 100644 --- a/dagger/compiler/compiler_test.go +++ b/dagger/compiler/compiler_test.go @@ -8,7 +8,7 @@ import ( // Test that a non-existing field is detected correctly func TestFieldNotExist(t *testing.T) { - c := &Compiler{} + c := New() root, err := c.Compile("test.cue", `foo: "bar"`) require.NoError(t, err) require.True(t, root.Lookup("foo").Exists()) @@ -17,7 +17,7 @@ func TestFieldNotExist(t *testing.T) { // Test that a non-existing definition is detected correctly func TestDefNotExist(t *testing.T) { - c := &Compiler{} + c := New() root, err := c.Compile("test.cue", `foo: #bla: "bar"`) require.NoError(t, err) require.True(t, root.Lookup("foo.#bla").Exists()) @@ -25,7 +25,7 @@ func TestDefNotExist(t *testing.T) { } func TestJSON(t *testing.T) { - c := &Compiler{} + c := New() v, err := c.Compile("", `foo: hello: "world"`) require.NoError(t, err) require.Equal(t, `{"foo":{"hello":"world"}}`, string(v.JSON())) diff --git a/dagger/compiler/json.go b/dagger/compiler/json.go index fd2f59d7..e138f7af 100644 --- a/dagger/compiler/json.go +++ b/dagger/compiler/json.go @@ -5,8 +5,6 @@ import ( "encoding/json" "fmt" - "cuelang.org/go/cue" - cuejson "cuelang.org/go/encoding/json" "github.com/KromDaniel/jonson" ) @@ -100,37 +98,6 @@ func (s JSON) Set(valueJSON []byte, path ...string) (JSON, error) { return root.ToJSON() } -func (s JSON) Merge(layers ...JSON) (JSON, error) { - r := new(cue.Runtime) - var resultInst *cue.Instance - for i, l := range append([]JSON{s}, layers...) { - if l == nil { - continue - } - filename := fmt.Sprintf("%d", i) - inst, err := cuejson.Decode(r, filename, []byte(l)) - if err != nil { - return nil, err - } - if resultInst == nil { - resultInst = inst - } else { - resultInst, err = resultInst.Fill(inst.Value()) - if err != nil { - return nil, err - } - if resultInst.Err != nil { - return nil, resultInst.Err - } - } - } - b, err := resultInst.Value().MarshalJSON() - if err != nil { - return nil, err - } - return JSON(b), nil -} - func (s JSON) String() string { if s == nil { return "{}" diff --git a/dagger/compiler/value.go b/dagger/compiler/value.go index 964cce8d..3f20f50e 100644 --- a/dagger/compiler/value.go +++ b/dagger/compiler/value.go @@ -2,6 +2,7 @@ package compiler import ( "sort" + "strconv" "cuelang.org/go/cue" cueformat "cuelang.org/go/cue/format" @@ -10,25 +11,8 @@ import ( // Value is a wrapper around cue.Value. // Use instead of cue.Value and cue.Instance type Value struct { - val cue.Value - cc *Compiler - inst *cue.Instance -} - -func (v *Value) CueInst() *cue.Instance { - return v.inst -} - -func (v *Value) Wrap(v2 cue.Value) *Value { - return wrapValue(v2, v.inst, v.cc) -} - -func wrapValue(v cue.Value, inst *cue.Instance, cc *Compiler) *Value { - return &Value{ - val: v, - cc: cc, - inst: inst, - } + val cue.Value + cc *Compiler } // FillPath fills the value in-place @@ -51,7 +35,7 @@ func (v *Value) LookupPath(p cue.Path) *Value { v.cc.rlock() defer v.cc.runlock() - return v.Wrap(v.val.LookupPath(p)) + return v.cc.Wrap(v.val.LookupPath(p)) } // Lookup is a helper function to lookup by path parts. @@ -71,8 +55,17 @@ func (v *Value) Kind() cue.Kind { // Field represents a struct field type Field struct { - Label string - Value *Value + Selector cue.Selector + Value *Value +} + +// Label returns the unquoted selector +func (f Field) Label() string { + l := f.Selector.String() + if unquoted, err := strconv.Unquote(l); err == nil { + return unquoted + } + return l } // Proxy function to the underlying cue.Value @@ -86,13 +79,13 @@ func (v *Value) Fields() ([]Field, error) { fields := []Field{} for it.Next() { fields = append(fields, Field{ - Label: it.Label(), - Value: v.Wrap(it.Value()), + Selector: it.Selector(), + Value: v.cc.Wrap(it.Value()), }) } sort.SliceStable(fields, func(i, j int) bool { - return fields[i].Label < fields[j].Label + return fields[i].Selector.String() < fields[j].Selector.String() }) return fields, nil @@ -140,7 +133,7 @@ func (v *Value) List() ([]*Value, error) { return nil, err } for it.Next() { - l = append(l, v.Wrap(it.Value())) + l = append(l, v.cc.Wrap(it.Value())) } return l, nil @@ -159,12 +152,12 @@ func (v *Value) Walk(before func(*Value) bool, after func(*Value)) { ) if before != nil { llBefore = func(child cue.Value) bool { - return before(v.Wrap(child)) + return before(v.cc.Wrap(child)) } } if after != nil { llAfter = func(child cue.Value) { - after(v.Wrap(child)) + after(v.cc.Wrap(child)) } } v.val.Walk(llBefore, llAfter) diff --git a/dagger/deployment.go b/dagger/deployment.go index 258c48fd..bbbb97c2 100644 --- a/dagger/deployment.go +++ b/dagger/deployment.go @@ -142,17 +142,20 @@ func (d *Environment) LocalDirs() map[string]string { } // 1. Scan the environment state // FIXME: use a common `flow` instance to avoid rescanning the tree. - src, err := compiler.InstanceMerge(d.plan, d.input) - if err != nil { - panic(err) + src := compiler.NewValue() + if err := src.FillPath(cue.MakePath(), d.plan); err != nil { + return nil + } + if err := src.FillPath(cue.MakePath(), d.input); err != nil { + return nil } flow := cueflow.New( &cueflow.Config{}, - src.CueInst(), - newTaskFunc(src.CueInst(), noOpRunner), + src.Cue(), + newTaskFunc(noOpRunner), ) for _, t := range flow.Tasks() { - v := compiler.Wrap(t.Value(), src.CueInst()) + v := compiler.Wrap(t.Value()) localdirs(v.Lookup("#up")) } @@ -173,17 +176,19 @@ func (d *Environment) Up(ctx context.Context, s Solver) error { // Reset the computed values d.computed = compiler.NewValue() - // Cueflow cue instance - src, err := compiler.InstanceMerge(d.plan, d.input) - if err != nil { + src := compiler.NewValue() + if err := src.FillPath(cue.MakePath(), d.plan); err != nil { + return err + } + if err := src.FillPath(cue.MakePath(), d.input); err != nil { return err } // Orchestrate execution with cueflow flow := cueflow.New( &cueflow.Config{}, - src.CueInst(), - newTaskFunc(src.CueInst(), newPipelineRunner(src.CueInst(), d.computed, s)), + src.Cue(), + newTaskFunc(newPipelineRunner(d.computed, s)), ) if err := flow.Run(ctx); err != nil { return err @@ -200,9 +205,9 @@ func (d *Environment) Down(ctx context.Context, _ *DownOpts) error { type QueryOpts struct{} -func newTaskFunc(inst *cue.Instance, runner cueflow.RunnerFunc) cueflow.TaskFunc { +func newTaskFunc(runner cueflow.RunnerFunc) cueflow.TaskFunc { return func(flowVal cue.Value) (cueflow.Runner, error) { - v := compiler.Wrap(flowVal, inst) + v := compiler.Wrap(flowVal) if !isComponent(v) { // No compute script return nil, nil @@ -215,7 +220,7 @@ func noOpRunner(t *cueflow.Task) error { return nil } -func newPipelineRunner(inst *cue.Instance, computed *compiler.Value, s Solver) cueflow.RunnerFunc { +func newPipelineRunner(computed *compiler.Value, s Solver) cueflow.RunnerFunc { return cueflow.RunnerFunc(func(t *cueflow.Task) error { ctx := t.Context() lg := log. @@ -239,7 +244,7 @@ func newPipelineRunner(inst *cue.Instance, computed *compiler.Value, s Solver) c Str("dependency", dep.Path().String()). Msg("dependency detected") } - v := compiler.Wrap(t.Value(), inst) + v := compiler.Wrap(t.Value()) p := NewPipeline(t.Path().String(), s) err := p.Do(ctx, v) if err != nil { diff --git a/dagger/pipeline.go b/dagger/pipeline.go index 12e178ef..80112524 100644 --- a/dagger/pipeline.go +++ b/dagger/pipeline.go @@ -382,7 +382,7 @@ func (p *Pipeline) Exec(ctx context.Context, op *compiler.Value, st llb.State) ( if err != nil { return st, err } - opts = append(opts, llb.AddEnv(env.Label, v)) + opts = append(opts, llb.AddEnv(env.Label(), v)) } } @@ -420,7 +420,7 @@ func (p *Pipeline) mountAll(ctx context.Context, mounts *compiler.Value) ([]llb. return nil, err } for _, mnt := range fields { - o, err := p.mount(ctx, mnt.Label, mnt.Value) + o, err := p.mount(ctx, mnt.Label(), mnt.Value) if err != nil { return nil, err } @@ -825,7 +825,7 @@ func dockerBuildOpts(op *compiler.Value) (map[string]string, error) { if err != nil { return nil, err } - opts["build-arg:"+buildArg.Label] = v + opts["build-arg:"+buildArg.Label()] = v } } @@ -839,7 +839,7 @@ func dockerBuildOpts(op *compiler.Value) (map[string]string, error) { if err != nil { return nil, err } - opts["label:"+label.Label] = s + opts["label:"+label.Label()] = s } }