-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_parser.go
148 lines (129 loc) · 2.73 KB
/
tag_parser.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
package validator
import (
"fmt"
"strings"
)
func (v *Validator) parseTag(rawTag string) (*tagChunk, error) {
if tags, ok := v.tagCache.Load(rawTag); ok {
return tags, nil
}
var (
rootChunk tagChunk
chunk *tagChunk
orParsing = false
)
const optionalTagName = "optional"
chunk = &rootChunk
s := newTagScanner(rawTag)
loop:
for {
token, lit := s.Scan()
if lit == optionalTagName {
chunk.Optional = true
}
switch token {
case eof:
if lit == optionalTagName {
continue
}
if lit == "" {
break loop
}
if orParsing {
idx := len(chunk.Tags) - 1
chunk.Tags[idx].params = append(chunk.Tags[idx].params, lit)
} else {
tag, err := v.newTag(lit)
if err != nil {
return nil, err
}
chunk.Tags = append(chunk.Tags, tag)
}
break loop
case tagSeparator:
if lit == optionalTagName {
continue
}
if lit == "" {
return nil, fmt.Errorf("parse: invalid literal in tag separator")
}
if orParsing {
idx := len(chunk.Tags) - 1
chunk.Tags[idx].params = append(chunk.Tags[idx].params, lit)
} else {
tag, err := v.newTag(lit)
if err != nil {
return nil, err
}
chunk.Tags = append(chunk.Tags, tag)
}
orParsing = false
case orSeparator:
if lit == optionalTagName {
continue
}
if lit == "" {
return nil, fmt.Errorf("parse: invalid literal in or separator")
}
if orParsing {
idx := len(chunk.Tags) - 1
chunk.Tags[idx].params = append(chunk.Tags[idx].params, lit)
} else {
chunk.Tags = append(chunk.Tags, Tag{name: "or", params: []string{lit}, validateFn: v.funcMap["or"]})
}
orParsing = true
case nextSeparator:
if lit != "" && lit != optionalTagName {
if orParsing {
idx := len(chunk.Tags) - 1
chunk.Tags[idx].params = append(chunk.Tags[idx].params, lit)
} else {
tag, err := v.newTag(lit)
if err != nil {
return nil, err
}
chunk.Tags = append(chunk.Tags, tag)
}
}
orParsing = false
chunk.Next = &tagChunk{}
chunk = chunk.Next
}
}
v.tagCache.Store(rawTag, &rootChunk)
return &rootChunk, nil
}
// newTag returns Tag.
func (v *Validator) newTag(lit string) (Tag, error) {
var (
name string
params []string
)
idx := strings.Index(lit, "(")
if idx < 0 {
name = lit
} else {
name = lit[:idx]
s := newTagParamsScanner(lit[idx+1 : len(lit)-1])
loop:
for {
token, lit := s.Scan()
switch token {
case eof:
params = append(params, lit)
break loop
case orSeparator:
params = append(params, lit)
}
}
}
fn, ok := v.funcMap[name]
if !ok {
return Tag{}, fmt.Errorf("parse: tag %s function not found", name)
}
return Tag{
name: name,
params: params,
validateFn: fn,
}, nil
}