-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathjsonlogic.go
192 lines (156 loc) · 4.46 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
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
package jsonlogic
import (
"encoding/json"
"io"
"strings"
"github.com/diegoholiveira/jsonlogic/v3/internal/typing"
)
// Apply reads a rule and data from `io.Reader`, applies the rule to the data
// and writes the result to the provided writer. It returns an error if rule
// processing or data handling fails.
//
// Parameters:
// - rule: io.Reader representing the transformation rule to be applied
// - data: io.Reader containing the input data to transform
// - result: io.Writer containing the transformed data
//
// Returns:
// - err: error if the transformation fails or if type assertions are invalid
func Apply(rule, data io.Reader, result io.Writer) error {
if data == nil {
data = strings.NewReader("{}")
}
var _rule any
var _data any
decoder := json.NewDecoder(rule)
err := decoder.Decode(&_rule)
if err != nil {
return err
}
decoder = json.NewDecoder(data)
err = decoder.Decode(&_data)
if err != nil {
return err
}
output, err := ApplyInterface(_rule, _data)
if err != nil {
return err
}
return json.NewEncoder(result).Encode(output)
}
// ApplyRaw applies a validation rule to a JSON data input, both provided as raw JSON messages.
// It processes the input data according to the provided rule and returns the transformed result.
//
// Parameters:
// - rule: json.RawMessage representing the transformation rule to be applied
// - data: json.RawMessage containing the input data to transform
//
// Returns:
// - output: json.RawMessage containing the transformed data
// - err: error if the transformation fails or if type assertions are invalid
func ApplyRaw(rule, data json.RawMessage) (json.RawMessage, error) {
if data == nil {
data = json.RawMessage("{}")
}
var _rule any
var _data any
err := json.Unmarshal(rule, &_rule)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &_data)
if err != nil {
return nil, err
}
result, err := ApplyInterface(_rule, _data)
if err != nil {
return nil, err
}
return json.Marshal(&result)
}
// ApplyInterface applies a transformation rule to input data using interface type assertions.
// It processes the input data according to the provided rule and returns the transformed result.
//
// Parameters:
// - rule: interface{} representing the transformation rule to be applied
// - data: interface{} containing the input data to transform
//
// Returns:
// - output: interface{} containing the transformed data
// - err: error if the transformation fails or if type assertions are invalid
func ApplyInterface(rule, data any) (output any, err error) {
defer func() {
if e := recover(); e != nil {
// fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
err = e.(error)
}
}()
if typing.IsMap(rule) {
return apply(rule, data), err
}
if typing.IsSlice(rule) {
var parsed []any
for _, value := range rule.([]any) {
parsed = append(parsed, parseValues(value, data))
}
return any(parsed), nil
}
return rule, err
}
func GetJsonLogicWithSolvedVars(rule, data json.RawMessage) ([]byte, error) {
if data == nil {
data = json.RawMessage("{}")
}
// parse rule and data from json.RawMessage to interface
var _rule any
var _data any
err := json.Unmarshal(rule, &_rule)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &_data)
if err != nil {
return nil, err
}
return solveVarsBackToJsonLogic(_rule, _data)
}
func parseValues(values, data any) any {
if values == nil || typing.IsPrimitive(values) {
return values
}
if typing.IsMap(values) {
return apply(values, data)
}
inputSlice := values.([]any)
length := len(inputSlice)
if length == 0 {
return inputSlice
}
parsed := make([]any, 0, length)
for _, value := range inputSlice {
if typing.IsMap(value) {
parsed = append(parsed, apply(value, data))
} else {
parsed = append(parsed, value)
}
}
return parsed
}
// If values represents a map (an operation), returns the result. Otherwise returns the
// values without parsing. This means that each of the returned values might be a subtree
// of JSONLogic.
// Used in lazy evaluation of "AND", "OR", and "IF" operators
func getValuesWithoutParsing(values, data any) any {
return values.([]any)
}
func apply(rules, data any) any {
ruleMap := rules.(map[string]any)
// A map with more than 1 key counts as a primitive so it's time to end recursion
if len(ruleMap) > 1 {
return ruleMap
}
for operator, values := range ruleMap {
return operation(operator, values, data)
}
return make(map[string]any)
}