-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_str.go
373 lines (324 loc) · 8.49 KB
/
array_str.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package jsonlogic
import (
"fmt"
"strings"
)
// AddOpMap adds "map" operation to the JSONLogic instance. Param restriction:
// - At least two params: the first evaluated to an array and the second the logic.
func AddOpMap(jl *JSONLogic) {
jl.AddOperation("map", opMap)
}
func opMap(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("map: expect at least two params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
arr, ok := scopedData.([]interface{})
if !ok {
return []interface{}{}, nil
}
mappedArr := []interface{}{}
for _, item := range arr {
mappedItem, err := apply(scopedLogic, item)
if err != nil {
return nil, err
}
mappedArr = append(mappedArr, mappedItem)
}
return mappedArr, nil
}
// AddOpFilter adds "filter" operation to the JSONLogic instance. Param restriction:
// - At least two params: the first evaluated to an array and the second the logic.
func AddOpFilter(jl *JSONLogic) {
jl.AddOperation("filter", opFilter)
}
func opFilter(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("filter: expect at least two params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
arr, ok := scopedData.([]interface{})
if !ok {
return []interface{}{}, nil
}
filteredArr := []interface{}{}
for _, item := range arr {
r, err := apply(scopedLogic, item)
if err != nil {
return nil, err
}
if ToBool(r) {
filteredArr = append(filteredArr, item)
}
}
return filteredArr, nil
}
// AddOpReduce adds "reduce" operation to the JSONLogic instance. Param restriction:
// - At least three params: the first evaluated to an array, the second the logic and the third the initial value.
func AddOpReduce(jl *JSONLogic) {
jl.AddOperation("reduce", opReduce)
}
func opReduce(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 3 {
return nil, fmt.Errorf("filter: expect at least three params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
initial := params[2]
arr, ok := scopedData.([]interface{})
if !ok {
return initial, nil
}
for _, item := range arr {
r, err := apply(scopedLogic, map[string]interface{}{
"current": item,
"accumulator": initial,
})
if err != nil {
return nil, err
}
initial = r
}
return initial, nil
}
// AddOpAll adds "all" operation to the JSONLogic instance. Param restriction:
// - At least two params: the first evaluated to an array and the second the logic.
func AddOpAll(jl *JSONLogic) {
jl.AddOperation("all", opAll)
}
func opAll(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("all: expect at least two params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
arr, ok := scopedData.([]interface{})
if !ok {
return nil, fmt.Errorf("all: expect array as the first param but got %T", scopedData)
}
if len(arr) == 0 {
return false, nil
}
for _, item := range arr {
r, err := apply(scopedLogic, item)
if err != nil {
return nil, err
}
if !ToBool(r) {
return false, nil
}
}
return true, nil
}
// AddOpNone adds "none" operation to the JSONLogic instance. Param restriction:
// - At least two params: the first evaluated to an array and the second the logic.
func AddOpNone(jl *JSONLogic) {
jl.AddOperation("none", opNone)
}
func opNone(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("none: expect at least two params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
arr, ok := scopedData.([]interface{})
if !ok {
return nil, fmt.Errorf("none: expect array as the first param but got %T", scopedData)
}
if len(arr) == 0 {
return true, nil
}
for _, item := range arr {
r, err := apply(scopedLogic, item)
if err != nil {
return nil, err
}
if ToBool(r) {
return false, nil
}
}
return true, nil
}
// AddOpSome adds "some" operation to the JSONLogic instance. Param restriction:
// - At least two params: the first evaluated to an array and the second the logic.
func AddOpSome(jl *JSONLogic) {
jl.AddOperation("some", opSome)
}
func opSome(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("none: expect at least two params")
}
scopedData, err := apply(params[0], data)
if err != nil {
return
}
scopedLogic := params[1]
arr, ok := scopedData.([]interface{})
if !ok {
return nil, fmt.Errorf("some: expect array as the first param but got %T", scopedData)
}
if len(arr) == 0 {
return false, nil
}
for _, item := range arr {
r, err := apply(scopedLogic, item)
if err != nil {
return nil, err
}
if ToBool(r) {
return true, nil
}
}
return false, nil
}
// AddOpMerge adds "merge" operation to the JSONLogic instance.
func AddOpMerge(jl *JSONLogic) {
jl.AddOperation("merge", opMerge)
}
func opMerge(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
ret := []interface{}{}
for _, param := range params {
if arr, ok := param.([]interface{}); ok {
ret = append(ret, arr...)
} else {
ret = append(ret, param)
}
}
return ret, nil
}
// AddOpIn adds "in" operation to the JSONLogic instance. Params restriction:
// - At least two params: the first to check and the second evaluated to an array or string.
// - All items must be evaluated to json primitives.
func AddOpIn(jl *JSONLogic) {
jl.AddOperation("in", opIn)
}
func opIn(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("in: expect at least two params")
}
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
param0 := params[0]
if !IsPrimitive(param0) {
return nil, fmt.Errorf("in: expect json primitive as first param but got %T", param0)
}
switch param1 := params[1].(type) {
case []interface{}:
for _, item := range param1 {
if !IsPrimitive(item) {
return nil, fmt.Errorf("in: expect json primitives in array but got %T", item)
}
if param0 == item {
return true, nil
}
}
return false, nil
case string:
s, err := ToString(param0)
if err != nil {
return nil, err
}
return strings.Contains(param1, s), nil
default:
return nil, fmt.Errorf("in: expect array/string as the second param but got %T", params[1])
}
}
// AddOpCat adds "cat" operation to the JSONLogic instance. Params restriction:
// - All items must be evaluated to json primitives that can converted to string.
func AddOpCat(jl *JSONLogic) {
jl.AddOperation("cat", opCat)
}
func opCat(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
parts := []string{}
for _, param := range params {
s, err := ToString(param)
if err != nil {
return nil, err
}
parts = append(parts, s)
}
return strings.Join(parts, ""), nil
}
// AddOpSubstr adds "substr" operation to the JSONLogic instance. Params restriction:
// - Two to three params: the first evaluated to string, and the second/third to numbers.
func AddOpSubstr(jl *JSONLogic) {
jl.AddOperation("substr", opSubstr)
}
func opSubstr(apply Applier, params []interface{}, data interface{}) (res interface{}, err error) {
if len(params) < 2 {
return nil, fmt.Errorf("substr: expect at least two params")
}
params, err = ApplyParams(apply, params, data)
if err != nil {
return
}
s, err := ToString(params[0])
if err != nil {
return nil, err
}
r := []rune(s)
var start int
{
param1, err := ToNumeric(params[1])
if err != nil {
return nil, err
}
start = int(param1)
if start < 0 {
start += len(r)
if start < 0 {
start = 0
}
}
}
var (
end int
hasEnd bool
)
if len(params) > 2 {
param2, err := ToNumeric(params[2])
if err != nil {
return nil, err
}
end = int(param2)
hasEnd = true
}
if !hasEnd {
return string(r[start:]), nil
}
if end >= 0 {
return string(r[start : start+end]), nil
}
end += len(r)
if end < 0 {
end = 0
}
return string(r[start:end]), nil
}