diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-06-06 09:48:40 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-06-07 13:02:58 +0200 |
commit | 0566bbf7c7f2898fcd1d6156b27733cd48aa0449 (patch) | |
tree | 69ee0bde4d334cb0565afd5fb4e17247c946aad9 /tpl/compare/compare.go | |
parent | 534e7155bb504682a37f5663d8c913e439b11e07 (diff) |
Fix raw TOML dates in where/eq
Note that this has only been a problem with "raw dates" in TOML files in /data and similar. The predefined front matter
dates `.Date` etc. are converted to a Go Time and has worked fine even after upgrading to v2 of the go-toml lib.
Fixes #9979
Diffstat (limited to 'tpl/compare/compare.go')
-rw-r--r-- | tpl/compare/compare.go | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go index 9905003b2..0b2d065ab 100644 --- a/tpl/compare/compare.go +++ b/tpl/compare/compare.go @@ -23,16 +23,19 @@ import ( "github.com/gohugoio/hugo/compare" "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/common/hreflect" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/types" ) // New returns a new instance of the compare-namespaced template functions. -func New(caseInsensitive bool) *Namespace { - return &Namespace{caseInsensitive: caseInsensitive} +func New(loc *time.Location, caseInsensitive bool) *Namespace { + return &Namespace{loc: loc, caseInsensitive: caseInsensitive} } // Namespace provides template functions for the "compare" namespace. type Namespace struct { + loc *time.Location // Enable to do case insensitive string compares. caseInsensitive bool } @@ -101,6 +104,11 @@ func (n *Namespace) Eq(first any, others ...any) bool { if types.IsNil(v) { return nil } + + if at, ok := v.(htime.AsTimeProvider); ok { + return at.AsTime(n.loc) + } + vv := reflect.ValueOf(v) switch vv.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: @@ -269,9 +277,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a leftStr = &str } case reflect.Struct: - switch av.Type() { - case timeType: - left = float64(toTimeUnix(av)) + if hreflect.IsTime(av.Type()) { + left = float64(ns.toTimeUnix(av)) } case reflect.Bool: left = 0 @@ -297,9 +304,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a rightStr = &str } case reflect.Struct: - switch bv.Type() { - case timeType: - right = float64(toTimeUnix(bv)) + if hreflect.IsTime(bv.Type()) { + right = float64(ns.toTimeUnix(bv)) } case reflect.Bool: right = 0 @@ -337,14 +343,10 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a return left, right } -var timeType = reflect.TypeOf((*time.Time)(nil)).Elem() - -func toTimeUnix(v reflect.Value) int64 { - if v.Kind() == reflect.Interface { - return toTimeUnix(v.Elem()) - } - if v.Type() != timeType { +func (ns *Namespace) toTimeUnix(v reflect.Value) int64 { + t, ok := hreflect.AsTime(v, ns.loc) + if !ok { panic("coding error: argument must be time.Time type reflect Value") } - return v.MethodByName("Unix").Call([]reflect.Value{})[0].Int() + return t.Unix() } |