From: Bjørn Erik Pedersen Date: Wed, 22 Oct 2025 16:29:40 +0000 (+0200) Subject: Fix where with uint64 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=df4f80d54f389407d935a0388707be2bf888d882;p=brevno-suite%2Fhugo Fix where with uint64 Fixes #14081 --- diff --git a/parser/metadecoders/decoder_integration_test.go b/parser/metadecoders/decoder_integration_test.go index 9c8a15e89..9da1ee17d 100644 --- a/parser/metadecoders/decoder_integration_test.go +++ b/parser/metadecoders/decoder_integration_test.go @@ -42,3 +42,38 @@ Sorted: {{ sort $mydata "weight" }}| b.AssertFileContent("public/index.html", "Sorted: [map[weight:1] map[weight:2] map[weight:3] map[weight:4]]|") } + +func TestYAMLIntegerWhere(t *testing.T) { + files := ` +-- assets/mydata.yaml -- +a: + weight: 1 +x: + weight: 2 +c: + weight: 3 +t: + weight: 4 +-- assets/myslice.yaml -- +- weight: 1 + name: one +- weight: 2 + name: two +- weight: 3 + name: three +- weight: 4 + name: four + +-- layouts/all.html -- +{{ $mydata1 := resources.Get "mydata.yaml" | transform.Unmarshal }} +{{ $myslice := resources.Get "myslice.yaml" | transform.Unmarshal }} +{{ $filtered := where $myslice "weight" "ge" $mydata1.x.weight }} +mydata1: {{ $mydata1 }}| +Filtered: {{ $filtered }}| + +` + + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.html", "[map[name:two weight:2] map[name:three weight:3] map[name:four weight:4]]|") +} diff --git a/tpl/collections/where.go b/tpl/collections/where.go index ee49d0bbb..34bfa057a 100644 --- a/tpl/collections/where.go +++ b/tpl/collections/where.go @@ -100,6 +100,11 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error ivp = &iv imv := mv.Int() imvp = &imv + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + iv := int64(v.Uint()) + ivp = &iv + imv := int64(mv.Uint()) + imvp = &imv case reflect.String: sv := v.String() svp = &sv @@ -495,6 +500,8 @@ func toFloat(v reflect.Value) (float64, error) { return v.Float(), nil case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return v.Convert(reflect.TypeOf(float64(0))).Float(), nil + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return v.Convert(reflect.TypeOf(float64(0))).Float(), nil case reflect.Interface: return toFloat(v.Elem()) } diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index ecf748f93..9d441b030 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -23,6 +23,7 @@ import ( "testing" "time" + qt "github.com/frankban/quicktest" "github.com/gohugoio/hugo/common/maps" ) @@ -862,6 +863,32 @@ func TestEvaluateSubElem(t *testing.T) { } } +func TestToFloat(t *testing.T) { + t.Parallel() + + c := qt.New(t) + + to := func(v any) float64 { + f, err := toFloat(reflect.ValueOf(v)) + c.Assert(err, qt.IsNil) + return f + } + + c.Assert(to(uint64(32)), qt.Equals, 32.0) + c.Assert(to(int64(32)), qt.Equals, 32.0) + c.Assert(to(uint32(32)), qt.Equals, 32.0) + c.Assert(to(int32(32)), qt.Equals, 32.0) + + c.Assert(to(uint16(32)), qt.Equals, 32.0) + c.Assert(to(int16(32)), qt.Equals, 32.0) + + c.Assert(to(uint8(32)), qt.Equals, 32.0) + c.Assert(to(int8(32)), qt.Equals, 32.0) + + c.Assert(to(uint(32)), qt.Equals, 32.0) + c.Assert(to(int(32)), qt.Equals, 32.0) +} + func BenchmarkWhereOps(b *testing.B) { ns := newNs() var seq []map[string]string