-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtag_parsing_ops.go
278 lines (267 loc) · 9.62 KB
/
tag_parsing_ops.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
package valix
import (
"fmt"
"strconv"
"strings"
)
const (
tagTokenNotNull = "notNull"
tagTokenNullable = "nullable"
tagTokenMandatory = "mandatory"
tagTokenRequired = "required"
tagTokenOptional = "optional"
tagTokenOnly = "only"
tagTokenOnlyMsg = "only_msg"
tagTokenType = "type"
tagTokenConstraint = "constraint"
tagTokenConstraints = "constraints"
tagTokenConstraintsPrefix = tagTokenConstraints + ":"
tagTokenOrder = "order"
tagTokenWhen = "when"
tagTokenUnwanted = "unwanted"
tagTokenRequiredWith = "required_with"
tagTokenRequiredWithAlt = "+"
tagTokenRequiredWithMsg = "required_with_msg"
tagTokenRequiredWithAltMsg = "+msg"
tagTokenUnwantedWith = "unwanted_with"
tagTokenUnwantedWithAlt = "-"
tagTokenUnwantedWithMsg = "unwanted_with_msg"
tagTokenUnwantedWithAltMsg = "-msg"
tagTokenStopOnFirst = "stop_on_first"
tagTokenStopOnFirstAlt = "stop1st"
// object level tag items...
tagTokenObjPrefix = "obj."
tagTokenObjIgnoreUnknownProperties = tagTokenObjPrefix + "ignoreUnknownProperties"
tagTokenObjUnknownProperties = tagTokenObjPrefix + "unknownProperties" // true/false
tagTokenObjConstraint = tagTokenObjPrefix + tagTokenConstraint
tagTokenObjOrdered = tagTokenObjPrefix + "ordered"
tagTokenObjWhen = tagTokenObjPrefix + tagTokenWhen
tagTokenObjNo = tagTokenObjPrefix + "no"
// array level tag items...
tagTokenArrPrefix = "arr."
tagTokenArrAllowNullItems = tagTokenArrPrefix + "allowNulls"
)
var tagExpectsColon = map[string]bool{
tagTokenNotNull: false,
tagTokenNullable: false,
// tagTokenMandatory, tagTokenRequired: either
tagTokenOptional: false,
tagTokenStopOnFirst: false,
tagTokenStopOnFirstAlt: false,
// tagTokenOnly: either
tagTokenOnlyMsg: true,
tagTokenType: true,
tagTokenOrder: true,
tagTokenConstraint: true,
tagTokenObjConstraint: true,
tagTokenWhen: true,
tagTokenUnwanted: true,
tagTokenRequiredWith: true,
tagTokenRequiredWithAlt: true,
tagTokenUnwantedWith: true,
tagTokenUnwantedWithAlt: true,
tagTokenRequiredWithMsg: true,
tagTokenRequiredWithAltMsg: true,
tagTokenUnwantedWithMsg: true,
tagTokenUnwantedWithAltMsg: true,
tagTokenObjIgnoreUnknownProperties: false,
tagTokenObjUnknownProperties: true,
tagTokenObjOrdered: false,
tagTokenObjWhen: true,
tagTokenObjNo: false,
tagTokenArrAllowNullItems: false,
}
type tagTokenOperation func(pv *PropertyValidator, hasColon bool, tagValue string) error
var tagOpRequiredWith = func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if expr, err := ParseExpression(tagValue); err != nil {
return err
} else if pv.RequiredWith == nil {
pv.RequiredWith = expr
} else {
for _, x := range expr {
pv.RequiredWith = append(pv.RequiredWith, x)
}
}
return nil
}
var tagOpUnwantedWith = func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if expr, err := ParseExpression(tagValue); err != nil {
return err
} else if pv.UnwantedWith == nil {
pv.UnwantedWith = expr
} else {
for _, x := range expr {
pv.UnwantedWith = append(pv.UnwantedWith, x)
}
}
return nil
}
var tagOpRequiredWithMsg = func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if unq, ok := isQuotedStr(tagValue); ok {
pv.RequiredWithMessage = unq
} else {
pv.RequiredWithMessage = tagValue
}
return nil
}
var tagOpUnwantedWithMsg = func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if unq, ok := isQuotedStr(tagValue); ok {
pv.UnwantedWithMessage = unq
} else {
pv.UnwantedWithMessage = tagValue
}
return nil
}
var tagTokenOperations = map[string]tagTokenOperation{
tagTokenNotNull: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.NotNull = true
return nil
},
tagTokenNullable: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.NotNull = false
return nil
},
tagTokenMandatory: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
return pv.setTagMandatoryWhen(hasColon, tagValue)
},
tagTokenRequired: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
return pv.setTagMandatoryWhen(hasColon, tagValue)
},
tagTokenOptional: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.Mandatory = false
return nil
},
tagTokenStopOnFirst: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.StopOnFirst = true
return nil
},
tagTokenStopOnFirstAlt: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.StopOnFirst = true
return nil
},
tagTokenOnly: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.Only = true
if hasColon {
return addConditions(&pv.OnlyConditions, tagValue, true)
}
return nil
},
tagTokenOnlyMsg: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if unq, ok := isQuotedStr(tagValue); ok {
pv.OnlyMessage = unq
} else {
pv.OnlyMessage = tagValue
}
return nil
},
tagTokenType: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
ty, ok := JsonTypeFromString(tagValue)
if !ok {
return fmt.Errorf(msgUnknownPropertyType, tagValue)
}
pv.Type = ty
return nil
},
tagTokenOrder: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
v, err := strconv.ParseInt(tagValue, 10, 32)
if err != nil {
return fmt.Errorf(msgUnknownTagValue, tagTokenOrder, "int", tagValue)
}
pv.Order = int(v)
return nil
},
tagTokenConstraint: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
return pv.addConstraint(tagValue)
},
tagTokenObjConstraint: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenObjConstraint)
}
return pv.ObjectValidator.addConstraint(tagValue)
},
tagTokenWhen: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
return addConditions(&pv.WhenConditions, tagValue, true)
},
tagTokenUnwanted: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
return addConditions(&pv.UnwantedConditions, tagValue, true)
},
tagTokenRequiredWith: tagOpRequiredWith,
tagTokenRequiredWithAlt: tagOpRequiredWith,
tagTokenUnwantedWith: tagOpUnwantedWith,
tagTokenUnwantedWithAlt: tagOpUnwantedWith,
tagTokenRequiredWithMsg: tagOpRequiredWithMsg,
tagTokenRequiredWithAltMsg: tagOpRequiredWithMsg,
tagTokenUnwantedWithMsg: tagOpUnwantedWithMsg,
tagTokenUnwantedWithAltMsg: tagOpUnwantedWithMsg,
tagTokenObjIgnoreUnknownProperties: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenObjIgnoreUnknownProperties)
}
pv.ObjectValidator.IgnoreUnknownProperties = true
return nil
},
tagTokenObjUnknownProperties: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenObjUnknownProperties)
}
b, err := strconv.ParseBool(tagValue)
if err != nil {
return fmt.Errorf(msgUnknownTagValue, tagTokenObjUnknownProperties, "boolean", tagValue)
}
pv.ObjectValidator.IgnoreUnknownProperties = b
return nil
},
tagTokenObjOrdered: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenObjOrdered)
}
pv.ObjectValidator.OrderedPropertyChecks = true
return nil
},
tagTokenObjWhen: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenObjWhen)
}
if isBracedStr(tagValue, true) {
if tokens, err := parseCommas(tagValue[1 : len(tagValue)-1]); err == nil {
for _, token := range tokens {
if unq, ok := isQuotedStr(token); ok {
pv.ObjectValidator.WhenConditions = append(pv.ObjectValidator.WhenConditions, unq)
} else {
pv.ObjectValidator.WhenConditions = append(pv.ObjectValidator.WhenConditions, token)
}
}
} else {
return err
}
} else if unq, ok := isQuotedStr(tagValue); ok {
pv.ObjectValidator.WhenConditions = append(pv.ObjectValidator.WhenConditions, unq)
} else {
pv.ObjectValidator.WhenConditions = append(pv.ObjectValidator.WhenConditions, tagValue)
}
return nil
},
tagTokenObjNo: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
pv.ObjectValidator = nil
return nil
},
tagTokenArrAllowNullItems: func(pv *PropertyValidator, hasColon bool, tagValue string) error {
if pv.ObjectValidator == nil {
return fmt.Errorf(msgPropertyNotObject, tagTokenArrAllowNullItems)
}
pv.ObjectValidator.AllowNullItems = true
return nil
},
}
func isQuotedStr(str string) (string, bool) {
if strings.HasPrefix(str, `"`) && strings.HasSuffix(str, `"`) {
return strings.ReplaceAll(str[1:len(str)-1], `""`, `"`), true
} else if strings.HasPrefix(str, `'`) && strings.HasSuffix(str, `'`) {
return strings.ReplaceAll(str[1:len(str)-1], `''`, `'`), true
}
return "", false
}
func isBracedStr(str string, allowCurly bool) bool {
return (strings.HasPrefix(str, "[") && strings.HasSuffix(str, "]")) ||
(allowCurly && strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}"))
}