cue: simplify lookup wrappers

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-31 13:40:46 -07:00
parent a8b41c06b7
commit 993b96dcb4
5 changed files with 52 additions and 70 deletions

View File

@ -11,8 +11,8 @@ func TestFieldNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: "bar"`)
require.NoError(t, err)
require.True(t, root.Get("foo").Exists())
require.False(t, root.Get("bar").Exists())
require.True(t, root.Lookup("foo").Exists())
require.False(t, root.Lookup("bar").Exists())
}
// Test that a non-existing definition is detected correctly
@ -20,8 +20,8 @@ func TestDefNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: #bla: "bar"`)
require.NoError(t, err)
require.True(t, root.Get("foo.#bla").Exists())
require.False(t, root.Get("foo.#nope").Exists())
require.True(t, root.Lookup("foo.#bla").Exists())
require.False(t, root.Lookup("foo.#nope").Exists())
}
func TestJSON(t *testing.T) {
@ -30,6 +30,6 @@ func TestJSON(t *testing.T) {
require.NoError(t, err)
require.Equal(t, `{"foo":{"hello":"world"}}`, string(v.JSON()))
// Reproduce a bug where Value.Get().JSON() ignores Get()
require.Equal(t, `{"hello":"world"}`, string(v.Get("foo").JSON()))
// Reproduce a bug where Value.Lookup().JSON() ignores Lookup()
require.Equal(t, `{"hello":"world"}`, string(v.Lookup("foo").JSON()))
}

View File

@ -1,22 +0,0 @@
package compiler
import (
"cuelang.org/go/cue"
)
func cueStringsToCuePath(parts ...string) cue.Path {
selectors := make([]cue.Selector, 0, len(parts))
for _, part := range parts {
selectors = append(selectors, cue.Str(part))
}
return cue.MakePath(selectors...)
}
func cuePathToStrings(p cue.Path) []string {
selectors := p.Selectors()
out := make([]string, len(selectors))
for i, sel := range selectors {
out[i] = sel.String()
}
return out
}

View File

@ -55,13 +55,8 @@ func (v *Value) LookupPath(p cue.Path) *Value {
}
// Lookup is a helper function to lookup by path parts.
func (v *Value) Lookup(path ...string) *Value {
return v.LookupPath(cueStringsToCuePath(path...))
}
// Get is a helper function to lookup by path string
func (v *Value) Get(target string) *Value {
return v.LookupPath(cue.ParsePath(target))
func (v *Value) Lookup(path string) *Value {
return v.LookupPath(cue.ParsePath(path))
}
// Proxy function to the underlying cue.Value
@ -174,6 +169,15 @@ func (v *Value) Walk(before func(*Value) bool, after func(*Value)) {
// Contrast with cue.Value.MarshalJSON which requires all values
// to be concrete.
func (v *Value) JSON() JSON {
cuePathToStrings := func(p cue.Path) []string {
selectors := p.Selectors()
out := make([]string, len(selectors))
for i, sel := range selectors {
out[i] = sel.String()
}
return out
}
var out JSON
v.val.Walk(
func(v cue.Value) bool {

View File

@ -176,14 +176,14 @@ func (d *Deployment) LocalDirs() map[string]string {
localdirs := func(code ...*compiler.Value) {
Analyze(
func(op *compiler.Value) error {
do, err := op.Get("do").String()
do, err := op.Lookup("do").String()
if err != nil {
return err
}
if do != "local" {
return nil
}
dir, err := op.Get("dir").String()
dir, err := op.Lookup("dir").String()
if err != nil {
return err
}
@ -199,7 +199,7 @@ func (d *Deployment) LocalDirs() map[string]string {
flow := cueflow.New(&cueflow.Config{}, inst, newTaskFunc(inst, noOpRunner))
for _, t := range flow.Tasks() {
v := compiler.Wrap(t.Value(), inst)
localdirs(v.Get("#compute"))
localdirs(v.Lookup("#compute"))
}
// 2. Scan the plan

View File

@ -61,7 +61,7 @@ func (p *Pipeline) FS() fs.FS {
}
func isComponent(v *compiler.Value) bool {
return v.Get("#compute").Exists()
return v.Lookup("#compute").Exists()
}
func ops(code ...*compiler.Value) ([]*compiler.Value, error) {
@ -70,14 +70,14 @@ func ops(code ...*compiler.Value) ([]*compiler.Value, error) {
for _, x := range code {
// 1. attachment array
if isComponent(x) {
xops, err := x.Get("#compute").List()
xops, err := x.Lookup("#compute").List()
if err != nil {
return nil, err
}
// 'from' has an executable attached
ops = append(ops, xops...)
// 2. individual op
} else if _, err := x.Get("do").String(); err == nil {
} else if _, err := x.Lookup("do").String(); err == nil {
ops = append(ops, x)
// 3. op array
} else if xops, err := x.List(); err == nil {
@ -111,20 +111,20 @@ func analyzeOp(fn func(*compiler.Value) error, op *compiler.Value) error {
if err := fn(op); err != nil {
return err
}
do, err := op.Get("do").String()
do, err := op.Lookup("do").String()
if err != nil {
return err
}
switch do {
case "load", "copy":
return Analyze(fn, op.Get("from"))
return Analyze(fn, op.Lookup("from"))
case "exec":
fields, err := op.Get("mount").Fields()
fields, err := op.Lookup("mount").Fields()
if err != nil {
return err
}
for _, mnt := range fields {
if from := mnt.Value.Get("from"); from.Exists() {
if from := mnt.Value.Lookup("from"); from.Exists() {
return Analyze(fn, from)
}
}
@ -174,7 +174,7 @@ func (p *Pipeline) Do(ctx context.Context, code ...*compiler.Value) error {
}
func (p *Pipeline) doOp(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
do, err := op.Get("do").String()
do, err := op.Lookup("do").String()
if err != nil {
return st, err
}
@ -218,7 +218,7 @@ func (p *Pipeline) Subdir(ctx context.Context, op *compiler.Value, st llb.State)
// FIXME: this could be more optimized by carrying subdir path as metadata,
// and using it in copy, load or mount.
dir, err := op.Get("dir").String()
dir, err := op.Lookup("dir").String()
if err != nil {
return st, err
}
@ -237,17 +237,17 @@ func (p *Pipeline) Subdir(ctx context.Context, op *compiler.Value, st llb.State)
func (p *Pipeline) Copy(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
// Decode copy options
src, err := op.Get("src").String()
src, err := op.Lookup("src").String()
if err != nil {
return st, err
}
dest, err := op.Get("dest").String()
dest, err := op.Lookup("dest").String()
if err != nil {
return st, err
}
// Execute 'from' in a tmp pipeline, and use the resulting fs
from := NewPipeline(op.Get("from").Path().String(), p.s, nil)
if err := from.Do(ctx, op.Get("from")); err != nil {
from := NewPipeline(op.Lookup("from").Path().String(), p.s, nil)
if err := from.Do(ctx, op.Lookup("from")); err != nil {
return st, err
}
fromResult, err := from.Result()
@ -272,7 +272,7 @@ func (p *Pipeline) Copy(ctx context.Context, op *compiler.Value, st llb.State) (
}
func (p *Pipeline) Local(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
dir, err := op.Get("dir").String()
dir, err := op.Lookup("dir").String()
if err != nil {
return st, err
}
@ -362,8 +362,8 @@ func (p *Pipeline) Exec(ctx context.Context, op *compiler.Value, st llb.State) (
opts = append(opts, llb.Dir(cmd.Dir))
// env
if env := op.Get("env"); env.Exists() {
envs, err := op.Get("env").Fields()
if env := op.Lookup("env"); env.Exists() {
envs, err := op.Lookup("env").Fields()
if err != nil {
return st, err
}
@ -446,8 +446,8 @@ func (p *Pipeline) mount(ctx context.Context, dest string, mnt *compiler.Value)
}
}
// eg. mount: "/foo": { from: www.source }
from := NewPipeline(mnt.Get("from").Path().String(), p.s, nil)
if err := from.Do(ctx, mnt.Get("from")); err != nil {
from := NewPipeline(mnt.Lookup("from").Path().String(), p.s, nil)
if err := from.Do(ctx, mnt.Lookup("from")); err != nil {
return nil, err
}
fromResult, err := from.Result()
@ -468,11 +468,11 @@ func (p *Pipeline) mount(ctx context.Context, dest string, mnt *compiler.Value)
}
func (p *Pipeline) Export(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
source, err := op.Get("source").String()
source, err := op.Lookup("source").String()
if err != nil {
return st, err
}
format, err := op.Get("format").String()
format, err := op.Lookup("format").String()
if err != nil {
return st, err
}
@ -550,15 +550,15 @@ func unmarshalAnything(data []byte, fn unmarshaller) (interface{}, error) {
func (p *Pipeline) Load(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
// Execute 'from' in a tmp pipeline, and use the resulting fs
from := NewPipeline(op.Get("from").Path().String(), p.s, nil)
if err := from.Do(ctx, op.Get("from")); err != nil {
from := NewPipeline(op.Lookup("from").Path().String(), p.s, nil)
if err := from.Do(ctx, op.Lookup("from")); err != nil {
return st, err
}
return from.Result()
}
func (p *Pipeline) FetchContainer(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
rawRef, err := op.Get("ref").String()
rawRef, err := op.Lookup("ref").String()
if err != nil {
return st, err
}
@ -609,7 +609,7 @@ func parseKeyValue(env string) (string, string) {
}
func (p *Pipeline) PushContainer(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
rawRef, err := op.Get("ref").String()
rawRef, err := op.Lookup("ref").String()
if err != nil {
return st, err
}
@ -638,11 +638,11 @@ func (p *Pipeline) PushContainer(ctx context.Context, op *compiler.Value, st llb
}
func (p *Pipeline) FetchGit(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
remote, err := op.Get("remote").String()
remote, err := op.Lookup("remote").String()
if err != nil {
return st, err
}
ref, err := op.Get("ref").String()
ref, err := op.Lookup("ref").String()
if err != nil {
return st, err
}
@ -793,17 +793,17 @@ func (p *Pipeline) DockerBuild(ctx context.Context, op *compiler.Value, st llb.S
}
func (p *Pipeline) WriteFile(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
content, err := op.Get("content").String()
content, err := op.Lookup("content").String()
if err != nil {
return st, err
}
dest, err := op.Get("dest").String()
dest, err := op.Lookup("dest").String()
if err != nil {
return st, err
}
mode, err := op.Get("mode").Int64()
mode, err := op.Lookup("mode").Int64()
if err != nil {
return st, err
}
@ -815,17 +815,17 @@ func (p *Pipeline) WriteFile(ctx context.Context, op *compiler.Value, st llb.Sta
}
func (p *Pipeline) Mkdir(ctx context.Context, op *compiler.Value, st llb.State) (llb.State, error) {
path, err := op.Get("path").String()
path, err := op.Lookup("path").String()
if err != nil {
return st, err
}
dir, err := op.Get("dir").String()
dir, err := op.Lookup("dir").String()
if err != nil {
return st, err
}
mode, err := op.Get("mode").Int64()
mode, err := op.Lookup("mode").Int64()
if err != nil {
return st, err
}