re-implemented ScanInputs

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-05-21 16:02:38 -07:00
parent df564dd877
commit 40376bb214
2 changed files with 27 additions and 65 deletions

View File

@ -296,11 +296,6 @@ func newPipelineRunner(computed *compiler.Value, s Solver) cueflow.RunnerFunc {
}) })
} }
func (e *Environment) ScanInputs() ([]*compiler.Value, error) { func (e *Environment) ScanInputs(ctx context.Context) []*compiler.Value {
vals, err := ScanInputs(e.plan) return ScanInputs(ctx, e.plan)
if err != nil {
return nil, err
}
return vals, nil
} }

View File

@ -1,82 +1,49 @@
package dagger package dagger
import ( import (
"fmt" "context"
"cuelang.org/go/cue"
"dagger.io/go/dagger/compiler" "dagger.io/go/dagger/compiler"
"github.com/rs/zerolog/log"
) )
// func isReference(val cue.Value) bool { func isReference(val *compiler.Value) bool {
// _, ref := val.ReferencePath() _, ref := val.ReferencePath()
// isRef := len(ref.Selectors()) > 0
// if isRef { if ref.String() == "" || val.Path().String() == ref.String() {
// return true return false
// }
// _, 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
} }
_, vals := val.Expr() for _, s := range ref.Selectors() {
for _, v := range vals { if s.IsDefinition() {
if checkRef(v) { return false
return true
} }
} }
return checkRef(val) return true
} }
func ScanInputs(value *compiler.Value) ([]*compiler.Value, error) { func ScanInputs(ctx context.Context, value *compiler.Value) []*compiler.Value {
vals := []*compiler.Value{} lg := log.Ctx(ctx)
inputs := []*compiler.Value{}
value.Walk( value.Walk(
func(val *compiler.Value) bool { func(val *compiler.Value) bool {
// if isReference(val.Cue()) { if isReference(val) {
// fmt.Println("### isReference ->", val.Cue().Path()) lg.Debug().Str("value.Path", val.Path().String()).Msg("found reference, stop walk")
// return false return false
// }
if val.HasAttr("input") && !isReference(val.Cue()) {
fmt.Printf("#### FOUND: %s\n", val.Path())
vals = append(vals, val)
} }
if !val.HasAttr("input") {
return true
}
lg.Debug().Str("value.Path", val.Path().String()).Msg("found input")
inputs = append(inputs, val)
return true return true
}, nil, }, nil,
) )
return vals, nil return inputs
} }