Skip to content

Commit

Permalink
Rename toLayout to toTimeLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Jul 3, 2024
1 parent ca6ad2a commit a23cb16
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 9 additions & 9 deletions internal/templates/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

// GetFuncMap returns the list of functions provided by sprig. It adds the
// functions "formatTime", "toTime", "mustToTime", and changes the function
// "fail".
// functions "toTime", "formatTime", "parseTime", "mustParseTime",
// "toTimeLayout" and changes the function "fail".
//
// The "toTime" function receives a time or a Unix epoch and returns a time.Time
// in UTC. The "formatTime" function uses "toTime" and formats the resulting
// time using RFC3339. The functions "parseTime" and "mustParseTime" parse a
// string and return the time.Time it represents. The "toLayout" function
// string and return the time.Time it represents. The "toTimeLayout" function
// converts strings like "time.RFC3339" or "time.UnixDate" to the actual layout
// represented by the Go constant with the same nameß. The "fail" function sets
// represented by the Go constant with the same name. The "fail" function sets
// the provided message, so that template errors are reported directly to the
// template without having the wrapper that text/template adds.
//
Expand All @@ -36,7 +36,7 @@ import (
// {{ parseTime "time.UnixDate" "Tue Jul 2 16:20:48 PDT 2024" "America/Los_Angeles" }}
// => loc, _ := time.LoadLocation("America/Los_Angeles")
// time.ParseInLocation(time.UnixDate, "Tue Jul 2 16:20:48 PDT 2024", loc)
// {{ toLayout "time.RFC3339" }}
// {{ toTimeLayout "time.RFC3339" }}
// => time.RFC3339
//
// sprig "env" and "expandenv" functions are removed to avoid the leak of
Expand All @@ -53,7 +53,7 @@ func GetFuncMap(failMessage *string) template.FuncMap {
m["toTime"] = toTime
m["parseTime"] = parseTime
m["mustParseTime"] = mustParseTime
m["toLayout"] = toLayout
m["toTimeLayout"] = toTimeLayout
return m
}

Expand Down Expand Up @@ -98,10 +98,10 @@ func mustParseTime(v ...string) (time.Time, error) {
case 1:
return time.Parse(time.RFC3339, v[0])
case 2:
layout := toLayout(v[0])
layout := toTimeLayout(v[0])
return time.Parse(layout, v[1])
case 3:
layout := toLayout(v[0])
layout := toTimeLayout(v[0])
loc, err := time.LoadLocation(v[2])
if err != nil {
return time.Time{}, err
Expand All @@ -112,7 +112,7 @@ func mustParseTime(v ...string) (time.Time, error) {
}
}

func toLayout(fmt string) string {
func toTimeLayout(fmt string) string {
if !strings.HasPrefix(fmt, "time.") {
return fmt
}
Expand Down
10 changes: 6 additions & 4 deletions internal/templates/funcmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestGetFuncMap_parseTime_mustParseTime(t *testing.T) {
})
}

func TestGetFuncMap_toLayout(t *testing.T) {
func TestGetFuncMap_toTimeLayout(t *testing.T) {
type args struct {
fmt string
}
Expand Down Expand Up @@ -176,8 +176,8 @@ func TestGetFuncMap_toLayout(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var failMesage string
fns := GetFuncMap(&failMesage)
toLayoutFunc := fns["toLayout"].(func(string) string)
assert.Equal(t, tt.want, toLayoutFunc(tt.args.fmt))
toTimeLayoutFunc := fns["toTimeLayout"].(func(string) string)
assert.Equal(t, tt.want, toTimeLayoutFunc(tt.args.fmt))
})
}
}
Expand Down Expand Up @@ -213,6 +213,7 @@ func TestTemplates(t *testing.T) {
{"toTime int64", args{`{{ .nbf | toTime }}`}, now.String(), assert.NoError, assert.Empty},
{"toTime int64 toJson", args{`{{ .nbf | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty},
{"toTime float64 toJson", args{`{{ .float64 | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty},
{"toTime dateModify", args{`{{ .nbf | toTime | dateModify "1h" }}`}, now.Add(time.Hour).String(), assert.NoError, assert.Empty},
{"formatTime", args{`{{ .nbf | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty},
{"formatTime float64", args{`{{ .float64 | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty},
{"formatTime in sprig", args{`{{ dateInZone "2006-01-02T15:04:05Z07:00" .float64 "UTC" }}`}, now.UTC().Format(time.RFC3339), assert.NoError, assert.Empty},
Expand All @@ -221,8 +222,9 @@ func TestTemplates(t *testing.T) {
{"parseTime time.UnixDate", args{`{{ .notAfter | parseTime "time.UnixDate" }}`}, now.Add(time.Hour).String(), assert.NoError, assert.Empty},
{"parseTime time.UnixDate toJson", args{`{{ .notAfter | parseTime "time.UnixDate" | toJson }}`}, strconv.Quote(now.Add(time.Hour).Format(time.RFC3339)), assert.NoError, assert.Empty},
{"parseTime time.UnixDate America/Los_Angeles", args{`{{ parseTime "time.UnixDate" .notAfter "America/Los_Angeles" }}`}, now.Add(time.Hour).String(), assert.NoError, assert.Empty},
{"parseTime dateModify", args{`{{ .notBefore | parseTime | dateModify "1h" }}`}, now.Add(time.Hour).String(), assert.NoError, assert.Empty},
{"parseTime in sprig ", args{`{{ toDate "Mon Jan _2 15:04:05 MST 2006" .notAfter }}`}, now.Add(time.Hour).String(), assert.NoError, assert.Empty},
{"toTime toLayout date", args{`{{ .nbf | toTime | date (toLayout "time.RFC3339") }}`}, now.Local().Format(time.RFC3339), assert.NoError, assert.Empty},
{"toTime toTimeLayout date", args{`{{ .nbf | toTime | date (toTimeLayout "time.RFC3339") }}`}, now.Local().Format(time.RFC3339), assert.NoError, assert.Empty},
{"parseTime error", args{`{{ parseTime "time.UnixDate" .notAfter "America/FooBar" }}`}, "0001-01-01 00:00:00 +0000 UTC", assert.NoError, assert.Empty},
{"mustParseTime error", args{`{{ mustParseTime "time.UnixDate" .notAfter "America/FooBar" }}`}, "", assert.Error, assert.Empty},
{"fail", args{`{{ fail "error" }}`}, "", assert.Error, assert.NotEmpty},
Expand Down

0 comments on commit a23cb16

Please sign in to comment.