-
Notifications
You must be signed in to change notification settings - Fork 54
/
predicates.go
66 lines (61 loc) · 1.48 KB
/
predicates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package evaluator
import (
"reflect"
"strings"
)
// Contains returns a boolean indicating whether array is a sequence that contains item.
func Contains(array interface{}, item interface{}) bool {
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
}
// ContainsString returns a bool indicating whether a string or array contains an object.
func ContainsString(container interface{}, item string) bool {
switch container := container.(type) {
case string:
return strings.Contains(container, item)
case []string:
for _, s := range container {
if s == item {
return true
}
}
case []interface{}:
for _, k := range container {
if s, ok := k.(string); ok && s == item {
return true
}
}
default:
return false
}
return false
}
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
func IsEmpty(value interface{}) bool {
value = ToLiquid(value)
if value == nil {
return false
}
r := reflect.ValueOf(value)
switch r.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return r.Len() == 0
case reflect.Bool:
return !r.Bool()
default:
return false
}
}
// IsTrue returns a bool indicating whether the value is true according to Liquid semantics.
func IsTrue(value interface{}) bool {
value = ToLiquid(value)
return value != nil && value != false
}