Skip to content

Commit

Permalink
cue: move LookupPath to query
Browse files Browse the repository at this point in the history
refactor to move all query-related logic to query.go
Prepares for query extension, among other things.

Change-Id: I753ccdfa98600d64443add4008d7cc3570f35c1e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9344
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed Apr 8, 2021
1 parent 460357b commit 6a1ae9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
27 changes: 27 additions & 0 deletions cue/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,30 @@ func resolveExpr(ctx *context, v *adt.Vertex, x ast.Expr) adt.Value {
}
return adt.Resolve(ctx.opCtx, c)
}

// LookupPath reports the value for path p relative to v.
func (v Value) LookupPath(p Path) Value {
n := v.v
outer:
for _, sel := range p.path {
f := sel.sel.feature(v.idx.Runtime)
for _, a := range n.Arcs {
if a.Label == f {
n = a
continue outer
}
}
// TODO: if optional, look up template for name.

var x *adt.Bottom
if err, ok := sel.sel.(pathError); ok {
x = &adt.Bottom{Err: err.Error}
} else {
// TODO: better message.
x = v.idx.mkErr(n, codeNotExist, "value %q not found", sel.sel)
}
v := makeValue(v.idx, n)
return newErrValue(v, x)
}
return makeValue(v.idx, n)
}
29 changes: 1 addition & 28 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,38 +1506,11 @@ func (v Value) Path() Path {
return Path{path: a}
}

// LookupPath reports the value for path p relative to v.
func (v Value) LookupPath(p Path) Value {
n := v.v
outer:
for _, sel := range p.path {
f := sel.sel.feature(v.idx.Runtime)
for _, a := range n.Arcs {
if a.Label == f {
n = a
continue outer
}
}
// TODO: if optional, look up template for name.

var x *adt.Bottom
if err, ok := sel.sel.(pathError); ok {
x = &adt.Bottom{Err: err.Error}
} else {
// TODO: better message.
x = v.idx.mkErr(n, codeNotExist, "value %q not found", sel.sel)
}
v := makeValue(v.idx, n)
return newErrValue(v, x)
}
return makeValue(v.idx, n)
}

// LookupDef reports the definition with the given name within struct v. The
// Exists method of the returned value will report false if the definition did
// not exist. The Err method reports if any error occurred during evaluation.
//
// Deprecated: use lookupPath.
// Deprecated: use LookupPath.
func (v Value) LookupDef(name string) Value {
ctx := v.ctx()
o, err := v.structValFull(ctx)
Expand Down

0 comments on commit 6a1ae9c

Please sign in to comment.