implemented @dagger(input) attributes to detect inputs

Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
Sam Alba 2021-05-14 17:53:23 -07:00
parent 07f87759d7
commit 54e8a6262d

View File

@ -14,40 +14,15 @@ func ScanForInputs(value cue.Value) ([]cue.Value, error) {
err error err error
) )
// walk before function, bool return is if the walk should recurse again // walk before function, bool return true if the walk should recurse again
before := func(v cue.Value) (bool, error) { before := func(v cue.Value) (bool, error) {
// explicit phase
// look for #up
label, _ := v.Label()
if label == "#up" {
return false, nil
}
// look to exclude any @dagger(computed)
attrs := v.Attributes(cue.ValueAttr)
for _, attr := range attrs {
name := attr.Name()
// match `@dagger(...)`
if name == "dagger" {
// loop over args (CSV content in attribute)
for i := 0; i < attr.NumArgs(); i++ {
key, _ := attr.Arg(i)
// we found an explicit computed value
if key == "computed" {
return false, nil
}
}
}
}
// inference phase // inference phase
switch v.IncompleteKind() { switch v.IncompleteKind() {
case cue.StructKind: case cue.StructKind:
return true, nil return true, nil
case cue.ListKind: case cue.ListKind:
if !v.IsConcrete() { if !v.IsConcrete() && isInput(v) {
vals = append(vals, v) vals = append(vals, v)
return false, nil return false, nil
} }
@ -55,6 +30,11 @@ func ScanForInputs(value cue.Value) ([]cue.Value, error) {
default: default:
if !isInput(v) {
// Not an input
return false, nil
}
// a leaf with default? // a leaf with default?
_, has := v.Default() _, has := v.Default()
if has { if has {
@ -81,6 +61,28 @@ func ScanForInputs(value cue.Value) ([]cue.Value, error) {
return vals, nil return vals, nil
} }
func isInput(v cue.Value) bool {
attrs := v.Attributes(cue.ValueAttr)
for _, attr := range attrs {
name := attr.Name()
// match `@dagger(...)`
if name == "dagger" {
// loop over args (CSV content in attribute)
for i := 0; i < attr.NumArgs(); i++ {
key, _ := attr.Arg(i)
// we found an explicit input value
if key == "input" {
return true
}
}
}
}
return false
}
// walkValue is a custome walk function so that we recurse into more types than CUE's buildin walk // walkValue is a custome walk function so that we recurse into more types than CUE's buildin walk
// specificially, we need to customize the options to val.Fields when val is a struct // specificially, we need to customize the options to val.Fields when val is a struct
func walkValue(val cue.Value, before func(cue.Value) (bool, error), after func(cue.Value) error) error { func walkValue(val cue.Value, before func(cue.Value) (bool, error), after func(cue.Value) error) error {