-
Notifications
You must be signed in to change notification settings - Fork 0
/
required.go
235 lines (225 loc) · 6.24 KB
/
required.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
package validate
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
// 解析Required表达式并返回表达式的结果
func computedRE(record reflect.Value, key string, expression string) bool {
// 先切割或运算
orE := strings.Split(expression, "|")
orRes := false
// 遍历或运算每一项
for _, e := range orE {
// 切割与运算
andE := strings.Split(e, "&")
// 与运算结果
andRes := true
// 遍历与运算每一项
for _, a := range andE {
symbol := []string{"==", "!=", "<=", ">=", "<", ">"}
for _, sb := range symbol {
if strings.Contains(a, sb) {
temp := strings.Split(a, sb)
re := regexp.MustCompile("'([^']*)'")
fields := re.FindStringSubmatch(temp[0])
// 没有拿到字段
if len(fields) <= 1 {
msg := fmt.Sprintf("属性%s的`required`表达式不正确!", key)
panic(msg)
}
// 判断结构体是否存在属性
if _, ok := record.Type().FieldByName(fields[1]); ok {
r := compare(sb, temp[1], record.FieldByName(fields[1]))
andRes = r
} else {
msg := fmt.Sprintf("结构体不存在%v的属性!", fields[1])
panic(msg)
}
break
}
}
// 如果与运算有一项结果为假,直接执行或运算的另一项
if !andRes {
break
}
}
// 如果或运算有一项结果为真,直接返回真
if andRes {
return andRes
}
}
return orRes
}
// 比较运算符两侧
func compare(symbol string, a string, b reflect.Value) bool {
_type := b.Type()
switch symbol {
case "==":
switch _type.Kind() {
// 布尔值
case reflect.Bool:
return a == "true" && b.Interface() == true || a == "false" && b.Interface() == false
// 数值类
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() == numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() == numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() == numA
} else {
panic(ok)
}
// 字符串类
case reflect.String:
// none为值的情况,判断指定属性是否为空并返回结果
if a == "none" {
return b.IsZero()
}
// 否则就是比较字符串
return b.String() == a
// 其他
default:
// none为值的情况,判断指定属性是否为空,并返回
if a == "none" && b.IsZero() {
return true
}
}
case "!=":
switch _type.Kind() {
case reflect.Bool:
return a == "true" && b.Interface() != true || a == "false" && b.Interface() != false
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() != numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() != numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() != numA
} else {
panic(ok)
}
case reflect.String:
if a == "none" {
return !b.IsZero()
}
return b.String() != a
default:
if a == "none" && !b.IsZero() {
return true
}
}
case "<=":
switch _type.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() <= numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() <= numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() <= numA
} else {
panic(ok)
}
default:
panic("只能数值类可以进行比较!")
}
case ">=":
switch _type.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() >= numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() >= numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() >= numA
} else {
panic(ok)
}
default:
panic("只能数值类可以进行比较!")
}
case "<":
switch _type.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() < numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() < numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() < numA
} else {
panic(ok)
}
default:
panic("只能数值类可以进行比较!")
}
case ">":
switch _type.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if numA, ok := strconv.ParseUint(a, 0, 0); ok == nil {
return b.Uint() > numA
} else {
panic(ok)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if numA, ok := strconv.ParseInt(a, 0, 0); ok == nil {
return b.Int() > numA
} else {
panic(ok)
}
case reflect.Float32, reflect.Float64:
if numA, ok := strconv.ParseFloat(a, 64); ok == nil {
return b.Float() > numA
} else {
panic(ok)
}
default:
panic("只能数值类可以进行比较!")
}
}
return false
}