-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsanitize.go
242 lines (224 loc) · 7.37 KB
/
sanitize.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
// Package sanitize provides an easy way to clean fields in structs: trimming, applying maximum
// string lengths, minimum numeric values, default values, and so on
package sanitize
import (
"fmt"
"reflect"
)
// DefaultTagName instance is the name of the tag that must be present on the string
// fields of the structs to be sanitized. Defaults to "san".
const DefaultTagName = "san"
type SanitizerFunc func(s Sanitizer, structValue reflect.Value, idx int) error
// Sanitizer instance
type Sanitizer struct {
tagName string
dateInput []string
dateKeepFormat bool
dateOutput string
sanitizersByName map[string]SanitizerFunc
}
// New sanitizer instance
func New(options ...Option) (*Sanitizer, error) {
s := &Sanitizer{
tagName: DefaultTagName,
}
for _, o := range options {
switch o.id() {
case optionTagNameID:
v := o.value().(string)
if len(v) < 1 || len(v) > 10 {
return nil, fmt.Errorf("tag name %q must be between 1 and 10 characters", v)
}
s.tagName = v
case optionDateFormatID:
v := o.value().(OptionDateFormat)
s.dateInput = v.Input
s.dateKeepFormat = v.KeepFormat
s.dateOutput = v.Output
case optionSanitizerFuncID:
if s.sanitizersByName == nil {
s.sanitizersByName = make(map[string]SanitizerFunc)
}
v := o.(OptionSanitizerFunc)
if _, ok := s.sanitizersByName[v.Name]; ok {
return nil, fmt.Errorf("sanitizer already registered with name %q", o.id())
}
s.sanitizersByName[v.Name] = o.value().(SanitizerFunc)
default:
return nil, fmt.Errorf("option %q is not valid", o.id())
}
}
return s, nil
}
// Sanitize performs sanitization on all fields of any struct, so long
// as the sanitization tag ("san" by default) has been defined on the string
// fields of the struct. The argument s must be the address of a struct to
// mutate.
//
// Will recursively check all struct, *struct, string, *string, int64, *int64,
// float64, *float64, bool, and *bool fields. Pointers are dereferenced and the
// data pointed to will be sanitized.
//
// Errors are returned as the struct's fields are processed, so the struct may
// not be in the same state as when the function began if an error is
// returned.
func (s *Sanitizer) Sanitize(o interface{}) error {
// Get both the value and the type of what the pointer points to. Value is
// used to mutate underlying data and Type is used to get the name of the
// field.
return s.sanitizeRec(reflect.ValueOf(o).Elem())
}
type fieldSanFn = func(s Sanitizer, structValue reflect.Value, idx int) error
var fieldSanFns = map[string]fieldSanFn{
"string": sanitizeStrField,
"[]string": sanitizeStrField,
"*[]string": sanitizeStrField,
"*string": sanitizeStrField,
"[]*string": sanitizeStrField,
"*[]*string": sanitizeStrField,
"int": sanitizeIntField,
"[]int": sanitizeIntField,
"*[]int": sanitizeIntField,
"*int": sanitizeIntField,
"[]*int": sanitizeIntField,
"*[]*int": sanitizeIntField,
"int8": sanitizeInt8Field,
"[]int8": sanitizeInt8Field,
"*[]int8": sanitizeInt8Field,
"*int8": sanitizeInt8Field,
"[]*int8": sanitizeInt8Field,
"*[]*int8": sanitizeInt8Field,
"int16": sanitizeInt16Field,
"[]int16": sanitizeInt16Field,
"*[]int16": sanitizeInt16Field,
"*int16": sanitizeInt16Field,
"[]*int16": sanitizeInt16Field,
"*[]*int16": sanitizeInt16Field,
"int32": sanitizeInt32Field,
"[]int32": sanitizeInt32Field,
"*[]int32": sanitizeInt32Field,
"*int32": sanitizeInt32Field,
"[]*int32": sanitizeInt32Field,
"*[]*int32": sanitizeInt32Field,
"int64": sanitizeInt64Field,
"[]int64": sanitizeInt64Field,
"*[]int64": sanitizeInt64Field,
"*int64": sanitizeInt64Field,
"[]*int64": sanitizeInt64Field,
"*[]*int64": sanitizeInt64Field,
"uint": sanitizeUintField,
"[]uint": sanitizeUintField,
"*[]uint": sanitizeUintField,
"*uint": sanitizeUintField,
"[]*uint": sanitizeUintField,
"*[]*uint": sanitizeUintField,
"uint8": sanitizeUint8Field,
"[]uint8": sanitizeUint8Field,
"*[]uint8": sanitizeUint8Field,
"*uint8": sanitizeUint8Field,
"[]*uint8": sanitizeUint8Field,
"*[]*uint8": sanitizeUint8Field,
"uint16": sanitizeUint16Field,
"[]uint16": sanitizeUint16Field,
"*[]uint16": sanitizeUint16Field,
"*uint16": sanitizeUint16Field,
"[]*uint16": sanitizeUint16Field,
"*[]*uint16": sanitizeUint16Field,
"uint32": sanitizeUint32Field,
"[]uint32": sanitizeUint32Field,
"*[]uint32": sanitizeUint32Field,
"*uint32": sanitizeUint32Field,
"[]*uint32": sanitizeUint32Field,
"*[]*uint32": sanitizeUint32Field,
"uint64": sanitizeUint64Field,
"[]uint64": sanitizeUint64Field,
"*[]uint64": sanitizeUint64Field,
"*uint64": sanitizeUint64Field,
"[]*uint64": sanitizeUint64Field,
"*[]*uint64": sanitizeUint64Field,
"float32": sanitizeFloat32Field,
"[]float32": sanitizeFloat32Field,
"*[]float32": sanitizeFloat32Field,
"*float32": sanitizeFloat32Field,
"[]*float32": sanitizeFloat32Field,
"*[]*float32": sanitizeFloat32Field,
"float64": sanitizeFloat64Field,
"[]float64": sanitizeFloat64Field,
"*[]float64": sanitizeFloat64Field,
"*float64": sanitizeFloat64Field,
"[]*float64": sanitizeFloat64Field,
"*[]*float64": sanitizeFloat64Field,
"bool": sanitizeBoolField,
"[]bool": sanitizeBoolField,
"*[]bool": sanitizeBoolField,
"*bool": sanitizeBoolField,
"[]*bool": sanitizeBoolField,
"*[]*bool": sanitizeBoolField,
}
// Called during recursion, since during recursion we need reflect.Value
// not interface{}.
func (s Sanitizer) sanitizeRec(v reflect.Value) error {
// Loop through fields of struct. If a struct is encountered, recurse. If a
// string is encountered, transform it. Else, skip.
for i := 0; i < v.Type().NumField(); i++ {
field := v.Field(i)
fkind := field.Kind()
// Prioritize custom sanitizers; tag's value can be re-resolved inside the sanitizer
tags := s.fieldTags(v.Type().Field(i).Tag)
for tag := range tags {
sanitizerFunc, ok := s.sanitizersByName[tag]
if !ok {
continue
}
if err := sanitizerFunc(s, v, i); err != nil {
return err
}
}
// If the field is a slice, sanitize it first
isPtrToSlice := fkind == reflect.Ptr && field.Elem().Kind() == reflect.Slice
isSlice := fkind == reflect.Slice
if isSlice || isPtrToSlice {
if err := sanitizeSliceField(s, v, i); err != nil {
return err
}
}
// Do we have a special sanitization function for this type? If so, use it
ftype := field.Type().String()
if sanFn, ok := fieldSanFns[ftype]; ok {
if err := sanFn(s, v, i); err != nil {
return err
}
}
// If the field is a struct, sanitize it recursively
isPtrToStruct := fkind == reflect.Ptr && field.Elem().Kind() == reflect.Struct
if fkind == reflect.Struct || isPtrToStruct {
if isPtrToStruct {
field = field.Elem()
}
if err := s.sanitizeRec(field); err != nil {
return err
}
continue
}
// If the field is a slice of structs, recurse through them
if isSlice || isPtrToSlice {
if isPtrToSlice {
field = field.Elem()
}
for i := 0; i < field.Len(); i++ {
f := field.Index(i)
if f.Kind() == reflect.Ptr {
f = f.Elem()
}
if f.Kind() != reflect.Struct {
continue
}
if err := s.sanitizeRec(f); err != nil {
return err
}
}
continue
}
}
return nil
}