/
parser.go
295 lines (267 loc) · 6.52 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
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
package layouts
import (
"net/mail"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
type fieldTags struct {
Column string
CommaSeparatedValue bool
Email bool
Required bool
Regex string
DateFormat string
Max float64
Min float64
MaxLength int64
MinLength int64
Url bool
Unique bool
hasMin bool
hasMax bool
hasMinLength bool
hasMaxLength bool
}
func parseOptions(tags string) (fieldTags, error) {
ft := fieldTags{}
tags = strings.TrimSpace(tags)
if len(tags) == 0 {
return ft, ErrTagNoFieldTag
}
name := "excelLayout:"
res := strings.Index(tags, name)
if res < 0 {
return ft, ErrTagNoFieldTag
}
tags = tags[res+len(name):]
if len(tags) == 0 {
return ft, ErrTagEmptyFieldTag
}
res = strings.Index(tags, "\"")
if res < 0 {
return ft, ErrTagEmptyFieldTag
}
tags = tags[res+1:]
res = strings.Index(tags, "\"")
if res < 0 {
return ft, ErrTagEmptyFieldTag
}
tags = strings.TrimSpace(tags[:res])
if len(tags) < 1 {
return ft, ErrTagEmptyFieldTag
}
options := strings.Split(tags, ",")
if len(options) == 0 {
return ft, ErrTagEmptyFieldTag
}
for _, o := range options {
pair := strings.SplitN(o, ":", 2)
key := strings.ToLower(strings.TrimSpace(pair[0]))
val := ""
if len(pair) > 1 {
val = strings.TrimSpace(pair[1])
}
switch key {
case "column":
if val == "" {
return ft, ErrTagMissingColumnValue
}
ft.Column = strings.TrimSpace(strings.ToUpper(pair[1]))
case "commaseparatedvalue":
ft.CommaSeparatedValue = true
case "dateformat":
if val == "" {
return ft, ErrTagMissingDateFormatValue
}
ft.DateFormat = strings.TrimSpace(pair[1])
case "regex":
if val == "" {
return ft, ErrTagMissingRegexValue
}
ft.Regex = strings.TrimSpace(pair[1])
case "email":
ft.Email = true
case "required":
ft.Required = true
case "max":
if val == "" {
return ft, ErrTagMissingMaxValue
}
ft.hasMax = true
v, _ := strconv.ParseFloat(val, 32)
ft.Max = v
case "min":
if val == "" {
return ft, ErrTagMissingMinValue
}
ft.hasMin = true
v, _ := strconv.ParseFloat(val, 32)
ft.Min = v
case "maxlength":
if val == "" {
return ft, ErrTagMissingMaxValue
}
ft.hasMaxLength = true
v, _ := strconv.ParseInt(val, 0, 32)
ft.MaxLength = v
case "minlength":
if val == "" {
return ft, ErrTagMissingMaxValue
}
ft.hasMinLength = true
v, _ := strconv.ParseInt(val, 0, 32)
ft.MinLength = v
case "url":
ft.Url = true
case "unique":
ft.Unique = true
}
}
if ft.Regex != "" {
if _, err := regexp.Compile(ft.Regex); err != nil {
return ft, ErrRegexInvalid
}
}
if (ft.hasMax && ft.hasMin) && (ft.Max < ft.Min) {
return ft, ErrTagInvalidMaxMinValues
}
if (ft.hasMaxLength && ft.hasMinLength) && (ft.MaxLength < ft.MinLength) {
return ft, ErrTagInvalidMaxMinLengthValues
}
return ft, nil
}
func parseStringRules(v string, tags fieldTags) (string, []error) {
value := strings.TrimSpace(v)
errors := []error{}
if tags.Required && len(value) == 0 {
errors = append(errors, ErrRequiredValueRuleFail)
}
if tags.hasMin {
errors = append(errors, ErrTagMinForbidden)
}
if tags.hasMax {
errors = append(errors, ErrTagMaxForbidden)
}
if len(value) > 0 {
if tags.hasMinLength && (int(tags.MinLength) > len(value)) {
errors = append(errors, ErrMinLengthValueRuleFail)
}
if tags.hasMaxLength && (int(tags.MaxLength) < len(value)) {
errors = append(errors, ErrMaxLengthValueRuleFail)
}
if tags.Url {
if _, err := url.ParseRequestURI(value); err != nil {
errors = append(errors, ErrUrlValueRuleFail)
}
}
if tags.Email {
if _, err := mail.ParseAddress(value); err != nil {
errors = append(errors, ErrEmailValueRuleFail)
}
}
if tags.Regex != "" {
regex, err := regexp.Compile(tags.Regex)
if err != nil {
errors = append(errors, ErrRegexInvalid)
}
if match := regex.MatchString(value); !match {
errors = append(errors, ErrRegexRuleFail)
}
}
}
if len(errors) > 0 {
return "", errors
}
return value, nil
}
func parseIntRules(v string, tags fieldTags) (int64, []error) {
value := strings.TrimSpace(v)
errors := []error{}
if tags.Required && value == "" {
errors = append(errors, ErrRequiredValueRuleFail)
} else if !tags.Required && value == "" {
value = "0"
}
if tags.hasMinLength {
errors = append(errors, ErrTagMinLengthForbidden)
}
if tags.hasMaxLength {
errors = append(errors, ErrTagMaxLengthForbidden)
}
val, err := strconv.Atoi(value)
if err != nil {
errors = append(errors, ErrIntegerInvalid)
return 0, errors
}
if tags.hasMin && int(tags.Min) > val {
errors = append(errors, ErrMinValueRuleFail)
}
if tags.hasMax && int(tags.Max) < val {
errors = append(errors, ErrMaxValueRuleFail)
}
if len(errors) > 0 {
return 0, errors
}
return int64(val), nil
}
func parseFloat64Rules(v string, tags fieldTags) (float64, []error) {
value := strings.TrimSpace(v)
errors := []error{}
if tags.Required && value == "" {
errors = append(errors, ErrRequiredValueRuleFail)
} else if !tags.Required && value == "" {
value = "0"
}
if tags.hasMinLength {
errors = append(errors, ErrTagMinLengthForbidden)
}
if tags.hasMaxLength {
errors = append(errors, ErrTagMaxLengthForbidden)
}
val, err := strconv.ParseFloat(value, 64)
if err != nil {
errors = append(errors, ErrDecimalInvalid)
return 0, errors
}
if tags.hasMin && tags.Min > val {
errors = append(errors, ErrMinValueRuleFail)
}
if tags.hasMax && tags.Max < val {
errors = append(errors, ErrMaxValueRuleFail)
}
if len(errors) > 0 {
return 0.0, errors
}
return val, nil
}
func parseTimeRules(v string, tags fieldTags) (time.Time, []error) {
errs := []error{}
var date time.Time
if tags.Required && v == "" {
errs = append(errs, ErrRequiredValueRuleFail)
}
if !tags.Required && v == "" {
return date, errs
}
if tags.DateFormat == "" {
tags.DateFormat = "2006-01-02 03:04:05"
}
regex := regexp.MustCompile(`^(?:(?:0|[1-9]\d*)(?:\.\d*)?|\.\d+)(?:\d[eE][+\-]?\d+)?$`)
match := regex.MatchString(v)
if match {
in, _ := strconv.ParseFloat(v, 64)
excelEpoch := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC)
date = excelEpoch.Add(time.Duration(in * float64(24*time.Hour)))
} else if parsed, err := time.Parse(tags.DateFormat, v); err == nil {
date = parsed
} else {
errs = append(errs, ErrDateFormatInvalid)
}
if len(errs) == 0 {
return date, nil
}
return date, errs
}