-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
955 lines (930 loc) · 22.3 KB
/
util.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
package validator
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/goodluckxu-go/validator/condition"
"github.com/goodluckxu-go/validator/param"
"io"
"net/http"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
// 读取body内容
func readBody(r *http.Request) []byte {
// 从原有Request.Body读取
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return bodyBytes
}
// 新建缓冲区并替换原有Request.body
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
return bodyBytes
}
// 将规则和数据处理成单条
func disintegrateRules(rules []Rule, data interface{}, init bool, rsPtr *[]ruleTree, args ...interface{}) (err error) {
rs := *rsPtr
pathIndexMap := map[string]int{}
pathIndex := 0
if len(args) > 1 {
pathIndexMap, _ = args[0].(map[string]int)
pathIndex, _ = args[1].(int)
}
var otherRules []Rule
boolMap := map[string]interface{}{} // 是否时map类型
n := len(rules)
sameBeforePrefix := ""
for index := 0; index < n; index++ {
rule := rules[index]
if !init {
data = rule.data
}
path := ""
if _, ok := pathIndexMap[path]; !ok {
pathIndexMap[path] = pathIndex
pathIndex++
rs = append(rs, ruleTree{
path: path,
prefix: noPrefix,
data: data,
index: pathIndex - 1,
parentIndex: -1,
})
}
if rule.prefix == "" && rule.Field == "" {
rs[0].methods = rule.Methods
rs[0].notes = rule.Notes
}
fieldList := strings.Split(rule.Field, ".")
if fieldList[0] == "*" {
if boolMap[rule.prefix] == nil {
boolMap[rule.prefix] = false
} else if boolMap[rule.prefix] == true {
err = fmt.Errorf("%s冲突,map和slice不能并存", rule.prefix)
return
}
// 数组
if sameBeforePrefix == rule.prefix {
continue
}
sameBeforePrefix = rule.prefix
jump := 0
for i := index; i < n; i++ {
newRule := rules[i]
if newRule.prefix != sameBeforePrefix {
break
}
jump++
}
dataList, _ := data.([]interface{})
// 带*的变成0
if len(dataList) == 0 {
dataList = []interface{}{nil}
}
for k, v := range dataList {
for i := index; i < index+jump; i++ {
newRule := rules[i]
newFieldList := strings.Split(newRule.Field, ".")
if len(newFieldList) == 1 {
continue
}
if newFieldList[1] == "*" {
otherRules = append(otherRules, Rule{
Field: strings.Join(newFieldList[1:], "."),
data: v,
prefix: fieldJoin(rule.prefix, k),
Methods: newRule.Methods,
Notes: newRule.Notes,
})
} else {
vMap, _ := v.(map[string]interface{})
otherRules = append(otherRules, Rule{
Field: strings.Join(newFieldList[1:], "."),
data: map[string]interface{}{
newFieldList[1]: vMap[newFieldList[1]],
},
prefix: fieldJoin(rule.prefix, k),
Methods: newRule.Methods,
Notes: newRule.Notes,
})
}
}
path = fieldJoin(rule.prefix, k)
pathIndexMap[path] = pathIndex
pathIndex++
parentIndex := -1
if tmp, ok := pathIndexMap[rule.prefix]; ok {
parentIndex = tmp
rsLen := len(rs)
if rs[parentIndex].firstChildIndex == 0 {
rs[parentIndex].firstChildIndex = rsLen
}
rs[parentIndex].lastChildIndex = rsLen
}
treeItem := ruleTree{
path: path,
prefix: rule.prefix,
field: fieldJoin(k),
data: v,
index: pathIndex - 1,
parentIndex: parentIndex,
}
if len(fieldList) == 1 {
treeItem.methods = rule.Methods
treeItem.notes = rule.Notes
}
rs = append(rs, treeItem)
}
index += jump - 1
continue
} else {
if boolMap[rule.prefix] == nil {
boolMap[rule.prefix] = true
} else if boolMap[rule.prefix] == false {
err = fmt.Errorf("%s冲突,map和slice不能并存", rule.prefix)
return
}
// 对象
dataMap, _ := data.(map[string]interface{})
if len(fieldList) > 1 {
otherRules = append(otherRules, Rule{
Field: strings.Join(fieldList[1:], "."),
data: dataMap[fieldList[0]],
prefix: fieldJoin(rule.prefix, fieldList[0]),
Methods: rule.Methods,
Notes: rule.Notes,
})
}
path = fieldJoin(rule.prefix, fieldList[0])
if _, ok := pathIndexMap[path]; ok {
continue
}
pathIndexMap[path] = pathIndex
pathIndex++
parentIndex := -1
if tmp, ok := pathIndexMap[rule.prefix]; ok {
parentIndex = tmp
rsLen := len(rs)
if rs[parentIndex].firstChildIndex == 0 {
rs[parentIndex].firstChildIndex = rsLen
}
rs[parentIndex].lastChildIndex = rsLen
}
treeItem := ruleTree{
path: path,
prefix: rule.prefix,
field: fieldList[0],
data: dataMap[fieldList[0]],
index: pathIndex - 1,
parentIndex: parentIndex,
}
if len(fieldList) == 1 {
treeItem.methods = rule.Methods
treeItem.notes = rule.Notes
}
rs = append(rs, treeItem)
}
}
if len(otherRules) > 0 {
if err = disintegrateRules(otherRules, nil, false, &rs, pathIndexMap, pathIndex); err != nil {
return
}
}
*rsPtr = rs
return
}
func ruleTreeSort(list []ruleTree, index int) (rs []*ruleTree) {
size := len(list)
stackList := make([]*ruleTree, size)
stackIndex := 0
rs = make([]*ruleTree, size)
rsIndex := 0
n := len(list)
for i := 0; i < n; i++ {
if list[i].parentIndex == index {
stackList[stackIndex] = &list[i]
stackIndex++
for stackIndex > 0 {
stackIndex--
curr := stackList[stackIndex]
rs[rsIndex] = curr
rsIndex++
if curr.lastChildIndex == 0 {
continue
}
for j := curr.lastChildIndex; j >= curr.firstChildIndex; j-- {
stackList[stackIndex] = &list[j]
stackIndex++
}
}
} else {
break
}
}
return
}
type stack struct {
path string
tree ruleTree
}
func searchTree(path string, treeList []ruleTree) (rs []ruleTree) {
var stackList []stack
stackList = append(stackList, stack{
path: path,
tree: treeList[0],
})
for len(stackList) > 0 {
curr := stackList[len(stackList)-1]
stackList = stackList[:len(stackList)-1]
// 搜索
pathList := strings.Split(curr.path, ".")
for i := curr.tree.firstChildIndex; i <= curr.tree.lastChildIndex; i++ {
newTree := treeList[i]
if pathList[0] == "*" {
if len(pathList) == 1 {
rs = append(rs, newTree)
} else {
stackList = append(stackList, stack{
path: strings.Join(pathList[1:], "."),
tree: newTree,
})
}
} else if treeList[i].field == pathList[0] {
if len(pathList) == 1 {
rs = append(rs, newTree)
} else {
stackList = append(stackList, stack{
path: strings.Join(pathList[1:], "."),
tree: newTree,
})
}
break
}
}
}
// 倒序
n := len(rs)
for i := 0; i < n/2; i++ {
rs[i], rs[n-1-i] = rs[n-1-i], rs[i]
}
return rs
}
func isSlice(prefix string) bool {
if prefix == "*" {
return true
}
if _, err := strconv.Atoi(prefix); err == nil {
return true
}
return false
}
func fieldJoin(vs ...interface{}) string {
if len(vs) == 0 {
return ""
}
var buf bytes.Buffer
for _, v := range vs {
s := ""
switch v.(type) {
case string:
s = v.(string)
case int:
s = strconv.Itoa(v.(int))
}
if s == "" {
continue
}
if buf.Len() > 0 {
_, _ = buf.WriteString(".")
}
_, _ = buf.WriteString(s)
}
return buf.String()
}
func stringJoin(s, deletePrefix string, vs ...interface{}) string {
var buf bytes.Buffer
if s != "" {
buf.WriteString(s)
}
for _, v := range vs {
switch v.(type) {
case string:
buf.WriteString(v.(string))
case []byte:
buf.Write(v.([]byte))
default:
buf.WriteString(toString(v))
}
}
s = buf.String()
if deletePrefix != "" {
s = strings.TrimPrefix(s, deletePrefix)
}
return s
}
func upData(data interface{}, key string, value interface{}) interface{} {
if key == "" {
return value
}
keyList := strings.Split(key, ".")
firstKey := keyList[0]
otherKey := strings.Join(keyList[1:], ".")
switch data.(type) {
case map[string]interface{}:
dataMap, _ := data.(map[string]interface{})
dataMap[firstKey] = upData(dataMap[firstKey], otherKey, value)
data = dataMap
case []interface{}:
index, _ := strconv.Atoi(firstKey)
dataList, _ := data.([]interface{})
if len(dataList) > index {
dataList[index] = upData(dataList[index], otherKey, value)
}
data = dataList
}
return data
}
func parseLang(langAddr string) {
if langAddr == "" {
return
}
var f *os.File
var err error
if f, err = os.Open(langAddr); err != nil {
panic(err)
}
var by []byte
if by, err = io.ReadAll(f); err != nil {
panic(err)
}
langMap := map[string]interface{}{}
if err = json.Unmarshal(by, &langMap); err != nil {
panic(err)
}
langValue := reflect.ValueOf(&lang).Elem()
langLen := langValue.NumField()
for key, val := range langMap {
for i := 0; i < langLen; i++ {
field := langValue.Type().Field(i).Name
if key == field {
langValue.Field(i).Set(reflect.ValueOf(val))
break
}
}
}
}
func validError(langStr string, message string, arg langArg) error {
if message != "" {
return errors.New(message)
}
if arg.notes != nil {
langStr = strings.ReplaceAll(langStr, "${notes}", toString(arg.notes))
}
if arg.array != nil {
langStr = strings.ReplaceAll(langStr, "${array}", toString(arg.array))
}
if arg.compare != nil {
langStr = strings.ReplaceAll(langStr, "${compare}", toString(arg.compare))
}
if arg.len != nil {
langStr = strings.ReplaceAll(langStr, "${len}", toString(arg.len))
}
return errors.New(langStr)
}
func getCommonFullField(field, otherField string) (string, string) {
fieldList := strings.Split(field, ".")
otherFieldList := strings.Split(otherField, ".")
var rsList []string
i := 0
for index, ov := range otherFieldList {
if len(fieldList) <= index {
break
}
fv := fieldList[index]
if ov != "*" && ov != fv {
break
}
rsList = append(rsList, fv)
i++
}
return strings.Join(rsList, "."), strings.Join(otherFieldList[i:], ".")
}
// 比较两个数是否相等
func isEqualData(dataOne, dataTwo interface{}) bool {
switch dataOne.(type) {
case string:
if dataTwoDate, ok := dataTwo.(time.Time); ok {
dataOneDate, err := timeParse(fmt.Sprintf("%v", dataOne))
if err != nil {
return false
}
if dataOneDate.Unix() == dataTwoDate.Unix() {
return true
}
}
if dataOne.(string) == fmt.Sprintf("%v", dataTwo) {
return true
}
case int, float64:
dataOneFloat64 := toFloat64(dataOne)
dataTwoFloat64 := toFloat64(dataTwo)
if dataOneFloat64 == dataTwoFloat64 {
return true
}
case bool:
if _, ok := dataTwo.(bool); !ok {
return false
}
if compareDataBool, bl := dataTwo.(bool); bl {
if dataOne.(bool) == compareDataBool {
return true
}
}
default:
if reflect.DeepEqual(dataOne, dataTwo) {
return true
}
}
return false
}
// 比较两个数值,数字或字符串的数字或日期
func isCompareData(dataOne, dataTwo interface{}) (int, error) {
var dataOneFloat64, dataTwoFloat64 float64
var err error
errInfo := errors.New("比较的数必须是数字,字符串的数字或日期且类型匹配")
switch dataOne.(type) {
case int64, float64:
dataOneFloat64 = toFloat64(dataOne)
dataTwoFloat64 = toFloat64(dataTwo)
case string:
isNumber := true
dataOneFloat64, err = strconv.ParseFloat(fmt.Sprintf("%v", dataOne), 64)
if err != nil {
isNumber = false
}
if isNumber {
dataTwoFloat64, err = strconv.ParseFloat(fmt.Sprintf("%v", dataTwo), 64)
if err != nil {
return 0, errInfo
}
} else {
var dataOneDate, dataTwoDate time.Time
dataOneDate, err = timeParse(fmt.Sprintf("%v", dataOne))
if err != nil {
return 0, errInfo
}
var ok bool
if dataTwoDate, ok = dataTwo.(time.Time); !ok {
dataTwoDate, err = timeParse(fmt.Sprintf("%v", dataTwo))
if err != nil {
return 0, errInfo
}
}
dataOneFloat64 = float64(dataOneDate.Unix())
dataTwoFloat64 = float64(dataTwoDate.Unix())
}
default:
return 0, errInfo
}
if dataOneFloat64 > dataTwoFloat64 {
return 1, nil
}
if dataOneFloat64 < dataTwoFloat64 {
return -1, nil
}
return 0, nil
}
// 字符串转时间
func timeParse(date string) (time.Time, error) {
formatAtByte := []byte("0000-00-00 00:00:00")
copy(formatAtByte, date)
return time.ParseInLocation("2006-01-02 15:04:05", string(formatAtByte), time.Local)
}
/**
* 验证时间
*/
func validDate(date string, format string) (err error) {
err = fmt.Errorf("日期格式和值不匹配,格式:%s, 值:%s", format, date)
format = strings.ReplaceAll(format, "Y", "YYYY")
format = strings.ReplaceAll(format, "m", "mm")
format = strings.ReplaceAll(format, "d", "dd")
format = strings.ReplaceAll(format, "H", "HH")
format = strings.ReplaceAll(format, "i", "ii")
format = strings.ReplaceAll(format, "s", "ss")
if len(date) != len(format) {
return
}
if err = validSingleDate("YYYY", &date, &format); err != nil {
return err
}
if err = validSingleDate("mm", &date, &format); err != nil {
return err
}
if err = validSingleDate("dd", &date, &format); err != nil {
return err
}
if err = validSingleDate("HH", &date, &format); err != nil {
return err
}
if err = validSingleDate("ii", &date, &format); err != nil {
return err
}
if err = validSingleDate("ss", &date, &format); err != nil {
return err
}
if date != format {
return
}
return nil
}
func validSingleDate(single string, date, format *string) error {
lenSingle := len(single)
newDate := *date
newFormat := *format
for {
index := strings.Index(newFormat, single)
if index == -1 {
break
}
validDate := newDate[index : index+lenSingle]
switch single {
case "YYYY":
if !regexp.MustCompile(`\d{4}`).MatchString(validDate) {
return fmt.Errorf("年格式和值不匹配,格式:Y, 值:%s", validDate)
}
case "mm":
validInt64, _ := strconv.ParseInt(validDate, 10, 64)
if validInt64 < 1 || validInt64 > 12 {
return fmt.Errorf("月格式和值不匹配,格式:m, 值:%s", validDate)
}
case "dd":
validInt64, _ := strconv.ParseInt(validDate, 10, 64)
if validInt64 < 1 || validInt64 > 31 {
return fmt.Errorf("日格式和值不匹配,格式:d, 值:%s", validDate)
}
case "HH":
validInt64, _ := strconv.ParseInt(validDate, 10, 64)
if validInt64 < 0 || validInt64 > 23 {
return fmt.Errorf("时格式和值不匹配,格式:H, 值:%s", validDate)
}
case "ii":
validInt64, _ := strconv.ParseInt(validDate, 10, 64)
if validInt64 < 0 || validInt64 > 59 {
return fmt.Errorf("分格式和值不匹配,格式:i, 值:%s", validDate)
}
case "ss":
validInt64, _ := strconv.ParseInt(validDate, 10, 64)
if validInt64 < 0 || validInt64 > 59 {
return fmt.Errorf("秒格式和值不匹配,格式:s, 值:%s", validDate)
}
}
newFormat = newFormat[0:index] + newFormat[index+lenSingle:]
newDate = newDate[0:index] + newDate[index+lenSingle:]
}
*date = newDate
*format = newFormat
return nil
}
// 验证参数
// args 参数
// minNum,maxNum 最小和最大参数数量
// ins 需要在这个列表中
func validArgs(args []interface{}, minNum, maxNum int, ins ...interface{}) error {
if minNum < 0 {
return fmt.Errorf("minNum必须大于等于0")
}
if minNum == maxNum && len(args) != minNum {
return fmt.Errorf("验证规则错误: 参数数量必须是%d", minNum)
}
if len(args) < minNum || (maxNum != -1 && len(args) > maxNum) {
if maxNum == -1 {
return fmt.Errorf("验证规则错误: 参数数量必须大于%d", minNum)
}
return fmt.Errorf("验证规则错误: 参数数量必须在%d-%d之间", minNum, maxNum)
}
for index, arg := range args {
if len(ins) <= index {
continue
}
if ins[index] == nil {
continue
}
if !inArray(arg, ins[index]) {
return fmt.Errorf("验证规则错误: 第%d个参数%v不在%v中", index+1, arg, ins[index])
}
}
return nil
}
// 公式比较
func formulaCompare(d *Data, args ...interface{}) (bool, error) {
var formulaList [][]interface{}
var formulaArgs []interface{}
var formulaSymbolList []string
for _, arg := range args {
switch arg.(type) {
case *condition.Formula:
bl, err := formulaCompare(d, arg.(*condition.Formula).Args...)
if err != nil {
return false, err
}
formulaArgs = append(formulaArgs, bl)
case string:
switch arg.(string) {
case "&&", "||":
formulaSymbolList = append(formulaSymbolList, arg.(string))
formulaList = append(formulaList, formulaArgs)
formulaArgs = []interface{}{}
default:
formulaArgs = append(formulaArgs, arg)
}
default:
formulaArgs = append(formulaArgs, arg)
}
}
if len(formulaArgs) > 0 {
formulaList = append(formulaList, formulaArgs)
}
var index int
var isBool bool
for index, formulaArgs = range formulaList {
if len(formulaArgs) == 1 {
if rs, ok := formulaArgs[0].(bool); ok {
isBool = rs
continue
}
return false, fmt.Errorf("公式错误: %v", formulaArgs)
}
var leftData interface{}
var rightData interface{}
var symbol string
if len(formulaArgs) == 2 {
symbol, _ = formulaArgs[0].(string)
leftData = d.GetValidData()
rightData = formulaArgs[1]
} else if len(formulaArgs) == 3 {
symbol, _ = formulaArgs[1].(string)
leftData = formulaArgs[0]
rightData = formulaArgs[2]
if field, ok := formulaArgs[0].(param.Field); ok {
rightData = true
for _, data := range d.GetLevelData(string(field)) {
bl, err := formulaCompare(d, data.Data, symbol, formulaArgs[2])
if err != nil {
return false, err
}
if !bl {
rightData = false
}
}
leftData = true
symbol = "="
}
} else {
return false, fmt.Errorf("公式错误: %v", formulaArgs)
}
formulaSymbol := ""
if len(formulaSymbolList) > index-1 && index > 0 {
formulaSymbol = formulaSymbolList[index-1]
}
var err error
var compareData int
var compareBool bool
switch symbol {
case ">":
compareData, err = isCompareData(leftData, rightData)
if err != nil {
return false, err
}
if compareData == 1 {
compareBool = true
}
case ">=":
compareData, err = isCompareData(leftData, rightData)
if err != nil {
return false, err
}
if compareData != -1 {
compareBool = true
}
case "<":
compareData, err = isCompareData(leftData, rightData)
if err != nil {
return false, err
}
if compareData == -1 {
compareBool = true
}
case "<=":
compareData, err = isCompareData(leftData, rightData)
if err != nil {
return false, err
}
if compareData != 1 {
compareBool = true
}
case "=":
if isEqualData(leftData, rightData) {
compareBool = true
}
case "!=":
if !isEqualData(leftData, rightData) {
compareBool = true
}
case "in":
if inArray(leftData, rightData) {
compareBool = true
}
case "not":
if !inArray(leftData, rightData) {
compareBool = true
}
default:
return false, fmt.Errorf("公式错误: %v", formulaArgs)
}
switch formulaSymbol {
case "&&":
if !isBool {
continue
}
isBool = compareBool
case "||":
if isBool {
continue
}
isBool = compareBool
default:
isBool = compareBool
}
}
return isBool, nil
}
func inArray(val interface{}, array interface{}) (exists bool) {
exists = false
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
exists = true
return
}
}
}
return
}
func inArrayRuleTree(v string, list []ruleTree) bool {
for _, val := range list {
if val.path == v {
return true
}
}
return false
}
// 获取float64类型数据
func toFloat64(i interface{}) float64 {
if i == nil {
return 0
}
var float64I float64
switch i.(type) {
case int:
float64I = float64(i.(int))
case int8:
float64I = float64(i.(int8))
case int16:
float64I = float64(i.(int16))
case int32:
float64I = float64(i.(int32))
case int64:
float64I = float64(i.(int64))
case uint:
float64I = float64(i.(uint))
case uint8:
float64I = float64(i.(uint8))
case uint16:
float64I = float64(i.(uint16))
case uint32:
float64I = float64(i.(uint32))
case uint64:
float64I = float64(i.(uint64))
case float32:
float64I = float64(i.(float32))
case float64:
float64I = i.(float64)
case string:
float64I, _ = strconv.ParseFloat(i.(string), 64)
case bool:
if i.(bool) {
float64I = 1
}
}
return float64I
}
func toString(i interface{}) string {
if i == nil {
return ""
}
switch i.(type) {
case string:
return i.(string)
case *string:
return *i.(*string)
case int, int8, int16, int32, int64:
var i64 int64
switch i.(type) {
case int:
i64 = int64(i.(int))
case int8:
i64 = int64(i.(int8))
case int16:
i64 = int64(i.(int16))
case int32:
i64 = int64(i.(int32))
case int64:
i64 = i.(int64)
}
return strconv.FormatInt(i64, 10)
case *int, *int8, *int16, *int32, *int64:
var i64 int64
switch i.(type) {
case *int:
i64 = int64(*i.(*int))
case *int8:
i64 = int64(*i.(*int8))
case *int16:
i64 = int64(*i.(*int16))
case *int32:
i64 = int64(*i.(*int32))
case *int64:
i64 = *i.(*int64)
}
return strconv.FormatInt(i64, 10)
case *uint, *uint8, *uint16, *uint32, *uint64:
var ui64 uint64
switch i.(type) {
case *uint:
ui64 = uint64(*i.(*uint))
case *uint8:
ui64 = uint64(*i.(*uint8))
case *uint16:
ui64 = uint64(*i.(*uint16))
case *uint32:
ui64 = uint64(*i.(*uint32))
case *uint64:
ui64 = *i.(*uint64)
}
return strconv.FormatUint(ui64, 10)
case uint, uint8, uint16, uint32, uint64:
var ui64 uint64
switch i.(type) {
case uint:
ui64 = uint64(i.(uint))
case uint8:
ui64 = uint64(i.(uint8))
case uint16:
ui64 = uint64(i.(uint16))
case uint32:
ui64 = uint64(i.(uint32))
case uint64:
ui64 = i.(uint64)
}
return strconv.FormatUint(ui64, 10)
case float32, float64:
var f64 float64
switch i.(type) {
case float32:
f64 = float64(i.(float32))
case float64:
f64 = i.(float64)
}
return strconv.FormatFloat(f64, 'G', -1, 64)
case *float32, *float64:
var f64 float64
switch i.(type) {
case *float32:
f64 = float64(*i.(*float32))
case *float64:
f64 = *i.(*float64)
}
return strconv.FormatFloat(f64, 'G', -1, 64)
case bool:
return strconv.FormatBool(i.(bool))
case complex64, complex128:
var c128 complex128
switch i.(type) {
case complex64:
c128 = complex128(i.(complex64))
case complex128:
c128 = i.(complex128)
}
return strconv.FormatComplex(c128, 'G', -1, 128)
case error:
return i.(error).Error()
}
return fmt.Sprintf("%v", i)
}