pull/11986/merge
Ehsan Karimi 1 year ago committed by GitHub
commit 2eb83910da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -21,6 +21,7 @@ import (
"encoding/json" "encoding/json"
"strings" "strings"
"text/template" "text/template"
"time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
@ -30,8 +31,8 @@ import (
// funcMap returns a mapping of all of the functions that Engine has. // funcMap returns a mapping of all of the functions that Engine has.
// //
// Because some functions are late-bound (e.g. contain context-sensitive // Because some functions are late-bound (e.g. contain context-sensitive
// data), the functions may not all perform identically outside of an Engine // data), the functions may not all perform identically outside an Engine
// as they will inside of an Engine. // as they will inside an Engine.
// //
// Known late-bound functions: // Known late-bound functions:
// //
@ -47,13 +48,15 @@ func funcMap() template.FuncMap {
// Add some extra functionality // Add some extra functionality
extra := template.FuncMap{ extra := template.FuncMap{
"toToml": toTOML, "toToml": toTOML,
"toYaml": toYAML, "toYaml": toYAML,
"fromYaml": fromYAML, "fromYaml": fromYAML,
"fromYamlArray": fromYAMLArray, "fromYamlArray": fromYAMLArray,
"toJson": toJSON, "toJson": toJSON,
"fromJson": fromJSON, "fromJson": fromJSON,
"fromJsonArray": fromJSONArray, "fromJsonArray": fromJSONArray,
"toSeconds": toSeconds,
"toMilliSeconds": toMilliSeconds,
// This is a placeholder for the "include" function, which is // This is a placeholder for the "include" function, which is
// late-bound to a template. By declaring it here, we preserve the // late-bound to a template. By declaring it here, we preserve the
@ -82,7 +85,7 @@ func funcMap() template.FuncMap {
func toYAML(v interface{}) string { func toYAML(v interface{}) string {
data, err := yaml.Marshal(v) data, err := yaml.Marshal(v)
if err != nil { if err != nil {
// Swallow errors inside of a template. // Swallow errors inside a template.
return "" return ""
} }
return strings.TrimSuffix(string(data), "\n") return strings.TrimSuffix(string(data), "\n")
@ -139,7 +142,7 @@ func toTOML(v interface{}) string {
func toJSON(v interface{}) string { func toJSON(v interface{}) string {
data, err := json.Marshal(v) data, err := json.Marshal(v)
if err != nil { if err != nil {
// Swallow errors inside of a template. // Swallow errors inside a template.
return "" return ""
} }
return string(data) return string(data)
@ -174,3 +177,27 @@ func fromJSONArray(str string) []interface{} {
} }
return a return a
} }
// toSeconds converts a duration string to seconds
//
// For example for 1h it will be 3600
func toSeconds(str string) float64 {
duration, err := time.ParseDuration(str)
if err != nil {
// Swallow errors inside a template.
return 0
}
return duration.Seconds()
}
// toMilliSeconds converts a duration string to milliseconds
//
// For example for 1h it will be 3600000
func toMilliSeconds(str string) int64 {
duration, err := time.ParseDuration(str)
if err != nil {
// Swallow errors inside a template.
return 0
}
return duration.Milliseconds()
}

@ -99,6 +99,14 @@ func TestFuncs(t *testing.T) {
tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`, tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`,
expect: `map[]`, expect: `map[]`,
vars: `["one", "two"]`, vars: `["one", "two"]`,
}, {
tpl: `{{ toSeconds .duration }}`,
expect: "3600",
vars: map[string]interface{}{"duration": "1h"},
}, {
tpl: `{{ toMilliSeconds .duration }}`,
expect: "3600000",
vars: map[string]interface{}{"duration": "1h"},
}} }}
for _, tt := range tests { for _, tt := range tests {

Loading…
Cancel
Save