mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
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
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
Reference in New Issue
Block a user