-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution.go
More file actions
40 lines (33 loc) · 1.45 KB
/
Copy pathexecution.go
File metadata and controls
40 lines (33 loc) · 1.45 KB
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
package expr
import (
"cmp"
"fmt"
)
type Operator string
const (
RelationalOperatorEqualTo Operator = "EQUAL_TO"
RelationalOperatorGreaterThan Operator = "GREATER_THAN"
RelationalOperatorGreaterThanOrEqualTo Operator = "GREATER_THAN_OR_EQUAL_TO"
// INFO: Guideline for another expression operator
// ArithmeticOperatorPlus Operator = "PLUS"
// ArithmeticOperatorMinus Operator = "MINUS"
// ArithmeticOperatorTimes Operator = "TIMES"
// ArithmeticOperatorDivide Operator = "DIVIDE"
// ...
// RelationalOperatorLessThan Operator = "LESS_THAN"
// RelationalOperatorLessThanOrEqualTo Operator = "LESS_THAN_OR_EQUAL_TO"
// RelationalOperatorIncludes Operator = "INCLUDES"
// ...
)
func Execute[T cmp.Ordered](operand1 T, operator Operator, operand2 T) (any, error) {
executers := map[Operator]func() any{
RelationalOperatorEqualTo: func() any { return cmp.Compare(operand1, operand2) == 0 },
RelationalOperatorGreaterThan: func() any { return cmp.Compare(operand1, operand2) == +1 },
RelationalOperatorGreaterThanOrEqualTo: func() any { return !cmp.Less(operand1, operand2) },
}
executer, found := executers[operator]
if !found {
return nil, fmt.Errorf("Unsupported operator %v, Can't execute the given operator %v on expression (%v %v %v)", operator, operator, operand1, operator, operand2)
}
return executer(), nil
}