Skip to content

Commit

Permalink
gotohelm: Add MustRegexSplit
Browse files Browse the repository at this point in the history
This commit adds the sprig `mustRegexSplit` function to the `helmette` package.
  • Loading branch information
chrisseto committed Jan 8, 2025
1 parent 7d0f738 commit 7000bd2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
7 changes: 7 additions & 0 deletions pkg/gotohelm/helmette/sprig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/gotohelm/testdata/src/example/sprig/sprig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 9 additions & 0 deletions pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
57 changes: 33 additions & 24 deletions pkg/gotohelm/testdata/src/example/sprig/sprig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down Expand Up @@ -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 -}}
Expand All @@ -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 -}}
Expand Down Expand Up @@ -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 -}}
Expand Down

0 comments on commit 7000bd2

Please sign in to comment.