Stop being a dummy reinventing the wheel and use template.IsTrue
This commit is contained in:
parent
3db02d3b23
commit
c98d0c2b3d
|
|
@ -74,25 +74,9 @@ var FuncMap = template.FuncMap{
|
||||||
"hasSuffix": swapStringsFuncBoolArgsOrder(strings.HasSuffix),
|
"hasSuffix": swapStringsFuncBoolArgsOrder(strings.HasSuffix),
|
||||||
|
|
||||||
"ternary": func(truthy interface{}, falsey interface{}, val interface{}) interface{} {
|
"ternary": func(truthy interface{}, falsey interface{}, val interface{}) interface{} {
|
||||||
v := reflect.ValueOf(val)
|
if t, ok := template.IsTrue(val); !ok {
|
||||||
|
panic(fmt.Sprintf(`template.IsTrue(%+v) says things are NOT OK`, val))
|
||||||
var t bool
|
} else if t {
|
||||||
switch v.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
t = v.Bool()
|
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
||||||
t = v.Int() != 0
|
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
||||||
t = v.Uint() != 0
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
t = v.Float() != 0
|
|
||||||
case reflect.String:
|
|
||||||
t = v.String() != ""
|
|
||||||
default:
|
|
||||||
t = !v.IsNil()
|
|
||||||
}
|
|
||||||
|
|
||||||
if t {
|
|
||||||
return truthy
|
return truthy
|
||||||
} else {
|
} else {
|
||||||
return falsey
|
return falsey
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue