forked from launchdarkly/go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.go
81 lines (70 loc) · 1.78 KB
/
operators.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ldclient
import (
"regexp"
"strings"
)
const (
operatorIn Operator = "in"
operatorEndsWith Operator = "endsWith"
operatorStartsWith Operator = "startsWith"
operatorMatches Operator = "matches"
operatorContains Operator = "contains"
)
type opFn (func(interface{}, interface{}) bool)
var allOps = map[Operator]opFn{
operatorIn: operatorInFn,
operatorEndsWith: operatorEndsWithFn,
operatorStartsWith: operatorStartsWithFn,
operatorMatches: operatorMatchesFn,
operatorContains: operatorContainsFn,
}
// Turn this into a static map
func operatorFn(operator Operator) opFn {
if op, ok := allOps[operator]; ok {
return op
} else {
return operatorNoneFn
}
}
func operatorInFn(uValue interface{}, cValue interface{}) bool {
return uValue == cValue
}
func operatorStartsWithFn(uValue interface{}, cValue interface{}) bool {
if uStr, ok := uValue.(string); ok {
if cStr, ok := cValue.(string); ok {
return strings.HasPrefix(uStr, cStr)
}
}
return false
}
func operatorEndsWithFn(uValue interface{}, cValue interface{}) bool {
if uStr, ok := uValue.(string); ok {
if cStr, ok := cValue.(string); ok {
return strings.HasSuffix(uStr, cStr)
}
}
return false
}
func operatorMatchesFn(uValue interface{}, cValue interface{}) bool {
if uStr, ok := uValue.(string); ok {
if pattern, ok := cValue.(string); ok {
if matched, err := regexp.MatchString(pattern, uStr); err == nil {
return matched
} else {
return false
}
}
}
return false
}
func operatorContainsFn(uValue interface{}, cValue interface{}) bool {
if uStr, ok := uValue.(string); ok {
if cStr, ok := cValue.(string); ok {
return strings.Contains(uStr, cStr)
}
}
return false
}
func operatorNoneFn(uValue interface{}, cValue interface{}) bool {
return false
}