1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-22 23:01:16 +01:00
liquid/generics/generics.go

74 lines
2.0 KiB
Go
Raw Normal View History

2017-06-29 13:08:25 -04:00
// Package generics defines methods such as sorting, comparison, and type conversion, that apply to interface types.
//
// It is similar to, and makes heavy use of, the reflect package.
//
// Since the intent is to provide runtime services for the Liquid expression interpreter,
// this package does not implement "generic" generics.
// It attempts to implement Liquid semantics (which are largely Ruby semantics).
2017-06-27 10:28:39 -04: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-28 17:18:48 -04:00
// Contains returns a boolean indicating whether array is a sequence that contains item.
func Contains(array interface{}, item interface{}) bool {
2017-07-03 12:00:43 -04:00
item = ToLiquid(item)
2017-06-28 17:18:48 -04:00
ref := reflect.ValueOf(array)
switch ref.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < ref.Len(); i++ {
if ref.Index(i).Interface() == item {
return true
}
}
}
return false
}
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
2017-06-27 17:54:24 -04:00
func IsEmpty(value interface{}) bool {
2017-07-03 12:00:43 -04:00
value = ToLiquid(value)
2017-06-27 17:54:24 -04:00
if value == nil {
return false
2017-06-27 10:28:39 -04:00
}
2017-06-27 17:54:24 -04:00
r := reflect.ValueOf(value)
switch r.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return r.Len() == 0
case reflect.Bool:
2017-06-29 12:26:04 -04:00
return !r.Bool()
default:
return false
2017-06-27 10:28:39 -04:00
}
}
2017-06-27 16:53:34 -04:00
2017-06-27 17:54:24 -04:00
// IsTrue returns a bool indicating whether the value is true according to Liquid semantics.
func IsTrue(value interface{}) bool {
2017-07-03 12:00:43 -04:00
value = ToLiquid(value)
2017-06-27 17:54:24 -04:00
return value != nil && value != false
}
2017-06-28 14:41:46 -04:00
// Length returns the length of a string or array. In keeping with Liquid semantics,
// and contra Go, it does not return the size of a map.
func Length(value interface{}) int {
2017-07-03 12:00:43 -04:00
value = ToLiquid(value)
2017-06-28 14:41:46 -04:00
ref := reflect.ValueOf(value)
switch ref.Kind() {
2017-06-28 17:18:48 -04:00
case reflect.Array, reflect.Slice, reflect.String:
return ref.Len()
default:
2017-06-28 14:41:46 -04:00
return 0
}
}