From 40376bb214d2640d6489b0d21a261af653473749 Mon Sep 17 00:00:00 2001 From: Sam Alba Date: Fri, 21 May 2021 16:02:38 -0700 Subject: [PATCH] re-implemented ScanInputs Signed-off-by: Sam Alba --- dagger/environment.go | 9 ++--- dagger/inputs_scan.go | 83 +++++++++++++------------------------------ 2 files changed, 27 insertions(+), 65 deletions(-) diff --git a/dagger/environment.go b/dagger/environment.go index 8e690c4c..c0177676 100644 --- a/dagger/environment.go +++ b/dagger/environment.go @@ -296,11 +296,6 @@ func newPipelineRunner(computed *compiler.Value, s Solver) cueflow.RunnerFunc { }) } -func (e *Environment) ScanInputs() ([]*compiler.Value, error) { - vals, err := ScanInputs(e.plan) - if err != nil { - return nil, err - } - - return vals, nil +func (e *Environment) ScanInputs(ctx context.Context) []*compiler.Value { + return ScanInputs(ctx, e.plan) } diff --git a/dagger/inputs_scan.go b/dagger/inputs_scan.go index a7f7ca43..50edfa7f 100644 --- a/dagger/inputs_scan.go +++ b/dagger/inputs_scan.go @@ -1,82 +1,49 @@ package dagger import ( - "fmt" + "context" - "cuelang.org/go/cue" "dagger.io/go/dagger/compiler" + "github.com/rs/zerolog/log" ) -// func isReference(val cue.Value) bool { -// _, ref := val.ReferencePath() -// isRef := len(ref.Selectors()) > 0 +func isReference(val *compiler.Value) bool { + _, ref := val.ReferencePath() -// if isRef { -// return true -// } - -// _, vals := val.Expr() -// for _, v := range vals { -// // walk recursively -// if v.Path().String() == val.Path().String() { -// // avoid loop by checking the same value -// continue -// } -// return isReference(v) -// } - -// return isRef -// } - -// walk recursively to find references -// func isReference(val cue.Value) bool { -// _, vals := val.Expr() -// for _, v := range vals { -// // walk recursively -// if v.Path().String() == val.Path().String() { -// // avoid loop by checking the same value -// continue -// } -// return isReference(v) -// } - -// _, ref := val.ReferencePath() -// return len(ref.Selectors()) > 0 -// } - -func isReference(val cue.Value) bool { - checkRef := func(vv cue.Value) bool { - _, ref := vv.ReferencePath() - return len(ref.Selectors()) > 0 + if ref.String() == "" || val.Path().String() == ref.String() { + return false } - _, vals := val.Expr() - for _, v := range vals { - if checkRef(v) { - return true + for _, s := range ref.Selectors() { + if s.IsDefinition() { + return false } } - return checkRef(val) + return true } -func ScanInputs(value *compiler.Value) ([]*compiler.Value, error) { - vals := []*compiler.Value{} +func ScanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value { + lg := log.Ctx(ctx) + inputs := []*compiler.Value{} value.Walk( func(val *compiler.Value) bool { - // if isReference(val.Cue()) { - // fmt.Println("### isReference ->", val.Cue().Path()) - // return false - // } - - if val.HasAttr("input") && !isReference(val.Cue()) { - fmt.Printf("#### FOUND: %s\n", val.Path()) - vals = append(vals, val) + if isReference(val) { + lg.Debug().Str("value.Path", val.Path().String()).Msg("found reference, stop walk") + return false } + + if !val.HasAttr("input") { + return true + } + + lg.Debug().Str("value.Path", val.Path().String()).Msg("found input") + inputs = append(inputs, val) + return true }, nil, ) - return vals, nil + return inputs }