Skip to content

Commit

Permalink
Merge pull request #10403 from k8s-infra-cherrypick-robot/cherry-pick…
Browse files Browse the repository at this point in the history
…-10391-to-release-1.7

[release-1.7] 🌱 Add more templating func to prowjob-gen
  • Loading branch information
k8s-ci-robot authored Apr 9, 2024
2 parents 3d8ad61 + 32c477d commit cb817df
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion hack/tools/prowjob-gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ func (g *generator) templateFunctions() template.FuncMap {
funcs["TrimPrefix"] = strings.TrimPrefix
funcs["TrimSuffix"] = strings.TrimSuffix
funcs["ReplaceAll"] = strings.ReplaceAll
// function cloned from sprig
funcs["last"] = last
funcs["list"] = list
funcs["has"] = has
funcs["trim"] = strings.TrimSpace
return funcs
}

Expand All @@ -192,6 +196,42 @@ func last(list any) any {

return l2.Index(l - 1).Interface()
default:
panic(fmt.Sprintf("Cannot find last on type %s", tp))
panic(fmt.Sprintf("cannot find last on type %s", tp))
}
}

func list(v ...interface{}) []interface{} {
return v
}

func has(needle interface{}, haystack interface{}) bool {
l, err := mustHas(needle, haystack)
if err != nil {
panic(err)
}

return l
}

func mustHas(needle interface{}, haystack interface{}) (bool, error) {
if haystack == nil {
return false, nil
}
tp := reflect.TypeOf(haystack).Kind()
switch tp {
case reflect.Slice, reflect.Array:
l2 := reflect.ValueOf(haystack)
var item interface{}
l := l2.Len()
for i := 0; i < l; i++ {
item = l2.Index(i).Interface()
if reflect.DeepEqual(needle, item) {
return true, nil
}
}

return false, nil
default:
return false, fmt.Errorf("cannot find has on type %s", tp)
}
}

0 comments on commit cb817df

Please sign in to comment.