mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
@@ -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]]|")
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user