-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.go
140 lines (121 loc) · 3.42 KB
/
logic.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
package jsonlogic
import (
"fmt"
)
// AddOpIf adds "if"/"?:" operation to the JSONLogic instance.
func AddOpIf(jl *JSONLogic) {
jl.AddOperation("if", opIf)
jl.AddOperation("?:", opIf)
}
func opIf(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
var i int
for i = 0; i < len(params)-1; i += 2 {
r, err := apply(params[i], data)
if err != nil {
return nil, err
}
if ToBool(r) {
return apply(params[i+1], data)
}
}
if len(params) == i+1 {
return apply(params[i], data)
}
return nil, nil
}
// AddOpStrictEqual adds "===" operation to the JSONLogic instance. Param restriction:
// - At least two params.
// - Params must be evaluated to json primitives.
func AddOpStrictEqual(jl *JSONLogic) {
jl.AddOperation("===", opStrictEqual)
}
func opStrictEqual(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("===: expect at least two params")
}
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
return CompareValues(EQ, params[0], params[1])
}
// AddOpStrictNotEqual adds "!==" operation to the JSONLogic instance. Param restriction: the same as "===".
func AddOpStrictNotEqual(jl *JSONLogic) {
jl.AddOperation("!==", opStrictNotEqual)
}
func opStrictNotEqual(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("!==: expect at least two params")
}
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
return CompareValues(NE, params[0], params[1])
}
// AddOpNegative adds "!" operation to the JSONLogic instance. Param restriction:
// - At least one param.
func AddOpNegative(jl *JSONLogic) {
jl.AddOperation("!", opNegative)
}
func opNegative(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 1 {
return nil, fmt.Errorf("!/!!: expect at least one param")
}
res, err = apply(params[0], data)
if err != nil {
return
}
return !ToBool(res), nil
}
// AddOpDoubleNegative adds "!!" operation to the JSONLogic instance. Param Restriction: the same as "!".
func AddOpDoubleNegative(jl *JSONLogic) {
jl.AddOperation("!!", opDoubleNegative)
}
func opDoubleNegative(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
r, err := opNegative(apply, params, data)
if err != nil {
return
}
return !r.(bool), nil
}
// AddOpAnd adds "and" operation to the JSONLogic instance. Param restriction:
// - At least one param.
func AddOpAnd(jl *JSONLogic) {
jl.AddOperation("and", opAnd)
}
func opAnd(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 1 {
return nil, fmt.Errorf("and: expect at least one params")
}
for _, param := range params {
res, err = apply(param, data)
if err != nil {
return
}
if !ToBool(res) {
return res, nil
}
}
return
}
// AddOpOr adds "or" operation to the JSONLogic instance. Param restriction:
// - At least one param.
func AddOpOr(jl *JSONLogic) {
jl.AddOperation("or", opOr)
}
func opOr(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 1 {
return nil, fmt.Errorf("or: expect at least one params")
}
for _, param := range params {
res, err = apply(param, data)
if err != nil {
return
}
if ToBool(res) {
return res, nil
}
}
return
}