Skip to content

Commit

Permalink
mosts test working, just vet remains
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed Aug 24, 2023
1 parent b6b9101 commit a7d3b28
Show file tree
Hide file tree
Showing 22 changed files with 261 additions and 188 deletions.
92 changes: 43 additions & 49 deletions lib/cuecmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func EnrichDatamodel(R *runtime.Runtime, dm *datamodel.Datamodel) error {
return nil
}

func writeOutput(val cue.Value, pkg string, opts []cue.Option, fopts []format.Option, outtype, outfile string, exs []string, escape, defaults bool) (err error) {
func writeOutput(val cue.Value, pkg string, opts []cue.Option, fopts []format.Option, outtype, outfile string, exs, schemas []string, escape, defaults, wantErrors bool) (err error) {
// fmt.Println("writeOutput", pkg, exs)
// when not set, this makes it so our loop will iterate once and output everything
if len(exs) == 0 {
Expand Down Expand Up @@ -85,46 +85,65 @@ func writeOutput(val cue.Value, pkg string, opts []cue.Option, fopts []format.Op

// error handling, so we can still process everything
hadError := false
handleErr := func(err error) {
handleErr := func(err error, ex string) {
hadError = true
if len(exs) > 1 {
fmt.Fprintln(os.Stderr, "//", ex)
}
fmt.Fprint(os.Stderr, cuetils.ExpandCueError(err))
}

// range of expressions the user desires
for _, ex := range exs {
// if more than one output, prefix with name in commment
handleStuff := func(err error, s, ex string) {
if err != nil {
handleErr(err, ex)
return
}
if len(exs) > 1 {
fmt.Fprintln(out, "//", ex)
}
fmt.Fprint(out, s)
}

// range of expressions the user desires
for _, ex := range exs {
// if more than one output, prefix with name in commment
v := getValByEx(ex, pkg, val)
if v.Err() != nil {
handleErr(v.Err())
if !wantErrors && v.Err() != nil {
handleErr(v.Err(), ex)
continue
}

if defaults {
v, _ = v.Default()
}

err = v.Validate(opts...)
if err != nil {
handleErr(err)
continue
for _, schema := range schemas {
s := getValByEx(schema, pkg, val)
// we don't ignore here because we want to actually have these schemas in the value to use
// and ignore any errors the data may have against them
if s.Err() != nil {
return fmt.Errorf("unable to find schema in value: %w\n", s.Err())
}

// keep unifying with all schemas
v = v.Unify(s)
}

if !wantErrors {
err = v.Validate(opts...)
if err != nil {
handleErr(err, ex)
continue
}
}

// we have a good(ish) value now, without basic errors

switch outtype {
case "cue":
write := func(n ast.Node) error {
write := func(n ast.Node) {
b, err := format.Node(n, fopts...)
if err != nil {
handleErr(err)
return err
}
fmt.Fprint(out, string(b))
return nil
handleStuff(err, string(b), ex)
}

// get formatted value
Expand All @@ -133,51 +152,27 @@ func writeOutput(val cue.Value, pkg string, opts []cue.Option, fopts []format.Op
syn = cuetils.ToFile(syn)

// eval / write the value
err = write(syn)
if err != nil {
handleErr(err)
continue
}
write(syn)

case "json":
b, err := gen.FormatJson(v, escape)
if err != nil {
handleErr(err)
continue
}
fmt.Fprint(out, string(b))
handleStuff(err, string(b), ex)

case "yaml":
b, err := gen.FormatYaml(v)
if err != nil {
handleErr(err)
continue
}
fmt.Fprint(out, string(b))
handleStuff(err, string(b), ex)

case "xml":
b, err := gen.FormatXml(v)
if err != nil {
handleErr(err)
continue
}
fmt.Fprintln(out, string(b))
handleStuff(err, string(b), ex)

case "toml":
b, err := gen.FormatToml(v)
if err != nil {
handleErr(err)
continue
}
fmt.Fprint(out, string(b))
handleStuff(err, string(b), ex)

case "text":
s, err := v.String()
if err != nil {
handleErr(err)
continue
}
fmt.Fprint(out, s)
handleStuff(err, s, ex)

default:
return fmt.Errorf("unknown output type %s", outtype)
Expand All @@ -193,7 +188,6 @@ func writeOutput(val cue.Value, pkg string, opts []cue.Option, fopts []format.Op
}

func getValByEx(ex, pkg string, val cue.Value) cue.Value {
// fmt.Println("EX:", ex)
if ex == "" || ex == "." {
return val
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/cuecmd/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Def(args []string, rflags flags.RootPflagpole, cflags flags.DefFlagpole) er
if bi.Module == "" {
pkg = bi.ID()
}
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, false, false)
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, rflags.Schema, false, false, false)
if err != nil {
return err
}
Expand Down
16 changes: 9 additions & 7 deletions lib/cuecmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ func Eval(args []string, rflags flags.RootPflagpole, cflags flags.EvalFlagpole)
}
}()

wantErrors := rflags.IngoreErrors || rflags.AllErrors

if err != nil {
return cuetils.ExpandCueError(err)
}

err = R.Load()
if err != nil {
fmt.Println("load.Err", err)
if err != nil && !wantErrors {
// fmt.Println("load.Err", err)
return cuetils.ExpandCueError(err)
}

val := R.Value
if val.Err() != nil {
fmt.Println("val.Err", val.Err())
// fmt.Println("val:", val.Err())
if val.Err() != nil && !wantErrors {
// fmt.Println("val.Err", val.Err())
return cuetils.ExpandCueError(val.Err())
}

Expand All @@ -50,11 +53,10 @@ func Eval(args []string, rflags flags.RootPflagpole, cflags flags.EvalFlagpole)
opts := []cue.Option{
cue.Docs(cflags.Comments),
cue.Attributes(cflags.Attributes),
cue.Definitions(true),
cue.Definitions(cflags.Definitions),
cue.Optional(cflags.Optional || cflags.All),
cue.InlineImports(cflags.InlineImports),
cue.ErrorsAsValues(rflags.IngoreErrors || rflags.AllErrors),
cue.ErrorsAsValues(wantErrors),
cue.ResolveReferences(cflags.Resolve),
}

Expand Down Expand Up @@ -85,7 +87,7 @@ func Eval(args []string, rflags flags.RootPflagpole, cflags flags.EvalFlagpole)
if bi.Module == "" {
pkg = bi.ID()
}
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, cflags.Escape, cflags.Defaults)
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, rflags.Schema, cflags.Escape, cflags.Defaults, wantErrors)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cuecmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Export(args []string, rflags flags.RootPflagpole, cflags flags.ExportFlagpo
if bi.Module == "" {
pkg = bi.ID()
}
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, cflags.Escape, false)
err = writeOutput(val, pkg, opts, fopts, cflags.Out, cflags.Outfile, cflags.Expression, rflags.Schema, cflags.Escape, false, false)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions lib/cuecmd/testdata/eval_errs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ cmp stdout expect-stdout

-- expect-stdout --
-- expect-stderr --
x.q: conflicting values "goodbye" and "hello":
./errs.cue:1:4
./errs.cue:2:4
./errs.cue:3:8
./errs.cue:3:14
bar: 2 errors in empty disjunction:
bar.a: conflicting values "str" and int (mismatched types string and int):
./errs.cue:5:10
Expand All @@ -13,11 +18,6 @@ bar.b: conflicting values 2 and string (mismatched types int and string):
./errs.cue:5:21
./errs.cue:6:6
./errs.cue:6:26
x.q: conflicting values "goodbye" and "hello":
./errs.cue:1:4
./errs.cue:2:4
./errs.cue:3:8
./errs.cue:3:14
-- errs.cue --
a: "hello"
b: "goodbye"
Expand Down
16 changes: 6 additions & 10 deletions lib/cuecmd/testdata/eval_flags.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Issue #969 (CUE)

! exec hof eval test.json -d '#D2'
cmp stderr expect-stderr2
cmp stderr expect-stderr1

! exec hof eval test.json vector.cue -d '#D1'
cmp stderr expect-stderr4
cmp stderr expect-stderr2

! exec hof eval test.json vector.cue -d '#D2'
cmp stderr expect-stderr5
cmp stderr expect-stderr3

-- test.json --
{
Expand All @@ -30,15 +30,10 @@ package Vector
}

-- expect-stderr1 --
use of -n/--name flag without a directory
unable to find schema in value: field not found: #D2
-- expect-stderr2 --
-d/--schema flag specified without a schema
unable to find schema in value: field not found: #D1
-- expect-stderr3 --
use of -n/--name flag without a directory
-- expect-stderr4 --
reference "#D1" not found:
--schema:1:1
-- expect-stderr5 --
X: conflicting values 1 and float (mismatched types int and float):
./test.json:2:8
./vector.cue:4:8
Expand All @@ -47,4 +42,5 @@ Y: conflicting values 2 and float (mismatched types int and float):
./vector.cue:5:8
Z: field not allowed:
./test.json:4:3
./vector.cue:1:1
./vector.cue:3:6
3 changes: 1 addition & 2 deletions lib/cuecmd/testdata/eval_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ val: list[3]

-- expect/stdout --
a: _|_ // a: conflicting values 5 and 4
l: [1, _|_, // l.1: conflicting values 3 and 2
]
l: [1, _|_]
list: [0, 1, 2]
val: _|_ // val: index out of range [3] with length 3
3 changes: 1 addition & 2 deletions lib/cuecmd/testdata/eval_loaderr.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
! exec hof eval non-existing .
! stdout .
cmp stderr "cannot find package \"non-existing\""

cmp stderr 'cannot find package "non-existing"'
23 changes: 8 additions & 15 deletions lib/cuecmd/testdata/eval_mixedfiletypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ exec hof eval x.cue data.json y.cue
exec hof eval x.cue data.json
cmp stdout stdout.golden

# Import JSON and verify that we can assert checks are ok
exec hof import data.json
exec hof eval x.cue data.cue y.cue

# Assert checks ok using CUE + JSON
exec hof eval x.cue data.json y.cue

-- data.json --
{
"team": {
Expand Down Expand Up @@ -49,14 +42,14 @@ checks: [string]: ok: true
-- stdout.golden --
#Team: {}
team: {
alice: ["EM"]
bob: ["TL"]
alice: ["EM"]
bob: ["TL"]
}
checks: {
enoughMembers: {
ok: true
}
hasManager: {
ok: true
}
enoughMembers: {
ok: true
}
hasManager: {
ok: true
}
}
2 changes: 2 additions & 0 deletions lib/cuecmd/testdata/eval_notexistfatal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ sortedx: error in call to list.Sort: field not found: less:
import "list"

x: [1, 2, 3]
// This should be a fatal error, as the passed struct will never be
// able to contain `less` by making the configuration more concrete.
sortedx: list.Sort(x, {})
-- expect-stderr2 --
4 changes: 2 additions & 2 deletions lib/cuecmd/testdata/eval_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cmp stdout expect-stdout

-- expect-stdout --
var: {
env: "staging"
name: "bar"
env: "staging"
name: "bar"
}
-- tags.cue --
package tags
Expand Down
13 changes: 6 additions & 7 deletions lib/cuecmd/testdata/vet_altdata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ cmp stdout export-stdout
[string]: string

-- foo.data --
{ "a": "b" }
{ "c": "d" }

-- export-stdout --
{
"a": "b",
"c": "d"
{
"a": "b",
"c": "d"
}
-- export-stdout --
a: "b"
c: "d"
Loading

0 comments on commit a7d3b28

Please sign in to comment.