Skip to content

Commit

Permalink
update more packages to cue v0.4.0 (cue.Context)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Jul 31, 2021
1 parent a5227d7 commit 650d6f2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
7 changes: 1 addition & 6 deletions lib/cuetils/print.go
Expand Up @@ -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
}

Expand Down
24 changes: 9 additions & 15 deletions lib/cuetils/runtime.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -123,24 +124,17 @@ 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))
}
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
/*
Expand Down
15 changes: 6 additions & 9 deletions lib/runtime.go
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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),
}
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions lib/test/api.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/test/from.go
Expand Up @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions lib/test/test.go
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 650d6f2

Please sign in to comment.