/
condition.go
286 lines (253 loc) · 7.95 KB
/
condition.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package core
import (
"reflect"
"time"
)
var (
TrueCondition = &BooleanCondition{&StaticValue{true}}
FalseCondition = &BooleanCondition{&StaticValue{false}}
)
type LogicalOperator int
type ComparisonOperator int
type Type int
const (
OR LogicalOperator = iota
AND
UnknownLogicalOperator
Equals ComparisonOperator = iota
NotEquals
LessThan
GreaterThan
LessThanOrEqual
GreaterThanOrEqual
UnknownComparisonOperator
String Type = iota
Nil
Int
Int64
Uint
Float64
Complex128
Bool
Time
Today
Array
Unknown
)
var KindToType = map[reflect.Kind]Type{
reflect.String: String,
reflect.Int: Int,
reflect.Int64: Int64,
reflect.Uint: Uint,
reflect.Float64: Float64,
reflect.Complex128: Complex128,
reflect.Bool: Bool,
reflect.Array: Array,
reflect.Slice: Array,
reflect.Map: Array,
}
var TypeOperations = map[Type]map[ComparisonOperator]ConditionResolver{
String: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(string) == right.(string) },
LessThan: func(left, right interface{}) bool { return left.(string) < right.(string) },
},
Nil: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left == nil && right == nil },
LessThan: func(left, right interface{}) bool { return false },
},
Int: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(int) == right.(int) },
LessThan: func(left, right interface{}) bool { return left.(int) < right.(int) },
},
Int64: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(int64) == right.(int64) },
LessThan: func(left, right interface{}) bool { return left.(int64) < right.(int64) },
},
Uint: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(uint) == right.(uint) },
LessThan: func(left, right interface{}) bool { return left.(uint) < right.(uint) },
},
Float64: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(float64) == right.(float64) },
LessThan: func(left, right interface{}) bool { return left.(float64) < right.(float64) },
},
Complex128: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(complex128) == right.(complex128) },
LessThan: func(left, right interface{}) bool { return false },
},
Bool: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(bool) == right.(bool) },
LessThan: func(left, right interface{}) bool { return false },
},
Time: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return left.(time.Time).Unix() == right.(time.Time).Unix() },
LessThan: func(left, right interface{}) bool { return left.(time.Time).Unix() < right.(time.Time).Unix() },
},
Today: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool {
l, r := left.(time.Time), right.(time.Time)
return l.YearDay() == r.YearDay() && l.Year() == r.Year()
},
LessThan: func(left, right interface{}) bool {
l, r := left.(time.Time), right.(time.Time)
if l.Year() > r.Year() {
return false
}
if l.Year() < r.Year() {
return true
}
return l.YearDay() < r.YearDay()
},
},
Array: map[ComparisonOperator]ConditionResolver{
Equals: func(left, right interface{}) bool { return reflect.DeepEqual(left, right) },
LessThan: func(left, right interface{}) bool { return reflect.ValueOf(left).Len() < reflect.ValueOf(right).Len() },
},
}
// Resolves a condition
type ConditionResolver func(left, right interface{}) bool
var ConditionLookup = map[ComparisonOperator]ConditionResolver{
Equals: EqualsComparison,
NotEquals: NotEqualsComparison,
LessThan: LessThanComparison,
GreaterThan: GreaterThanComparison,
LessThanOrEqual: LessThanOrEqualComparison,
GreaterThanOrEqual: GreaterThanOrEqualComparison,
}
type Verifiable interface {
IsTrue(context *Context) bool
}
// represents a group of conditions
type ConditionGroup struct {
verifiables []Verifiable
joins []LogicalOperator
}
func (g *ConditionGroup) IsTrue(context *Context) bool {
l := len(g.verifiables) - 1
if l == 0 {
return g.verifiables[0].IsTrue(context)
}
for i := 0; i <= l; i++ {
if g.verifiables[i].IsTrue(context) {
if i == l || g.joins[i] == OR {
return true
}
} else if i != l && g.joins[i] == AND {
for ; i < l; i++ {
if g.joins[i] == OR {
break
}
}
}
}
return false
}
type BooleanCondition struct {
value Value
}
func (c *BooleanCondition) IsTrue(context *Context) bool {
value := c.value.Resolve(context)
if b, ok := value.(bool); ok {
return b
}
return false
}
// represents a conditions (such as x == y)
type Condition struct {
left Value
operator ComparisonOperator
right Value
}
func (c *Condition) IsTrue(context *Context) bool {
left := c.left.Resolve(context)
right := c.right.Resolve(context)
return ConditionLookup[c.operator](left, right)
}
func EqualsComparison(left, right interface{}) bool {
var t Type
if left, right, t = convertToSameType(left, right); t == Unknown {
return false
}
return TypeOperations[t][Equals](left, right)
}
func NotEqualsComparison(left, right interface{}) bool {
return !EqualsComparison(left, right)
}
func LessThanComparison(left, right interface{}) bool {
var t Type
if left, right, t = convertToSameType(left, right); t == Unknown {
return false
}
return TypeOperations[t][LessThan](left, right)
}
func LessThanOrEqualComparison(left, right interface{}) bool {
var t Type
if left, right, t = convertToSameType(left, right); t == Unknown {
return false
}
return TypeOperations[t][Equals](left, right) || TypeOperations[t][LessThan](left, right)
}
func GreaterThanComparison(left, right interface{}) bool {
var t Type
if left, right, t = convertToSameType(left, right); t == Unknown {
return false
}
return !TypeOperations[t][Equals](left, right) && !TypeOperations[t][LessThan](left, right)
}
func GreaterThanOrEqualComparison(left, right interface{}) bool {
var t Type
if left, right, t = convertToSameType(left, right); t == Unknown {
return false
}
return !TypeOperations[t][LessThan](left, right)
}
func convertToSameType(left, right interface{}) (interface{}, interface{}, Type) {
//rely on the above code to handle this properly
if left == nil || right == nil {
return left, right, Nil
}
if l, ok := left.(string); ok {
if r, ok := right.(string); ok {
return l, r, String
}
return left, right, Unknown
}
leftValue, rightValue := reflect.ValueOf(left), reflect.ValueOf(right)
leftKind, rightKind := leftValue.Kind(), rightValue.Kind()
if leftKind == rightKind {
if t, ok := KindToType[leftKind]; ok {
return left, right, t
}
}
if left, right, t := convertNumbersToSameType(leftValue, leftKind, rightValue, rightKind); t != Unknown {
return left, right, t
}
return left, right, Unknown
}
func convertNumbersToSameType(leftValue reflect.Value, leftKind reflect.Kind, rightValue reflect.Value, rightKind reflect.Kind) (interface{}, interface{}, Type) {
if isInt(leftKind) {
if isInt(rightKind) {
return leftValue.Int(), rightValue.Int(), Int64
}
return nil, nil, Unknown
}
if isFloat(leftKind) {
if isFloat(rightKind) {
return leftValue.Float(), rightValue.Float(), Float64
}
return nil, nil, Unknown
}
if isComplex(leftKind) && isComplex(rightKind) {
return leftValue.Complex(), rightValue.Complex(), Complex128
}
return nil, nil, Unknown
}
func isInt(kind reflect.Kind) bool {
return kind == reflect.Int || kind == reflect.Int8 || kind == reflect.Int16 || kind == reflect.Int32 || kind == reflect.Int64
}
func isFloat(kind reflect.Kind) bool {
return kind == reflect.Float64 || kind == reflect.Float32
}
func isComplex(kind reflect.Kind) bool {
return kind == reflect.Complex128 || kind == reflect.Complex64
}