From 650d6f2bb6fe93457c22ce00e9fd27463a7950e6 Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Sat, 31 Jul 2021 00:36:27 -0400 Subject: [PATCH] update more packages to cue v0.4.0 (cue.Context) Signed-off-by: Tony Worm --- lib/cuetils/print.go | 7 +------ lib/cuetils/runtime.go | 24 +++++++++--------------- lib/runtime.go | 15 ++++++--------- lib/test/api.go | 16 ++++++++++------ lib/test/from.go | 4 ++-- lib/test/test.go | 14 ++++++++------ 6 files changed, 36 insertions(+), 44 deletions(-) diff --git a/lib/cuetils/print.go b/lib/cuetils/print.go index 419d59fac..856903439 100644 --- a/lib/cuetils/print.go +++ b/lib/cuetils/print.go @@ -63,15 +63,10 @@ func ValueToSyntaxString(val cue.Value, opts ...cue.Option) (string, error) { } func (CRT *CueRuntime) ParseCueExpr(expr string) (cue.Value, error) { - inst, err := CRT.CueRuntime.Compile("", expr) - if err != nil { - return cue.Value{}, err - } - val := inst.Value() + val := CRT.CueContext.CompileString(expr) if val.Err() != nil { return val, val.Err() } - return val, nil } diff --git a/lib/cuetils/runtime.go b/lib/cuetils/runtime.go index 13ce190c8..2726c76a4 100644 --- a/lib/cuetils/runtime.go +++ b/lib/cuetils/runtime.go @@ -9,6 +9,7 @@ import ( "cuelang.org/go/cue" "cuelang.org/go/cue/build" + "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/errors" "cuelang.org/go/cue/load" ) @@ -50,7 +51,7 @@ type CueRuntime struct { Workspace string FS billy.Filesystem - CueRuntime *cue.Runtime + CueContext *cue.Context CueConfig *load.Config BuildInstances []*build.Instance CueErrors []error @@ -67,9 +68,9 @@ func (CRT *CueRuntime) ConvertToValue(in interface{}) (cue.Value, error) { if !ook { switch T := in.(type) { case string: - i, err := CRT.CueRuntime.Compile("", in) - if err != nil { - return O, err + i := CRT.CueContext.CompileString(T) + if i.Err() != nil { + return O, i.Err() } v := i.Value() if v.Err() != nil { @@ -108,7 +109,7 @@ func (CRT *CueRuntime) load() (err error) { // XXX TODO XXX // add the second arg from our runtime when implemented - CRT.CueRuntime = &cue.Runtime{} + CRT.CueContext = cuecontext.New() CRT.BuildInstances = load.Instances(CRT.Entrypoints, nil) for _, bi := range CRT.BuildInstances { // fmt.Printf("%d: start\n", i) @@ -123,9 +124,9 @@ func (CRT *CueRuntime) load() (err error) { } // Build the Instance - I, err := CRT.CueRuntime.Build(bi) - if err != nil { - es := errors.Errors(err) + V := CRT.CueContext.BuildInstance(bi) + if V.Err() != nil { + es := errors.Errors(V.Err()) // fmt.Println("BUILD ERR", es, I) for _, e := range es { errs = append(errs, e.(error)) @@ -133,14 +134,7 @@ func (CRT *CueRuntime) load() (err error) { continue } - // fmt.Println(i, "built", I) - - CRT.CueInstance = I - - // Get top level value from cuelang - V := I.Value() CRT.CueValue = V - // fmt.Println(i, "valued", V) // Decode? we want to be lazy /* diff --git a/lib/runtime.go b/lib/runtime.go index c54d515ec..c48b2bb00 100644 --- a/lib/runtime.go +++ b/lib/runtime.go @@ -8,6 +8,7 @@ import ( "time" "cuelang.org/go/cue" + "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/build" "cuelang.org/go/cue/errors" "cuelang.org/go/cue/load" @@ -30,7 +31,7 @@ type Runtime struct { verbose bool // Cue ralated - CueRT *cue.Runtime + CueCTX *cue.Context BuildInstances []*build.Instance CueInstances []*cue.Instance TopLevelValues []cue.Value @@ -46,7 +47,7 @@ func NewRuntime(entrypoints [] string, cmdflags flags.GenFlagpole) (*Runtime) { Entrypoints: entrypoints, Flagpole: cmdflags, - CueRT: &cue.Runtime{}, + CueCTX: cuecontext.New(), Generators: make(map[string]*gen.Generator), } @@ -72,19 +73,15 @@ func (R *Runtime) LoadCue() []error { } // Build the Instance - I, err := R.CueRT.Build(bi) - if err != nil { - es := errors.Errors(err) + V := R.CueCTX.BuildInstance(bi) + if V.Err() != nil { + es := errors.Errors(V.Err()) // fmt.Println("BUILD ERR", es, I) for _, e := range es { errs = append(errs, e.(error)) } continue } - R.CueInstances = append(R.CueInstances, I) - - // Get top level value from cuelang - V := I.Value() R.TopLevelValues = append(R.TopLevelValues, V) // Get top level struct from cuelang diff --git a/lib/test/api.go b/lib/test/api.go index 624d6f58b..ab62140d8 100644 --- a/lib/test/api.go +++ b/lib/test/api.go @@ -2,12 +2,13 @@ package test import ( "fmt" + // "encoding/json" "io/ioutil" "strings" "time" "cuelang.org/go/cue" - "cuelang.org/go/encoding/json" + // "cuelang.org/go/encoding/json" "github.com/parnurzeal/gorequest" "github.com/hofstadter-io/hof/lib/cuetils" @@ -237,12 +238,16 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue return err } - inst, err := json.Decode(T.CRT, "", body) - if err != nil { - return err + V := T.CTX.CompileBytes(body, cue.Filename("body.json")) + if V.Err() != nil { + return V.Err() } - V := inst.Value() + //inst, err := json.Decode(T.CRT, "", body) + + //V := inst.Value() + + // TODO: bi-directional subsume to check for equality? result := value.Unify(V) if result.Err() != nil { return result.Err() @@ -251,7 +256,6 @@ func checkResponse(T *Tester, verbose int, actual gorequest.Response, expect cue err = result.Validate() if err != nil { fmt.Println(value) - fmt.Println(inst.Value()) return err } diff --git a/lib/test/from.go b/lib/test/from.go index 04d9c0896..37cd29827 100644 --- a/lib/test/from.go +++ b/lib/test/from.go @@ -34,14 +34,14 @@ func RunTestFromArgsFlags(args []string, cmdflags flags.TestFlagpole) (error) { } // Get test suites from top level - suites, err := getValueTestSuites(crt.CueRuntime, crt.CueValue, cmdflags.Suite) + suites, err := getValueTestSuites(crt.CueContext, crt.CueValue, cmdflags.Suite) if err != nil { return err } // find tests in suites for s, suite := range suites { - ts, err := getValueTestSuiteTesters(crt.CueRuntime, suite.Value, cmdflags.Tester) + ts, err := getValueTestSuiteTesters(crt.CueContext, suite.Value, cmdflags.Tester) if err != nil { return err } diff --git a/lib/test/test.go b/lib/test/test.go index 0e81bb0d1..18be98f3b 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -23,7 +23,7 @@ type Suite struct { Name string // Runtime the Cue values were built from - CRT *cue.Runtime + CTX *cue.Context // Cue Value for the Suite Value cue.Value @@ -41,11 +41,11 @@ type Suite struct { Errors []error } -func getValueTestSuites(crt *cue.Runtime, val cue.Value, labels []string) ([]Suite, error) { +func getValueTestSuites(ctx *cue.Context, val cue.Value, labels []string) ([]Suite, error) { vals, err := cuetils.GetByAttrKeys(val, "test", append(labels, "suite"), nil) suites := []Suite{} for _, v := range vals { - suites = append(suites, Suite{Name: v.Key, CRT: crt, Value: v.Val}) + suites = append(suites, Suite{Name: v.Key, CTX: ctx, Value: v.Val}) } return suites, err } @@ -59,13 +59,15 @@ type Tester struct { Type string // Runtime the Cue values were built from - CRT *cue.Runtime + CTX *cue.Context // Cue Value for the Tester Value cue.Value // Execution output Output string + //Stdout string + //Stderr string // pass/fail/skip stats Stats Stats @@ -74,7 +76,7 @@ type Tester struct { Errors []error } -func getValueTestSuiteTesters(crt *cue.Runtime, val cue.Value, labels []string) ([]Tester, error) { +func getValueTestSuiteTesters(ctx *cue.Context, val cue.Value, labels []string) ([]Tester, error) { vals, err := cuetils.GetByAttrKeys(val, "test", labels, []string{}) testers := []Tester{} for _, v := range vals { @@ -83,7 +85,7 @@ func getValueTestSuiteTesters(crt *cue.Runtime, val cue.Value, labels []string) if err != nil { return testers, err } - testers = append(testers, Tester{Name: v.Key, Type: typ, CRT: crt, Value: v.Val}) + testers = append(testers, Tester{Name: v.Key, Type: typ, CTX: ctx, Value: v.Val}) } return testers, err }