Skip to content

Commit

Permalink
cue/encoding/jsonschema: fix staticcheck errors
Browse files Browse the repository at this point in the history
This fixes various errors found by staticcheck:

  - an unused method is removed
  - various usages of cue.Runtime are replaced by cue.Context
  - the default case of a switch statement is moved to be last

Updates #2480.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I0945d3c63fcd581aa7a6f198bc74fc1a56841b5c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196248
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
NoamTD authored and mvdan committed Jun 13, 2024
1 parent b44f02f commit b095ecb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
10 changes: 5 additions & 5 deletions encoding/jsonschema/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func (d *decoder) decode(v cue.Value) *ast.File {
if ref == nil {
return f
}
i, err := v.Lookup(ref...).Fields()
var selectors []cue.Selector
for _, r := range ref {
selectors = append(selectors, cue.Str(r))
}
i, err := v.LookupPath(cue.MakePath(selectors...)).Fields()
if err != nil {
d.errs = errors.Append(d.errs, errors.Promote(err, ""))
return nil
Expand Down Expand Up @@ -198,10 +202,6 @@ func (d *decoder) uint(n cue.Value) ast.Expr {
return n.Syntax(cue.Final()).(ast.Expr)
}

func (d *decoder) bool(n cue.Value) ast.Expr {
return n.Syntax(cue.Final()).(ast.Expr)
}

func (d *decoder) boolValue(n cue.Value) bool {
x, err := n.Bool()
if err != nil {
Expand Down
34 changes: 23 additions & 11 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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/token"
Expand Down Expand Up @@ -68,18 +69,26 @@ func TestDecode(t *testing.T) {
}
}

r := &cue.Runtime{}
var in *cue.Instance
ctx := cuecontext.New()
var v cue.Value
var out, errout []byte
outIndex := -1
errIndex := -1

for i, f := range a.Files {
switch path.Ext(f.Name) {
case ".json":
in, err = json.Decode(r, f.Name, f.Data)
expr, err := json.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildExpr(expr)
case ".yaml":
in, err = yaml.Decode(r, f.Name, f.Data)
file, err := yaml.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildFile(file)
case ".cue":
out = f.Data
outIndex = i
Expand All @@ -94,7 +103,7 @@ func TestDecode(t *testing.T) {

updated := false

expr, err := Extract(in, cfg)
expr, err := Extract(v, cfg)
if err != nil {
got := []byte(errors.Details(err, nil))

Expand All @@ -120,7 +129,8 @@ func TestDecode(t *testing.T) {

// verify the generated CUE.
if !bytes.Contains(a.Comment, []byte("#noverify")) {
if _, err = r.Compile(fullpath, b); err != nil {
v := ctx.CompileBytes(b, cue.Filename(fullpath))
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}
}
Expand Down Expand Up @@ -160,26 +170,28 @@ func TestX(t *testing.T) {

a := txtar.Parse([]byte(data))

r := &cue.Runtime{}
var in *cue.Instance
ctx := cuecontext.New()
var v cue.Value
var err error
for _, f := range a.Files {
switch path.Ext(f.Name) {
case ".json":
in, err = json.Decode(r, f.Name, f.Data)
expr, err := json.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildExpr(expr)
case ".yaml":
in, err = yaml.Decode(r, f.Name, f.Data)
file, err := yaml.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildFile(file)
}
}

cfg := &Config{ID: "test"}
expr, err := Extract(in, cfg)
expr, err := Extract(v, cfg)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions encoding/jsonschema/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@ func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string

return label{a[1], false}, a[2:]

default:
return label{elem, false}, a[1:]

case "additionalProperties",
"patternProperties",
"items",
Expand All @@ -278,6 +275,9 @@ func (s *state) getNextSelector(v cue.Value, a []string) (l label, tail []string

// Other known fields cannot be supported.
return label{}, nil

default:
return label{elem, false}, a[1:]
}
}

Expand Down

0 comments on commit b095ecb

Please sign in to comment.