@ -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:
//
//
@ -54,6 +55,8 @@ func funcMap() template.FuncMap {
"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 ( )
}