-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.go
211 lines (187 loc) · 6.11 KB
/
generator.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
package galidator
import (
"reflect"
"strings"
)
type (
// A struct to implement generator interface
generatorS struct {
// Custom validators
customValidators Validators
// Custom error messages
messages Messages
}
// An interface to generate a validator or ruleSet
generator interface {
// Overrides current validators(if there is one) with passed validators
//
// Call this method before calling `generator.Validator` method to have effect
CustomValidators(validators Validators) generator
// Overrides current messages(if there is one) with passed messages
//
// Call this method before calling `generator.Validator` method to have effect
CustomMessages(messages Messages) generator
// Generates a validator interface which can be used to validate struct or map by some rules.
//
// `input` can be a ruleSet or a struct instance.
//
// Please use CapitalCase for rules' keys (Important for getting data out of struct types)
Validator(input interface{}, messages ...Messages) Validator
// Generates a validator interface which can be used to validate struct or map or slice by passing an instance of them
validator(input interface{}) Validator
// Generates a ruleSet to validate passed information
//
// Variable name will be used in output of error keys
RuleSet(name ...string) ruleSet
// An alias for RuleSet function
R(name ...string) ruleSet
// Generates a complex validator to validate maps and structs
ComplexValidator(rules Rules, messages ...Messages) Validator
}
)
func (o *generatorS) CustomValidators(validators Validators) generator {
o.customValidators = validators
return o
}
func (o *generatorS) CustomMessages(messages Messages) generator {
o.messages = messages
return o
}
func (o *generatorS) Validator(rule interface{}, errorMessages ...Messages) Validator {
var messages Messages = o.messages
if len(errorMessages) != 0 {
for key, value := range errorMessages[0] {
messages[key] = value
}
}
switch rule := rule.(type) {
case ruleSet:
break
default:
for reflect.TypeOf(rule).Kind() == reflect.Ptr {
rule = reflect.ValueOf(rule).Elem().Interface()
}
}
var output Validator = nil
switch v := rule.(type) {
case ruleSet:
output = &validatorS{rule: v, rules: nil, messages: nil}
default:
if reflect.TypeOf(v).Kind() == reflect.Struct || reflect.TypeOf(v).Kind() == reflect.Slice {
output = o.validator(v)
} else {
panic("'rule' has to be a ruleSet or a struct instance")
}
}
deepPassMessages(output, &messages)
return output
}
func (o *generatorS) validator(input interface{}) Validator {
inputValue := reflect.ValueOf(input)
inputType := reflect.TypeOf(input)
r := o.RuleSet()
if inputType.Kind() == reflect.Struct {
rules := Rules{}
for i := 0; i < inputType.NumField(); i++ {
elementT := inputType.Field(i)
element := inputValue.Field(i)
for element.Type().Kind() == reflect.Ptr {
elementType := element.Type().Elem()
element = reflect.New(elementType).Elem()
elementT.Type = element.Type()
}
tags := []string{elementT.Tag.Get("g"), elementT.Tag.Get("galidator")}
r = o.RuleSet(elementT.Tag.Get("json"))
if elementT.Type.Kind() == reflect.Struct || elementT.Type.Kind() == reflect.Map {
validator := o.validator(element.Interface())
r.setDeepValidator(validator)
} else if elementT.Type.Kind() == reflect.Slice {
child := elementT.Type.Elem()
if child.Kind() != reflect.Slice && child.Kind() != reflect.Struct && child.Kind() != reflect.Map {
r.Children(o.R().Type(child))
} else {
validator := o.validator(reflect.Zero(elementT.Type.Elem()).Interface())
r.setChildrenValidator(validator)
}
}
// Add messages of rules
for _, fullTag := range tags {
filters := strings.Split(fullTag, ",")
for j := 0; j < len(filters); j++ {
tag := strings.SplitN(filters[j], "=", 2)
normalFuncName := applyRules(r, tag, o, true)
value := elementT.Tag.Get("_" + normalFuncName)
if value == "" {
value = elementT.Tag.Get(normalFuncName)
}
addSpecificMessage(r, normalFuncName, value)
}
}
// Support for binding tag that is used for Bind actions
if bindingTags := elementT.Tag.Get("binding"); bindingTags != "" {
splits := strings.Split(bindingTags, ",")
for j := 0; j < len(splits); j++ {
rule := strings.Split(splits[j], "=")
value := elementT.Tag.Get("_" + rule[0])
if value == "" {
value = elementT.Tag.Get(rule[0])
}
addSpecificMessage(r, rule[0], value)
}
}
rules[elementT.Name] = r
}
return &validatorS{rules: rules}
} else if inputType.Kind() == reflect.Slice {
child := inputType.Elem()
if child.Kind() != reflect.Slice && child.Kind() != reflect.Struct && child.Kind() != reflect.Map {
r.Children(o.R().Type(child))
} else {
validator := o.validator(reflect.Zero(child).Interface())
r.setChildrenValidator(validator)
}
return &validatorS{rule: r}
} else if inputType.Kind() == reflect.Map {
panic("do not use map in Validator creation based on struct elements tags")
} else {
r.Type(inputType)
return &validatorS{rule: r}
}
}
func (o *generatorS) RuleSet(name ...string) ruleSet {
var output = ""
if len(name) != 0 {
output = name[0]
}
ruleSet := &ruleSetS{name: output, validators: Validators{}, requires: requires{}, options: options{}, isOptional: true}
return ruleSet.setGeneratorCustomValidators(&o.customValidators)
}
func (o *generatorS) R(name ...string) ruleSet {
return o.RuleSet(name...)
}
func (o *generatorS) ComplexValidator(rules Rules, errorMessages ...Messages) Validator {
var messages Messages = o.messages
if len(errorMessages) != 0 {
for key, value := range errorMessages[0] {
messages[key] = value
}
}
output := &validatorS{rule: nil, rules: rules, messages: nil}
deepPassMessages(output, &messages)
return output
}
// Returns a new Generator
func NewGenerator() generator {
return &generatorS{
messages: Messages{},
customValidators: Validators{},
}
}
// An alias for `NewGenerator` funcion
func New() generator {
return NewGenerator()
}
// An alias for `NewGenerator` funcion
func G() generator {
return NewGenerator()
}