Skip to content

Commit

Permalink
all: remove various cue.Value.Lookup usages
Browse files Browse the repository at this point in the history
Replace various usages of the deprecated cue.Value.Lookup with
cue.Value.LookupPath.

Also, fix some typos and reorganize some imports.

Updates #2480.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I8faff629c07601dcb51fda527d8f10b80265bd92
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194951
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
NoamTD authored and mvdan committed May 20, 2024
1 parent 97bf1a6 commit 3818528
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
const commandSection = "command"

func lookupString(obj cue.Value, key, def string) string {
str, err := obj.Lookup(key).String()
str, err := obj.LookupPath(cue.MakePath(cue.Str(key))).String()
if err == nil {
def = str
}
Expand Down
6 changes: 3 additions & 3 deletions encoding/openapi/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func Extract(data cue.InstanceOrValue, c *Config) (*ast.File, error) {

v := data.Value()

doc, _ := v.Lookup("info", "title").String() // Required
if s, _ := v.Lookup("info", "description").String(); s != "" {
doc, _ := v.LookupPath(cue.MakePath(cue.Str("info"), cue.Str("title"))).String() // Required
if s, _ := v.LookupPath(cue.MakePath(cue.Str("info"), cue.Str("description"))).String(); s != "" {
doc += "\n\n" + s
}
cg := internal.NewComment(true, doc)
Expand Down Expand Up @@ -84,7 +84,7 @@ func Extract(data cue.InstanceOrValue, c *Config) (*ast.File, error) {
// add(internal.NewAttr("openapi", "version="+ version))
// }

if info := v.Lookup("info"); info.Exists() {
if info := v.LookupPath(cue.MakePath(cue.Str("info"))); info.Exists() {
decls := []interface{}{}
if st, ok := info.Syntax().(*ast.StructLit); ok {
// Remove title.
Expand Down
6 changes: 3 additions & 3 deletions encoding/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ func (c *Config) compose(inst cue.InstanceOrValue, schemas *ast.StructLit) (x *a
errs = errors.Append(errs, errors.Newf(i.Value().Pos(),
"info must be a struct"))
}
title, _ = i.Value().Lookup("title").String()
version, _ = i.Value().Lookup("version").String()
title, _ = i.Value().LookupPath(cue.MakePath(cue.Str("title"))).String()
version, _ = i.Value().LookupPath(cue.MakePath(cue.Str("version"))).String()

default:
errs = errors.Append(errs, errors.Newf(i.Value().Pos(),
Expand All @@ -186,7 +186,7 @@ func (c *Config) compose(inst cue.InstanceOrValue, schemas *ast.StructLit) (x *a
}

if version == "" {
version, _ = val.Lookup("$version").String()
version, _ = val.LookupPath(cue.MakePath(cue.Str("$version"))).String()
if version == "" {
version = "no version"
}
Expand Down
5 changes: 3 additions & 2 deletions encoding/protobuf/jsonpb/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"encoding/base64"
"strings"

"github.com/cockroachdb/apd/v3"

"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/protobuf/pbinternal"
"github.com/cockroachdb/apd/v3"
)

// Option is an option.
Expand Down Expand Up @@ -77,7 +78,7 @@ func NewDecoder(schema cue.Value, options ...Option) *Decoder {
// according to the protocol buffer to JSON mapping defined in the protocol
// buffer spec.
//
// RewriteFile is idempotent, calling it multiples times on an expression gives
// RewriteFile is idempotent, calling it multiple times on an expression gives
// the same result.
func (d *Decoder) RewriteFile(file *ast.File) error {
var r rewriter
Expand Down
10 changes: 5 additions & 5 deletions pkg/tool/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (c *httpCmd) Run(ctx *task.Context) (res interface{}, err error) {
u = ctx.String("url")
)
var r io.Reader
if obj := ctx.Obj.Lookup("request"); obj.Exists() {
if v := obj.Lookup("body"); v.Exists() {
if obj := ctx.Obj.LookupPath(cue.MakePath(cue.Str("request"))); obj.Exists() {
if v := obj.LookupPath(cue.MakePath(cue.Str("body"))); v.Exists() {
r, err = v.Reader()
if err != nil {
return nil, err
Expand All @@ -65,7 +65,7 @@ func (c *httpCmd) Run(ctx *task.Context) (res interface{}, err error) {
}

var caCert []byte
caCertValue := ctx.Obj.LookupPath(cue.ParsePath("tls.caCert"))
caCertValue := ctx.Obj.LookupPath(cue.MakePath(cue.Str("tls"), cue.Str("caCert")))
if caCertValue.Exists() {
caCert, err = caCertValue.Bytes()
if err != nil {
Expand All @@ -74,7 +74,7 @@ func (c *httpCmd) Run(ctx *task.Context) (res interface{}, err error) {
}

tlsVerify := true
tlsVerifyValue := ctx.Obj.LookupPath(cue.ParsePath("tls.verify"))
tlsVerifyValue := ctx.Obj.LookupPath(cue.MakePath(cue.Str("tls"), cue.Str("verify")))
if tlsVerifyValue.Exists() {
tlsVerify, err = tlsVerifyValue.Bool()
if err != nil {
Expand Down Expand Up @@ -143,7 +143,7 @@ func (c *httpCmd) Run(ctx *task.Context) (res interface{}, err error) {
}

func parseHeaders(obj cue.Value, label string) (http.Header, error) {
m := obj.Lookup(label)
m := obj.LookupPath(cue.MakePath(cue.Str(label)))
if !m.Exists() {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tool/os/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *environCmd) Run(ctx *task.Context) (res interface{}, err error) {

for _, kv := range os.Environ() {
name, str, _ := strings.Cut(kv, "=")
if v := ctx.Obj.Lookup(name); v.Exists() {
if v := ctx.Obj.LookupPath(cue.MakePath(cue.Str(name))); v.Exists() {
update[name], err = fromString(name, str, v)
if err != nil {
return nil, err
Expand Down
13 changes: 8 additions & 5 deletions tools/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,23 @@ func TestFlowValuePanic(t *testing.T) {
}

func taskFunc(v cue.Value) (flow.Runner, error) {
switch name, err := v.Lookup("$id").String(); name {
idPath := cue.MakePath(cue.Str("$id"))
valPath := cue.MakePath(cue.Str("val"))

switch name, err := v.LookupPath(idPath).String(); name {
default:
if err == nil {
return flow.RunnerFunc(func(t *flow.Task) error {
t.Fill(map[string]string{"stdout": "foo"})
return nil
}), nil
} else if v.LookupPath(cue.MakePath(cue.Str("$id"))).Exists() {
} else if v.LookupPath(idPath).Exists() {
return nil, err
}

case "valToOut":
return flow.RunnerFunc(func(t *flow.Task) error {
if str, err := t.Value().Lookup("val").String(); err == nil {
if str, err := t.Value().LookupPath(valPath).String(); err == nil {
t.Fill(map[string]string{"out": str})
}
return nil
Expand Down Expand Up @@ -216,14 +219,14 @@ func taskFunc(v cue.Value) (flow.Runner, error) {
// This task is used to serialize different runners in case
// non-deterministic scheduling is possible.
return flow.RunnerFunc(func(t *flow.Task) error {
seq, err := t.Value().Lookup("seq").Int64()
seq, err := t.Value().LookupPath(cue.MakePath(cue.Str("seq"))).Int64()
if err != nil {
return err
}

waitSeqNum(seq)

if str, err := t.Value().Lookup("val").String(); err == nil {
if str, err := t.Value().LookupPath(valPath).String(); err == nil {
t.Fill(map[string]string{"out": str})
}

Expand Down

0 comments on commit 3818528

Please sign in to comment.