-
Notifications
You must be signed in to change notification settings - Fork 4
/
jsonlogic.go
67 lines (57 loc) · 1.73 KB
/
jsonlogic.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
package jsonlogic
import (
"log"
)
type jsonLogic struct {
ops map[string]Operator
}
func (jl *jsonLogic) Apply(rule interface{}, data interface{}) (interface{}, error) {
return jl.apply(rule, data)
}
func (jl *jsonLogic) AddOperation(symbol string, op Operator) error {
if op == nil {
delete(jl.ops, symbol)
} else {
jl.ops[symbol] = op
}
return nil
}
func (jl *jsonLogic) Validate(rule interface{}) error {
return jl.validate(rule)
}
func (jl *jsonLogic) mustAddOperation(symbol string, op Operator) {
if err := jl.AddOperation(symbol, op); err != nil {
log.Fatal(err)
}
return
}
// NewEmptyLogic makes an empty LogicApplier without operators
func NewEmptyLogic() LogicApplier {
return &jsonLogic{ops: make(map[string]Operator)}
}
// NewJSONLogic makes a new LogicApplier with default jsonlogic operators
func NewJSONLogic() LogicApplier {
jl := &jsonLogic{ops: make(map[string]Operator)}
jl.mustAddOperation("+", opAdd{})
jl.mustAddOperation("-", opSub{})
jl.mustAddOperation("*", opMul{})
jl.mustAddOperation("/", opDiv{})
jl.mustAddOperation("%", opMod{})
jl.mustAddOperation(">", opGreater{})
jl.mustAddOperation(">=", opGreaterEqual{})
jl.mustAddOperation("<", opLess{})
jl.mustAddOperation("<=", opLessEqual{})
jl.mustAddOperation("min", opMin{})
jl.mustAddOperation("max", opMax{})
jl.mustAddOperation("var", opVar{})
jl.mustAddOperation("if", opIf{})
jl.mustAddOperation("===", opStrictEqual{})
jl.mustAddOperation("!==", opStrictNEqual{})
jl.mustAddOperation("!", opNot{})
jl.mustAddOperation("!!", opNotNot{})
jl.mustAddOperation("or", opOr{})
jl.mustAddOperation("and", opAnd{})
jl.mustAddOperation("_default_aggregator", opAndBool{})
jl.mustAddOperation("_quick_access", opVarStrictEqual{})
return jl
}