-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
186 lines (175 loc) · 6.53 KB
/
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
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
package csv
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
// parseStructTag parses the struct tag and extracts the header and ruleSet.
// structSlicePointer is a pointer to a slice of structs.
func (c *CSV) parseStructTag(structSlicePointer any) error {
rv := reflect.ValueOf(structSlicePointer)
if rv.Kind() != reflect.Ptr {
return NewError(c.i18nLocalizer, ErrStructSlicePointerID, "")
}
elem := rv.Elem()
switch elem.Kind() {
case reflect.Slice, reflect.Array:
elemType := elem.Type().Elem()
if elemType.Kind() != reflect.Struct {
return NewError(c.i18nLocalizer, ErrStructSlicePointerID, "")
}
ruleSet, err := c.extractRuleSet(elemType)
if err != nil {
return err
}
c.ruleSet = ruleSet
default:
return NewError(c.i18nLocalizer, ErrStructSlicePointerID, fmt.Sprintf("element=%v", elem.Kind()))
}
return nil
}
// / extractRuleSet extracts the ruleSet from the struct.
func (c *CSV) extractRuleSet(structType reflect.Type) (ruleSet, error) {
ruleSet := make(ruleSet, 0, structType.NumField())
for i := 0; i < structType.NumField(); i++ {
tag := structType.Field(i).Tag
validators, err := c.parseValidateTag(tag.Get(validateTag.String()))
if err != nil {
return nil, err
}
ruleSet = append(ruleSet, validators)
}
return ruleSet, nil
}
// parseValidateTag parses the validate tag.
// This function return a set of Validate functions based on
// the rules specified in the validation tag.
func (c *CSV) parseValidateTag(tags string) (validators, error) {
tagList := strings.Split(tags, ",")
validatorList := make(validators, 0, len(tagList))
for _, t := range tagList {
switch {
case strings.HasPrefix(t, booleanTagValue.String()):
validatorList = append(validatorList, newBooleanValidator())
case strings.HasPrefix(t, alphaTagValue.String()) && !strings.HasPrefix(t, alphanumericTagValue.String()):
validatorList = append(validatorList, newAlphaValidator())
case strings.HasPrefix(t, numericTagValue.String()):
validatorList = append(validatorList, newNumericValidator())
case strings.HasPrefix(t, alphanumericTagValue.String()):
validatorList = append(validatorList, newAlphanumericValidator())
case strings.HasPrefix(t, requiredTagValue.String()):
validatorList = append(validatorList, newRequiredValidator())
case strings.HasPrefix(t, equalTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newEqualValidator(threshold))
case strings.HasPrefix(t, notEqualTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newNotEqualValidator(threshold))
case strings.HasPrefix(t, greaterThanTagValue.String()) && !strings.HasPrefix(t, greaterThanEqualTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newGreaterThanValidator(threshold))
case strings.HasPrefix(t, greaterThanEqualTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newGreaterThanEqualValidator(threshold))
case strings.HasPrefix(t, lessThanTagValue.String()) && !strings.HasPrefix(t, lessThanEqualTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newLessThanValidator(threshold))
case strings.HasPrefix(t, lessThanEqualTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newLessThanEqualValidator(threshold))
case strings.HasPrefix(t, minTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newMinValidator(threshold))
case strings.HasPrefix(t, maxTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newMaxValidator(threshold))
case strings.HasPrefix(t, lengthTagValue.String()):
threshold, err := c.parseThreshold(t)
if err != nil {
return nil, err
}
validatorList = append(validatorList, newLengthValidator(threshold))
case strings.HasPrefix(t, oneOfTagValue.String()):
oneOf, err := c.parseSpecifiedValues(t)
if err != nil {
return nil, NewError(c.i18nLocalizer, ErrInvalidOneOfFormatID, t)
}
validatorList = append(validatorList, newOneOfValidator(oneOf))
case strings.HasPrefix(t, lowercaseTagValue.String()):
validatorList = append(validatorList, newLowercaseValidator())
case strings.HasPrefix(t, uppercaseTagValue.String()):
validatorList = append(validatorList, newUppercaseValidator())
case strings.HasPrefix(t, asciiTagValue.String()):
validatorList = append(validatorList, newASCIIValidator())
case strings.HasPrefix(t, emailTagValue.String()):
validatorList = append(validatorList, newEmailValidator())
case strings.HasPrefix(t, containsTagValue.String()) && !strings.HasPrefix(t, containsAnyTagValue.String()):
values, err := c.parseSpecifiedValues(t)
if err != nil {
return nil, err
}
if len(values) != 1 {
return nil, NewError(c.i18nLocalizer, ErrInvalidContainsFormatID, t)
}
validatorList = append(validatorList, newContainsValidator(values[0]))
case strings.HasPrefix(t, containsAnyTagValue.String()):
values, err := c.parseSpecifiedValues(t)
if err != nil {
return nil, err
}
if len(values) == 0 {
return nil, NewError(c.i18nLocalizer, ErrInvalidContainsAnyFormatID, t)
}
validatorList = append(validatorList, newContainsAnyValidator(values))
}
}
return validatorList, nil
}
// parseThreshold parses the threshold value.
// tagValue is the value of the struct tag. e.g. eq=10, gt=5.2
func (c *CSV) parseThreshold(tagValue string) (float64, error) {
parts := strings.Split(tagValue, "=")
if len(parts) == 2 {
num, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return 0, NewError(c.i18nLocalizer, ErrInvalidThresholdFormatID, tagValue)
}
return num, nil
}
return 0, NewError(c.i18nLocalizer, ErrInvalidThresholdFormatID, tagValue)
}
// parseSpecifiedValues parses the tag values.
// tagValue is the value of the struct tag. e.g. oneof=male female prefer_not_to
func (c *CSV) parseSpecifiedValues(tagValue string) ([]string, error) {
parts := strings.Split(tagValue, "=")
if len(parts) == 2 {
return strings.Split(parts[1], " "), nil
}
return nil, errors.New("invalid tag values format")
}