-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrule_cache.go
More file actions
57 lines (52 loc) · 1.34 KB
/
Copy pathrule_cache.go
File metadata and controls
57 lines (52 loc) · 1.34 KB
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
package valid
import (
"errors"
"github.com/golyu/valid/rule"
"reflect"
"strconv"
"strings"
)
// 缓存field上面的tag规则
type RuleCache struct {
isMust bool // 是否必传
errCode int64 // 错误码
rules []rule.Rule // 规则
}
// genRule 切割并生成各个规则,并获取是否存在必传和错误码
// error 解析tag出错
func genRule(f reflect.StructField) (RuleCache, error) {
ruleCache := RuleCache{}
ruleCache.rules = make([]rule.Rule, 0)
tag := f.Tag.Get(VALID_TAG)
if len(tag) > 0 {
fs := strings.Split(tag, SPLIT_SEP)
if len(fs) > 0 {
for i, vTag := range fs {
switch i {
case 0: //首个,是否必传
if fs[0] == MUST {
ruleCache.isMust = true
continue
}
case len(fs) - 1: // 最后一个,校验码
if strings.HasPrefix(vTag, "ErrorCode(") && strings.HasSuffix(vTag, ")") {
codeStr := vTag[10 : len(vTag)-1]
var err error
ruleCache.errCode, err = strconv.ParseInt(codeStr, 10, 64)
if err != nil {
return ruleCache, errors.New("Parse ErrorCode tag failure:" + vTag)
}
continue
}
}
// 解析单个
r, err := rule.GetRule(vTag)
if err != nil {
return ruleCache, errors.New("Parse rule tag failure:" + vTag)
}
ruleCache.rules = append(ruleCache.rules, r)
}
}
}
return ruleCache, nil
}