forked from go-playground/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_decoder.go
174 lines (139 loc) · 4.36 KB
/
form_decoder.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
package form
import (
"bytes"
"net/url"
"reflect"
"strings"
"sync"
)
// DecodeCustomTypeFunc allows for registering/overriding types to be parsed.
type DecodeCustomTypeFunc func([]string) (interface{}, error)
// DecodeErrors is a map of errors encountered during form decoding
type DecodeErrors map[string]error
func (d DecodeErrors) Error() string {
buff := bytes.NewBufferString(blank)
for k, err := range d {
buff.WriteString(fieldNS)
buff.WriteString(k)
buff.WriteString(errorText)
buff.WriteString(err.Error())
buff.WriteString("\n")
}
return strings.TrimSpace(buff.String())
}
// An InvalidDecoderError describes an invalid argument passed to Decode.
// (The argument passed to Decode must be a non-nil pointer.)
type InvalidDecoderError struct {
Type reflect.Type
}
func (e *InvalidDecoderError) Error() string {
if e.Type == nil {
return "form: Decode(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "form: Decode(non-pointer " + e.Type.String() + ")"
}
return "form: Decode(nil " + e.Type.String() + ")"
}
type key struct {
ivalue int
value string
searchValue string
}
type recursiveData struct {
alias string
sliceLen int
keys []key
}
type dataMap []*recursiveData
// Decoder is the main decode instance
type Decoder struct {
tagName string
mode Mode
structCache *structCacheMap
customTypeFuncs map[reflect.Type]DecodeCustomTypeFunc
maxArraySize int
dataPool *sync.Pool
}
// NewDecoder creates a new decoder instance with sane defaults
func NewDecoder() *Decoder {
d := &Decoder{
tagName: "form",
mode: ModeImplicit,
structCache: newStructCacheMap(),
maxArraySize: 10000,
}
d.dataPool = &sync.Pool{New: func() interface{} {
return &decoder{
d: d,
namespace: make([]byte, 0, 64),
}
}}
return d
}
// SetTagName sets the given tag name to be used by the decoder.
// Default is "form"
func (d *Decoder) SetTagName(tagName string) {
d.tagName = tagName
}
// SetMode sets the mode the decoder should run
// Default is ModeImplicit
func (d *Decoder) SetMode(mode Mode) {
d.mode = mode
}
// SetMaxArraySize sets maximum array size that can be created.
// This limit is for the array indexing this library supports to
// avoid potential DOS or man-in-the-middle attacks using an unusually
// high number.
// DEFAULT: 10000
func (d *Decoder) SetMaxArraySize(size uint) {
d.maxArraySize = int(size)
}
// RegisterTagNameFunc registers a custom tag name parser function
// NOTE: This method is not thread-safe it is intended that these all be registered prior to any parsing
//
// ADDITIONAL: once a custom function has been registered the default, or custom set, tag name is ignored
// and relies 100% on the function for the name data. The return value WILL BE CACHED and so return value
// must be consistent.
func (d *Decoder) RegisterTagNameFunc(fn TagNameFunc) {
d.structCache.tagFn = fn
}
// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types.
// NOTE: This method is not thread-safe it is intended that these all be registered prior to any parsing
//
// ADDITIONAL: if a struct type is registered, the function will only be called if a url.Value exists for
// the struct and not just the struct fields eg. url.Values{"User":"Name%3Djoeybloggs"} will call the
// custom type function with `User` as the type, however url.Values{"User.Name":"joeybloggs"} will not.
func (d *Decoder) RegisterCustomTypeFunc(fn DecodeCustomTypeFunc, types ...interface{}) {
if d.customTypeFuncs == nil {
d.customTypeFuncs = map[reflect.Type]DecodeCustomTypeFunc{}
}
for _, t := range types {
d.customTypeFuncs[reflect.TypeOf(t)] = fn
}
}
// Decode parses the given values and sets the corresponding struct and/or type values
//
// Decode returns an InvalidDecoderError if interface passed is invalid.
func (d *Decoder) Decode(v interface{}, values url.Values) (err error) {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr || val.IsNil() {
return &InvalidDecoderError{reflect.TypeOf(v)}
}
dec := d.dataPool.Get().(*decoder)
dec.values = values
dec.dm = dec.dm[0:0]
val = val.Elem()
typ := val.Type()
if val.Kind() == reflect.Struct && typ != timeType {
dec.traverseStruct(val, typ, dec.namespace[0:0])
} else {
dec.setFieldByType(val, dec.namespace[0:0], 0)
}
if len(dec.errs) > 0 {
err = dec.errs
dec.errs = nil
}
d.dataPool.Put(dec)
return
}