Skip to content

Commit df4f80d

Browse files
committed
Fix where with uint64
Fixes #14081
1 parent d4c7888 commit df4f80d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

‎parser/metadecoders/decoder_integration_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,38 @@ Sorted: {{ sort $mydata "weight" }}|
4242

4343
b.AssertFileContent("public/index.html", "Sorted: [map[weight:1] map[weight:2] map[weight:3] map[weight:4]]|")
4444
}
45+
46+
func TestYAMLIntegerWhere(t *testing.T) {
47+
files := `
48+
-- assets/mydata.yaml --
49+
a:
50+
weight: 1
51+
x:
52+
weight: 2
53+
c:
54+
weight: 3
55+
t:
56+
weight: 4
57+
-- assets/myslice.yaml --
58+
- weight: 1
59+
name: one
60+
- weight: 2
61+
name: two
62+
- weight: 3
63+
name: three
64+
- weight: 4
65+
name: four
66+
67+
-- layouts/all.html --
68+
{{ $mydata1 := resources.Get "mydata.yaml" | transform.Unmarshal }}
69+
{{ $myslice := resources.Get "myslice.yaml" | transform.Unmarshal }}
70+
{{ $filtered := where $myslice "weight" "ge" $mydata1.x.weight }}
71+
mydata1: {{ $mydata1 }}|
72+
Filtered: {{ $filtered }}|
73+
74+
`
75+
76+
b := hugolib.Test(t, files)
77+
78+
b.AssertFileContent("public/index.html", "[map[name:two weight:2] map[name:three weight:3] map[name:four weight:4]]|")
79+
}

‎tpl/collections/where.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
100100
ivp = &iv
101101
imv := mv.Int()
102102
imvp = &imv
103+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
104+
iv := int64(v.Uint())
105+
ivp = &iv
106+
imv := int64(mv.Uint())
107+
imvp = &imv
103108
case reflect.String:
104109
sv := v.String()
105110
svp = &sv
@@ -495,6 +500,8 @@ func toFloat(v reflect.Value) (float64, error) {
495500
return v.Float(), nil
496501
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
497502
return v.Convert(reflect.TypeOf(float64(0))).Float(), nil
503+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
504+
return v.Convert(reflect.TypeOf(float64(0))).Float(), nil
498505
case reflect.Interface:
499506
return toFloat(v.Elem())
500507
}

‎tpl/collections/where_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"testing"
2424
"time"
2525

26+
qt "github.com/frankban/quicktest"
2627
"github.com/gohugoio/hugo/common/maps"
2728
)
2829

@@ -862,6 +863,32 @@ func TestEvaluateSubElem(t *testing.T) {
862863
}
863864
}
864865

866+
func TestToFloat(t *testing.T) {
867+
t.Parallel()
868+
869+
c := qt.New(t)
870+
871+
to := func(v any) float64 {
872+
f, err := toFloat(reflect.ValueOf(v))
873+
c.Assert(err, qt.IsNil)
874+
return f
875+
}
876+
877+
c.Assert(to(uint64(32)), qt.Equals, 32.0)
878+
c.Assert(to(int64(32)), qt.Equals, 32.0)
879+
c.Assert(to(uint32(32)), qt.Equals, 32.0)
880+
c.Assert(to(int32(32)), qt.Equals, 32.0)
881+
882+
c.Assert(to(uint16(32)), qt.Equals, 32.0)
883+
c.Assert(to(int16(32)), qt.Equals, 32.0)
884+
885+
c.Assert(to(uint8(32)), qt.Equals, 32.0)
886+
c.Assert(to(int8(32)), qt.Equals, 32.0)
887+
888+
c.Assert(to(uint(32)), qt.Equals, 32.0)
889+
c.Assert(to(int(32)), qt.Equals, 32.0)
890+
}
891+
865892
func BenchmarkWhereOps(b *testing.B) {
866893
ns := newNs()
867894
var seq []map[string]string

0 commit comments

Comments
 (0)