2017-06-27 16:28:39 +02:00
|
|
|
package generics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenericError is an error regarding generic conversion.
|
|
|
|
type GenericError string
|
|
|
|
|
|
|
|
func (e GenericError) Error() string { return string(e) }
|
|
|
|
|
|
|
|
func genericErrorf(format string, a ...interface{}) error {
|
|
|
|
return GenericError(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:02:05 +02:00
|
|
|
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
|
2017-06-27 23:54:24 +02:00
|
|
|
func IsEmpty(value interface{}) bool {
|
|
|
|
if value == nil {
|
2017-06-27 22:02:05 +02:00
|
|
|
return false
|
2017-06-27 16:28:39 +02:00
|
|
|
}
|
2017-06-27 23:54:24 +02:00
|
|
|
r := reflect.ValueOf(value)
|
2017-06-27 22:02:05 +02:00
|
|
|
switch r.Kind() {
|
|
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
|
|
|
return r.Len() == 0
|
|
|
|
case reflect.Bool:
|
|
|
|
return r.Bool() == false
|
|
|
|
default:
|
|
|
|
return false
|
2017-06-27 16:28:39 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 22:53:34 +02:00
|
|
|
|
2017-06-27 23:54:24 +02:00
|
|
|
// IsTrue returns a bool indicating whether the value is true according to Liquid semantics.
|
|
|
|
func IsTrue(value interface{}) bool {
|
|
|
|
return value != nil && value != false
|
|
|
|
}
|