mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Make template comparison functions handle floats
This commit is contained in:
parent
667a047cea
commit
73cbefdbc8
2 changed files with 18 additions and 12 deletions
|
@ -25,47 +25,51 @@ func Ne(x, y interface{}) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ge(a, b interface{}) bool {
|
func Ge(a, b interface{}) bool {
|
||||||
left, right := compareGetInt(a, b)
|
left, right := compareGetFloat(a, b)
|
||||||
return left >= right
|
return left >= right
|
||||||
}
|
}
|
||||||
|
|
||||||
func Gt(a, b interface{}) bool {
|
func Gt(a, b interface{}) bool {
|
||||||
left, right := compareGetInt(a, b)
|
left, right := compareGetFloat(a, b)
|
||||||
return left > right
|
return left > right
|
||||||
}
|
}
|
||||||
|
|
||||||
func Le(a, b interface{}) bool {
|
func Le(a, b interface{}) bool {
|
||||||
left, right := compareGetInt(a, b)
|
left, right := compareGetFloat(a, b)
|
||||||
return left <= right
|
return left <= right
|
||||||
}
|
}
|
||||||
|
|
||||||
func Lt(a, b interface{}) bool {
|
func Lt(a, b interface{}) bool {
|
||||||
left, right := compareGetInt(a, b)
|
left, right := compareGetFloat(a, b)
|
||||||
return left < right
|
return left < right
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareGetInt(a interface{}, b interface{}) (int64, int64) {
|
func compareGetFloat(a interface{}, b interface{}) (float64, float64) {
|
||||||
var left, right int64
|
var left, right float64
|
||||||
av := reflect.ValueOf(a)
|
av := reflect.ValueOf(a)
|
||||||
|
|
||||||
switch av.Kind() {
|
switch av.Kind() {
|
||||||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||||
left = int64(av.Len())
|
left = float64(av.Len())
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
left = av.Int()
|
left = float64(av.Int())
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
left = av.Float()
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
left, _ = strconv.ParseInt(av.String(), 10, 64)
|
left, _ = strconv.ParseFloat(av.String(), 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
bv := reflect.ValueOf(b)
|
bv := reflect.ValueOf(b)
|
||||||
|
|
||||||
switch bv.Kind() {
|
switch bv.Kind() {
|
||||||
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
|
||||||
right = int64(bv.Len())
|
right = float64(bv.Len())
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
right = bv.Int()
|
right = float64(bv.Int())
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
right = bv.Float()
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
right, _ = strconv.ParseInt(bv.String(), 10, 64)
|
right, _ = strconv.ParseFloat(bv.String(), 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
return left, right
|
return left, right
|
||||||
|
|
|
@ -16,6 +16,8 @@ func TestGt(t *testing.T) {
|
||||||
{5, 5, false},
|
{5, 5, false},
|
||||||
{-2, 1, false},
|
{-2, 1, false},
|
||||||
{2, -5, true},
|
{2, -5, true},
|
||||||
|
{0.0, 1.23, false},
|
||||||
|
{1.23, 0.0, true},
|
||||||
{"8", "5", true},
|
{"8", "5", true},
|
||||||
{"5", "0001", true},
|
{"5", "0001", true},
|
||||||
{[]int{100, 99}, []int{1, 2, 3, 4}, false},
|
{[]int{100, 99}, []int{1, 2, 3, 4}, false},
|
||||||
|
|
Loading…
Reference in a new issue