Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Harniakou <anton.harniakou@gmail.com>2019-05-30 12:32:58 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-05-30 12:32:58 +0300
commitfb007e9ae56f295abe9835485f98dcf3cc362420 (patch)
treeb93b528068dabc328c1cab90eaf9def7de582b4b /tpl/collections
parent4c560020bc0c50f8004873be8adf83698b7c095a (diff)
tpl/collections: Convert numeric values to float64 and compare them
Fixes #5685
Diffstat (limited to 'tpl/collections')
-rw-r--r--tpl/collections/where.go13
-rw-r--r--tpl/collections/where_test.go21
2 files changed, 34 insertions, 0 deletions
diff --git a/tpl/collections/where.go b/tpl/collections/where.go
index c96c38910..17d6552e6 100644
--- a/tpl/collections/where.go
+++ b/tpl/collections/where.go
@@ -114,6 +114,17 @@ func (ns *Namespace) checkCondition(v, mv reflect.Value, op string) (bool, error
slv = v.Interface()
slmv = mv.Interface()
}
+ } else if isNumber(v.Kind()) && isNumber(mv.Kind()) {
+ fv, err := toFloat(v)
+ if err != nil {
+ return false, err
+ }
+ fvp = &fv
+ fmv, err := toFloat(mv)
+ if err != nil {
+ return false, err
+ }
+ fmvp = &fmv
} else {
if mv.Kind() != reflect.Array && mv.Kind() != reflect.Slice {
return false, nil
@@ -426,6 +437,8 @@ func toFloat(v reflect.Value) (float64, error) {
switch v.Kind() {
case reflect.Float32, reflect.Float64:
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.Interface:
return toFloat(v.Elem())
}
diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go
index fb768cfde..295b89051 100644
--- a/tpl/collections/where_test.go
+++ b/tpl/collections/where_test.go
@@ -88,6 +88,27 @@ func TestWhere(t *testing.T) {
seq: []map[string]float64{
{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
},
+ key: "b", match: 4, op: "<",
+ expect: []map[string]float64{{"a": 1, "b": 2}},
+ },
+ {
+ seq: []map[string]int{
+ {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
+ },
+ key: "b", match: 4.0, op: "<",
+ expect: []map[string]int{{"a": 1, "b": 2}},
+ },
+ {
+ seq: []map[string]int{
+ {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
+ },
+ key: "b", match: 4.2, op: "<",
+ expect: []map[string]int{{"a": 1, "b": 2}, {"a": 3, "b": 4}},
+ },
+ {
+ seq: []map[string]float64{
+ {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
+ },
key: "b", match: 4.0, op: "<=",
expect: []map[string]float64{{"a": 1, "b": 2}, {"a": 3, "b": 4}},
},