forked from influxdata/influxdb
/
boolean_operators.go
136 lines (115 loc) · 3.37 KB
/
boolean_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package datastore
import (
"protocol"
"regexp"
)
type BooleanOperation func(leftValue, rightValue *protocol.FieldValue) (bool, error)
var (
registeredOperators = map[string]BooleanOperation{}
)
func init() {
registeredOperators["=="] = EqualityOperator
registeredOperators["!="] = not(EqualityOperator)
registeredOperators[">="] = GreaterThanOrEqualOperator
registeredOperators[">"] = GreaterThanOperator
registeredOperators["<"] = not(GreaterThanOrEqualOperator)
registeredOperators["<="] = not(GreaterThanOperator)
registeredOperators["~="] = RegexMatcherOperator
}
func not(op BooleanOperation) BooleanOperation {
return func(leftValue, rightValue *protocol.FieldValue) (bool, error) {
ok, err := op(leftValue, rightValue)
return !ok, err
}
}
type Type int
const (
TYPE_INT = iota
TYPE_STRING
TYPE_BOOL
TYPE_DOUBLE
TYPE_UNKNOWN
)
func coerceValues(leftValue, rightValue *protocol.FieldValue) (interface{}, interface{}, Type) {
if leftValue.Int64Value != nil {
if rightValue.Int64Value != nil {
return *leftValue.Int64Value, *rightValue.Int64Value, TYPE_INT
} else if rightValue.DoubleValue != nil {
return float64(*leftValue.Int64Value), *rightValue.DoubleValue, TYPE_DOUBLE
}
return nil, nil, TYPE_UNKNOWN
}
if leftValue.DoubleValue != nil {
if rightValue.Int64Value != nil {
return *leftValue.DoubleValue, float64(*rightValue.Int64Value), TYPE_DOUBLE
} else if rightValue.DoubleValue != nil {
return *leftValue.DoubleValue, *rightValue.DoubleValue, TYPE_DOUBLE
}
return nil, nil, TYPE_UNKNOWN
}
if leftValue.StringValue != nil {
if rightValue.StringValue == nil {
return nil, nil, TYPE_UNKNOWN
}
return *leftValue.StringValue, *rightValue.StringValue, TYPE_STRING
}
if leftValue.BoolValue != nil {
if rightValue.BoolValue == nil {
return nil, nil, TYPE_BOOL
}
return *leftValue.BoolValue, *rightValue.BoolValue, TYPE_BOOL
}
return nil, nil, TYPE_UNKNOWN
}
func EqualityOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := coerceValues(leftValue, rightValue)
switch cType {
case TYPE_STRING:
return v1.(string) == v2.(string), nil
case TYPE_INT:
return v1.(int64) == v2.(int64), nil
case TYPE_DOUBLE:
return v1.(float64) == v2.(float64), nil
case TYPE_BOOL:
return v1.(bool) == v2.(bool), nil
default:
return false, nil
}
}
func RegexMatcherOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := coerceValues(leftValue, rightValue)
switch cType {
case TYPE_STRING:
// TODO: assume that the regex is valid
matches, _ := regexp.MatchString(v2.(string), v1.(string))
return matches, nil
default:
return false, nil
}
}
func GreaterThanOrEqualOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := coerceValues(leftValue, rightValue)
switch cType {
case TYPE_STRING:
return v1.(string) >= v2.(string), nil
case TYPE_INT:
return v1.(int64) >= v2.(int64), nil
case TYPE_DOUBLE:
return v1.(float64) >= v2.(float64), nil
default:
return false, nil
}
}
func GreaterThanOperator(leftValue, rightValue *protocol.FieldValue) (bool, error) {
v1, v2, cType := coerceValues(leftValue, rightValue)
switch cType {
case TYPE_STRING:
return v1.(string) > v2.(string), nil
case TYPE_INT:
return v1.(int64) > v2.(int64), nil
case TYPE_DOUBLE:
return v1.(float64) > v2.(float64), nil
default:
return false, nil
}
}