From a23cb164a9545d8085a55f78999af8048424f69c Mon Sep 17 00:00:00 2001 From: Mariano Cano Date: Wed, 3 Jul 2024 11:02:24 -0700 Subject: [PATCH] Rename toLayout to toTimeLayout --- internal/templates/funcmap.go | 18 +++++++++--------- internal/templates/funcmap_test.go | 10 ++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/templates/funcmap.go b/internal/templates/funcmap.go index bb570c01..a1c03ded 100644 --- a/internal/templates/funcmap.go +++ b/internal/templates/funcmap.go @@ -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. // @@ -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 @@ -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 } @@ -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 @@ -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 } diff --git a/internal/templates/funcmap_test.go b/internal/templates/funcmap_test.go index 4309ae21..659d6184 100644 --- a/internal/templates/funcmap_test.go +++ b/internal/templates/funcmap_test.go @@ -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 } @@ -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)) }) } } @@ -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}, @@ -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},