diff --git a/pkg/gotohelm/helmette/sprig.go b/pkg/gotohelm/helmette/sprig.go index 749ccf371..c44e92152 100644 --- a/pkg/gotohelm/helmette/sprig.go +++ b/pkg/gotohelm/helmette/sprig.go @@ -247,6 +247,13 @@ func MustRegexMatch(pattern, s string) bool { return RegexMatch(pattern, s) } +// MustRegexSplit is the go equivalent of sprig's `mustRegexMatch`. +// +gotohelm:builtin=mustRegexSplit +func MustRegexSplit(pattern, s string, n int) []string { + r := regexp.MustCompile(pattern) + return r.Split(s, n) +} + // Coalesce is the go equivalent of sprig's `coalesce`. // +gotohelm:builtin=coalesce func Coalesce[T any](values ...T) T { diff --git a/pkg/gotohelm/testdata/src/example/sprig/sprig.go b/pkg/gotohelm/testdata/src/example/sprig/sprig.go index baaad27f3..d89e3f0a9 100644 --- a/pkg/gotohelm/testdata/src/example/sprig/sprig.go +++ b/pkg/gotohelm/testdata/src/example/sprig/sprig.go @@ -63,6 +63,15 @@ func regexReplaceAll() any { } } +func mustRegexSplit() [][]string { + return [][]string{ + helmette.MustRegexSplit(" ", "1 2 3 4 5", -1), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 1), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 2), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 10), + } +} + func yaml() any { return []string{ helmette.ToYaml(nil), diff --git a/pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go b/pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go index 894e59319..e4282ee88 100644 --- a/pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go +++ b/pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go @@ -64,6 +64,15 @@ func regexReplaceAll() any { } } +func mustRegexSplit() [][]string { + return [][]string{ + helmette.MustRegexSplit(" ", "1 2 3 4 5", -1), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 1), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 2), + helmette.MustRegexSplit(" ", "1 2 3 4 5", 10), + } +} + func yaml() any { return []string{ helmette.ToYaml(nil), diff --git a/pkg/gotohelm/testdata/src/example/sprig/sprig.yaml b/pkg/gotohelm/testdata/src/example/sprig/sprig.yaml index ab02fcadf..2d55ac52f 100644 --- a/pkg/gotohelm/testdata/src/example/sprig/sprig.yaml +++ b/pkg/gotohelm/testdata/src/example/sprig/sprig.yaml @@ -28,6 +28,15 @@ {{- end -}} {{- end -}} +{{- define "sprig.mustRegexSplit" -}} +{{- range $_ := (list 1) -}} +{{- $_is_returning := false -}} +{{- $_is_returning = true -}} +{{- (dict "r" (list (mustRegexSplit " " "1 2 3 4 5" -1) (mustRegexSplit " " "1 2 3 4 5" (1 | int)) (mustRegexSplit " " "1 2 3 4 5" (2 | int)) (mustRegexSplit " " "1 2 3 4 5" (10 | int)))) | toJson -}} +{{- break -}} +{{- end -}} +{{- end -}} + {{- define "sprig.yaml" -}} {{- range $_ := (list 1) -}} {{- $_is_returning := false -}} @@ -96,15 +105,15 @@ {{- define "sprig.float" -}} {{- range $_ := (list 1) -}} {{- $_is_returning := false -}} -{{- $_155_f__ := (list (float64 "3.2") nil) -}} -{{- $f := ((index $_155_f__ 0) | float64) -}} -{{- $_ := (index $_155_f__ 1) -}} -{{- $_156_integer__ := (list (float64 "3") nil) -}} -{{- $integer := ((index $_156_integer__ 0) | float64) -}} -{{- $_ := (index $_156_integer__ 1) -}} -{{- $_157_invalidInput_err := (list (float64 "abc") nil) -}} -{{- $invalidInput := ((index $_157_invalidInput_err 0) | float64) -}} -{{- $err := (index $_157_invalidInput_err 1) -}} +{{- $_164_f__ := (list (float64 "3.2") nil) -}} +{{- $f := ((index $_164_f__ 0) | float64) -}} +{{- $_ := (index $_164_f__ 1) -}} +{{- $_165_integer__ := (list (float64 "3") nil) -}} +{{- $integer := ((index $_165_integer__ 0) | float64) -}} +{{- $_ := (index $_165_integer__ 1) -}} +{{- $_166_invalidInput_err := (list (float64 "abc") nil) -}} +{{- $invalidInput := ((index $_166_invalidInput_err 0) | float64) -}} +{{- $err := (index $_166_invalidInput_err 1) -}} {{- $errorHappen := 0.3 -}} {{- if (ne (toJson $err) "null") -}} {{- end -}} @@ -126,15 +135,15 @@ {{- define "sprig.atoi" -}} {{- range $_ := (list 1) -}} {{- $_is_returning := false -}} -{{- $_180_positive__ := (list (atoi "234") nil) -}} -{{- $positive := ((index $_180_positive__ 0) | int) -}} -{{- $_ := (index $_180_positive__ 1) -}} -{{- $_181_negative__ := (list (atoi "-23") nil) -}} -{{- $negative := ((index $_181_negative__ 0) | int) -}} -{{- $_ := (index $_181_negative__ 1) -}} -{{- $_182_invalidInput_err := (list (atoi "paokwdpo") nil) -}} -{{- $invalidInput := ((index $_182_invalidInput_err 0) | int) -}} -{{- $err := (index $_182_invalidInput_err 1) -}} +{{- $_189_positive__ := (list (atoi "234") nil) -}} +{{- $positive := ((index $_189_positive__ 0) | int) -}} +{{- $_ := (index $_189_positive__ 1) -}} +{{- $_190_negative__ := (list (atoi "-23") nil) -}} +{{- $negative := ((index $_190_negative__ 0) | int) -}} +{{- $_ := (index $_190_negative__ 1) -}} +{{- $_191_invalidInput_err := (list (atoi "paokwdpo") nil) -}} +{{- $invalidInput := ((index $_191_invalidInput_err 0) | int) -}} +{{- $err := (index $_191_invalidInput_err 1) -}} {{- $errorHappen := (0 | int) -}} {{- if (ne (toJson $err) "null") -}} {{- end -}} @@ -214,12 +223,12 @@ {{- define "sprig.errTypes" -}} {{- range $_ := (list 1) -}} {{- $_is_returning := false -}} -{{- $_284_x1_err1 := (list (atoi "1") nil) -}} -{{- $x1 := ((index $_284_x1_err1 0) | int) -}} -{{- $err1 := (index $_284_x1_err1 1) -}} -{{- $_285_x2_err2 := (list (float64 "1.1") nil) -}} -{{- $x2 := ((index $_285_x2_err2 0) | float64) -}} -{{- $err2 := (index $_285_x2_err2 1) -}} +{{- $_293_x1_err1 := (list (atoi "1") nil) -}} +{{- $x1 := ((index $_293_x1_err1 0) | int) -}} +{{- $err1 := (index $_293_x1_err1 1) -}} +{{- $_294_x2_err2 := (list (float64 "1.1") nil) -}} +{{- $x2 := ((index $_294_x2_err2 0) | float64) -}} +{{- $err2 := (index $_294_x2_err2 1) -}} {{- $_is_returning = true -}} {{- (dict "r" (list (list $x1 $err1) (list $x2 $err2))) | toJson -}} {{- break -}}