Skip to content

Commit

Permalink
Add Min function to sprig helmette
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalKorepta committed May 24, 2024
1 parent 6904e4b commit 6b29f69
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/gotohelm/helmette/sprig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helmette
import (
"encoding/json"
"fmt"
"math"
"reflect"
"regexp"
"sort"
Expand All @@ -14,6 +15,18 @@ import (
"golang.org/x/exp/maps"
)

// Min is the go equivalent of sprig's `min`
// +gotohelm:builtin=min
func Min(in ...int) int {
result := math.MaxInt
for _, i := range in {
if i < result {
result = i
}
}
return result
}

// First function can not return `T` as sprig implementation
// will return nil if array is of the size 0.
//
Expand Down
13 changes: 13 additions & 0 deletions pkg/gotohelm/testdata/src/example/sprig/sprig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func Sprig() map[string]any {
"len": lenTest(),
"first": first(),
"toString": toString(),
"min": minFunc(),
}
}

func minFunc() []int {
return []int{
// spig Min function does not allow to not pass parameters. Error returned from helm template:
// wrong number of args for min: want at least 1 got 0
//helmette.Min(),
helmette.Min(-1, 0, 1),
helmette.Min(1),
helmette.Min(2, 1),
helmette.Min(1, 1, 2),
}
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/gotohelm/testdata/src/example/sprig/sprig.rewritten.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ func Sprig() map[string]any {
"len": lenTest(),
"first": first(),
"toString": toString(),
"min": minFunc(),
}
}

func minFunc() []int {
return []int{
// spig Min function does not allow to not pass parameters. Error returned from helm template:
// wrong number of args for min: want at least 1 got 0
//helmette.Min(),
helmette.Min(-1, 0, 1),
helmette.Min(1),
helmette.Min(2, 1),
helmette.Min(1, 1, 2),
}
}

Expand Down
9 changes: 8 additions & 1 deletion pkg/gotohelm/testdata/src/example/sprig/sprig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

{{- define "sprig.Sprig" -}}
{{- range $_ := (list 1) -}}
{{- (dict "r" (dict "concat" (get (fromJson (include "sprig.concat" (dict "a" (list ) ))) "r") "default" (get (fromJson (include "sprig.default_" (dict "a" (list ) ))) "r") "keys" (get (fromJson (include "sprig.keys" (dict "a" (list ) ))) "r") "empty" (get (fromJson (include "sprig.empty" (dict "a" (list ) ))) "r") "strings" (get (fromJson (include "sprig.stringsFunctions" (dict "a" (list ) ))) "r") "unset" (get (fromJson (include "sprig.unset" (dict "a" (list ) ))) "r") "regex" (get (fromJson (include "sprig.regex" (dict "a" (list ) ))) "r") "atoi" (get (fromJson (include "sprig.atoi" (dict "a" (list ) ))) "r") "float" (get (fromJson (include "sprig.float" (dict "a" (list ) ))) "r") "len" (get (fromJson (include "sprig.lenTest" (dict "a" (list ) ))) "r") "first" (get (fromJson (include "sprig.first" (dict "a" (list ) ))) "r") "toString" (get (fromJson (include "sprig.toString" (dict "a" (list ) ))) "r") )) | toJson -}}
{{- (dict "r" (dict "concat" (get (fromJson (include "sprig.concat" (dict "a" (list ) ))) "r") "default" (get (fromJson (include "sprig.default_" (dict "a" (list ) ))) "r") "keys" (get (fromJson (include "sprig.keys" (dict "a" (list ) ))) "r") "empty" (get (fromJson (include "sprig.empty" (dict "a" (list ) ))) "r") "strings" (get (fromJson (include "sprig.stringsFunctions" (dict "a" (list ) ))) "r") "unset" (get (fromJson (include "sprig.unset" (dict "a" (list ) ))) "r") "regex" (get (fromJson (include "sprig.regex" (dict "a" (list ) ))) "r") "atoi" (get (fromJson (include "sprig.atoi" (dict "a" (list ) ))) "r") "float" (get (fromJson (include "sprig.float" (dict "a" (list ) ))) "r") "len" (get (fromJson (include "sprig.lenTest" (dict "a" (list ) ))) "r") "first" (get (fromJson (include "sprig.first" (dict "a" (list ) ))) "r") "toString" (get (fromJson (include "sprig.toString" (dict "a" (list ) ))) "r") "min" (get (fromJson (include "sprig.minFunc" (dict "a" (list ) ))) "r") )) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}

{{- define "sprig.minFunc" -}}
{{- range $_ := (list 1) -}}
{{- (dict "r" (list (min -1 0 1) (min 1) (min 2 1) (min 1 1 2))) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}
Expand Down

0 comments on commit 6b29f69

Please sign in to comment.