Skip to content

Commit

Permalink
Support short versions of time layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Jul 3, 2024
1 parent a23cb16 commit a87b577
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
48 changes: 22 additions & 26 deletions internal/templates/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// 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 "toTimeLayout" function
// converts strings like "time.RFC3339" or "time.UnixDate" to the actual layout
// converts strings like "time.RFC3339" or "UnixDate" to the actual layout
// 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)
// {{ toTimeLayout "time.RFC3339" }}
// {{ toTimeLayout "RFC3339" }}
// => time.RFC3339
//
// sprig "env" and "expandenv" functions are removed to avoid the leak of
Expand Down Expand Up @@ -113,49 +113,45 @@ func mustParseTime(v ...string) (time.Time, error) {
}

func toTimeLayout(fmt string) string {
if !strings.HasPrefix(fmt, "time.") {
return fmt
}

switch fmt {
case "time.Layout":
switch strings.TrimPrefix(fmt, "time.") {
case "Layout":
return time.Layout
case "time.ANSIC":
case "ANSIC":
return time.ANSIC
case "time.UnixDate":
case "UnixDate":
return time.UnixDate
case "time.RubyDate":
case "RubyDate":
return time.RubyDate
case "time.RFC822":
case "RFC822":
return time.RFC822
case "time.RFC822Z":
case "RFC822Z":
return time.RFC822Z
case "time.RFC850":
case "RFC850":
return time.RFC850
case "time.RFC1123":
case "RFC1123":
return time.RFC1123
case "time.RFC1123Z":
case "RFC1123Z":
return time.RFC1123Z
case "time.RFC3339":
case "RFC3339":
return time.RFC3339
case "time.RFC3339Nano":
case "RFC3339Nano":
return time.RFC3339Nano
// From the ones bellow, only time.DateTime will parse a complete date.
case "time.Kitchen":
case "Kitchen":
return time.Kitchen
case "time.Stamp":
case "Stamp":
return time.Stamp
case "time.StampMilli":
case "StampMilli":
return time.StampMilli
case "time.StampMicro":
case "StampMicro":
return time.StampMicro
case "time.StampNano":
case "StampNano":
return time.StampNano
case "time.DateTime":
case "DateTime":
return time.DateTime
case "time.DateOnly":
case "DateOnly":
return time.DateOnly
case "time.TimeOnly":
case "TimeOnly":
return time.TimeOnly
default:
return fmt
Expand Down
8 changes: 7 additions & 1 deletion internal/templates/funcmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"strconv"
"strings"
"testing"
"text/template"
"time"
Expand Down Expand Up @@ -170,14 +171,16 @@ func TestGetFuncMap_toTimeLayout(t *testing.T) {
{"time.DateTime", args{"time.DateTime"}, time.DateTime},
{"time.DateOnly", args{"time.DateOnly"}, time.DateOnly},
{"time.TimeOnly", args{"time.TimeOnly"}, time.TimeOnly},
{"default", args{"time.MyFormat"}, "time.MyFormat"},
{"default", args{"MyFormat"}, "MyFormat"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var failMesage string
fns := GetFuncMap(&failMesage)
toTimeLayoutFunc := fns["toTimeLayout"].(func(string) string)
assert.Equal(t, tt.want, toTimeLayoutFunc(tt.args.fmt))
fmt := strings.TrimPrefix(tt.args.fmt, "time.")
assert.Equal(t, tt.want, toTimeLayoutFunc(fmt))
})
}
}
Expand Down Expand Up @@ -224,6 +227,9 @@ func TestTemplates(t *testing.T) {
{"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},
{"toTimeLayout", args{`{{ toTimeLayout "time.RFC3339" }}`}, time.RFC3339, assert.NoError, assert.Empty},
{"toTimeLayout short", args{`{{ toTimeLayout "RFC3339" }}`}, 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},
{"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},
Expand Down

0 comments on commit a87b577

Please sign in to comment.