2017-07-06 01:09:59 +02:00
|
|
|
// Package evaluator defines methods such as sorting, comparison, and type conversion, that apply to interface types.
|
2017-06-29 19:08:25 +02:00
|
|
|
//
|
|
|
|
// 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-07-05 17:35:07 +02:00
|
|
|
package evaluator
|
2017-06-27 16:28:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2017-06-28 20:41:46 +02: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 18:00:43 +02:00
|
|
|
value = ToLiquid(value)
|
2017-06-28 20:41:46 +02:00
|
|
|
ref := reflect.ValueOf(value)
|
|
|
|
switch ref.Kind() {
|
2017-06-28 23:18:48 +02:00
|
|
|
case reflect.Array, reflect.Slice, reflect.String:
|
|
|
|
return ref.Len()
|
|
|
|
default:
|
2017-06-28 20:41:46 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|