-
Notifications
You must be signed in to change notification settings - Fork 1
/
condition.go
319 lines (270 loc) · 7.84 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package liquid
import (
"fmt"
"reflect"
"strings"
)
// ErrInvalidOperator wraps an invalid variable operator error
type ErrInvalidOperator string
func (e ErrInvalidOperator) Error() string {
return fmt.Sprintf("Liquid::InvalidOperator: %v", string(e))
}
// ErrBadArgument wraps an argument list error
type ErrBadArgument struct {
args []Expression
}
func (e ErrBadArgument) Error() string {
return fmt.Sprintf("Liquid::ArgumentError: %v", e.args)
}
// Condition is a callable func that wraps an operator
type Condition struct {
a Expression
operator operator
b Expression
or []*Condition
and []*Condition
}
// Evaluate the supplied condition
func (c *Condition) Evaluate(ctx Context) (bool, error) {
result, err := c.operator(c.a.Evaluate(ctx), c.b.Evaluate(ctx))
if err != nil {
return false, err
}
for _, or := range c.or {
if result {
break
}
sub, err := or.Evaluate(ctx)
if err != nil {
return false, err
}
result = result || sub
}
for _, and := range c.and {
sub, err := and.Evaluate(ctx)
if err != nil {
return false, err
}
result = result && sub
}
return result, nil
}
func (c *Condition) Or(op1 Expression, operator string, op2 Expression) error {
orCondition, err := NewCondition(op1, operator, op2)
if err != nil {
return err
}
c.or = append(c.or, orCondition)
return nil
}
func (c *Condition) And(op1 Expression, operator string, op2 Expression) error {
andCondition, err := NewCondition(op1, operator, op2)
if err != nil {
return err
}
c.and = append(c.and, andCondition)
return nil
}
type operator func(a, b Expression) (bool, error)
func equal(a, b Expression) (bool, error) {
return reflect.DeepEqual(a, b), nil
}
func notEqual(a, b Expression) (bool, error) {
r, err := equal(a, b)
return !r, err
}
func lt(a, b Expression) (bool, error) {
if t := reflect.TypeOf(a); t == reflect.TypeOf(b) {
switch a.(type) {
case integerExpr:
return a.(integerExpr) < b.(integerExpr), nil
case floatExpr:
return a.(floatExpr) < b.(floatExpr), nil
case stringExpr:
return a.(stringExpr) < b.(stringExpr), nil
}
}
return false, ErrBadArgument{}
}
func gt(a, b Expression) (bool, error) {
if t := reflect.TypeOf(a); t == reflect.TypeOf(b) {
switch a.(type) {
case integerExpr:
return a.(integerExpr) > b.(integerExpr), nil
case floatExpr:
return a.(floatExpr) > b.(floatExpr), nil
case stringExpr:
return a.(stringExpr) > b.(stringExpr), nil
}
}
return false, ErrBadArgument{}
}
func contains(a, b Expression) (bool, error) {
switch a.(type) {
case arrayExpr:
for _, value := range a.(arrayExpr) {
if reflect.DeepEqual(interfaceToExpression(value), b) {
return true, nil
}
}
return false, nil
case stringExpr:
return strings.Contains(string(a.(stringExpr)), fmt.Sprintf("%v", b)), nil
}
return false, nil
}
var operators = map[string]operator{
"==": equal,
"!=": notEqual,
"<>": notEqual,
"<": lt,
">": gt,
">=": func(a, b Expression) (bool, error) {
if g, err := gt(a, b); err != nil || g {
return g, err
}
return equal(a, b)
},
"<=": func(a, b Expression) (bool, error) {
if l, err := lt(a, b); err != nil || l {
return l, err
}
return equal(a, b)
},
"contains": contains,
}
func NewCondition(op1 Expression, operator string, op2 Expression) (*Condition, error) {
if found, ok := operators[operator]; ok {
return &Condition{a: op1, operator: found, b: op2}, nil
}
// # If the operator is empty this means that the decision statement is just
// # a single variable. We can just poll this variable from the context and
// # return this as the result.
// return context.evaluate(left) if op.nil?
// left = context.evaluate(left)
// right = context.evaluate(right)
// operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}"))
// if operation.respond_to?(:call)
// operation.call(self, left, right)
// elsif left.respond_to?(operation) && right.respond_to?(operation)
// begin
// left.send(operation, right)
// rescue ::ArgumentError => e
// raise Liquid::ArgumentError.new(e.message)
// end
// end
// raise(Liquid::ArgumentError.new("Unknown operator #{op}"))
return nil, ErrInvalidOperator(operator)
}
// module Liquid
// # Container for liquid nodes which conveniently wraps decision making logic
// #
// # Example:
// #
// # c = Condition.new(1, '==', 1)
// # c.evaluate #=> true
// #
// class Condition #:nodoc:
// @@operators = {
// '=='.freeze => ->(cond, left, right) { cond.send(:equal_variables, left, right) },
// '!='.freeze => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
// '<>'.freeze => ->(cond, left, right) { !cond.send(:equal_variables, left, right) },
// '<'.freeze => :<,
// '>'.freeze => :>,
// '>='.freeze => :>=,
// '<='.freeze => :<=,
// 'contains'.freeze => lambda do |cond, left, right|
// if left && right && left.respond_to?(:include?)
// right = right.to_s if left.is_a?(String)
// left.include?(right)
// else
// false
// end
// end
// }
// def self.operators
// @@operators
// end
// attr_reader :attachment
// attr_accessor :left, :operator, :right
// def initialize(left = nil, operator = nil, right = nil)
// @left = left
// @operator = operator
// @right = right
// @child_relation = nil
// @child_condition = nil
// end
// def evaluate(context = Context.new)
// result = interpret_condition(left, right, operator, context)
// case @child_relation
// when :or
// result || @child_condition.evaluate(context)
// when :and
// result && @child_condition.evaluate(context)
// else
// result
// end
// end
// def or(condition)
// @child_relation = :or
// @child_condition = condition
// end
// def and(condition)
// @child_relation = :and
// @child_condition = condition
// end
// def attach(attachment)
// @attachment = attachment
// end
// def else?
// false
// end
// def inspect
// "#<Condition #{[@left, @operator, @right].compact.join(' '.freeze)}>"
// end
// private
// def equal_variables(left, right)
// if left.is_a?(Liquid::Expression::MethodLiteral)
// if right.respond_to?(left.method_name)
// return right.send(left.method_name)
// else
// return nil
// end
// end
// if right.is_a?(Liquid::Expression::MethodLiteral)
// if left.respond_to?(right.method_name)
// return left.send(right.method_name)
// else
// return nil
// end
// end
// left == right
// end
// def interpret_condition(left, right, op, context)
// # If the operator is empty this means that the decision statement is just
// # a single variable. We can just poll this variable from the context and
// # return this as the result.
// return context.evaluate(left) if op.nil?
// left = context.evaluate(left)
// right = context.evaluate(right)
// operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}"))
// if operation.respond_to?(:call)
// operation.call(self, left, right)
// elsif left.respond_to?(operation) && right.respond_to?(operation)
// begin
// left.send(operation, right)
// rescue ::ArgumentError => e
// raise Liquid::ArgumentError.new(e.message)
// end
// end
// end
// end
// class ElseCondition < Condition
// def else?
// true
// end
// def evaluate(_context)
// true
// end
// end
// end