@@ -36,7 +36,15 @@ func TagNameResolver(tagName string) TagResolver {
36
36
type PathParamFunc func (r * http.Request , key string ) string
37
37
38
38
// FormParamFunc is a function that returns value of specified form parameter.
39
- type FormParamFunc func (r * http.Request , key string ) string
39
+ type FormParamFunc func (r * http.Request , key string ) []string
40
+
41
+ func DefaultFormParamFunc (r * http.Request , key string ) []string {
42
+ // ParseForm is called in Parser.Parse, so we can safely assume that r.Form is already populated.
43
+ if values , ok := r .PostForm [key ]; ok && len (values ) > 0 {
44
+ return values
45
+ }
46
+ return nil
47
+ }
40
48
41
49
// Parser can Parse query and path parameters from http.Request into a struct.
42
50
// Fields struct have to be tagged such that either QueryParamTagResolver or PathParamTagResolver returns
@@ -56,7 +64,7 @@ func DefaultParser() Parser {
56
64
return Parser {
57
65
ParamTagResolver : TagNameResolver (defaultTagName ),
58
66
PathParamFunc : nil , // keep nil, as there is no sensible default of how to get value of path parameter
59
- FormParamFunc : nil , // keep nil, as there is no sensible default of how to get value of form parameter
67
+ FormParamFunc : DefaultFormParamFunc ,
60
68
}
61
69
}
62
70
@@ -155,30 +163,23 @@ func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath [
155
163
pathParamName , okPath := p .resolvePath (tag )
156
164
formParamName , okForm := p .resolveForm (tag )
157
165
queryParamName , okQuery := p .resolveQuery (tag )
166
+
167
+ newPath := append (append ([]int {}, currentNestingIndexPath ... ), i )
158
168
if okPath {
159
- newPath := make ([]int , 0 , len (currentNestingIndexPath )+ 1 )
160
- newPath = append (newPath , currentNestingIndexPath ... )
161
- newPath = append (newPath , i )
162
169
paths = append (paths , taggedFieldIndexPath {
163
170
paramType : paramTypePath ,
164
171
paramName : pathParamName ,
165
172
indexPath : newPath ,
166
173
})
167
174
}
168
175
if okForm {
169
- newPath := make ([]int , 0 , len (currentNestingIndexPath )+ 1 )
170
- newPath = append (newPath , currentNestingIndexPath ... )
171
- newPath = append (newPath , i )
172
176
paths = append (paths , taggedFieldIndexPath {
173
177
paramType : paramTypeForm ,
174
178
paramName : formParamName ,
175
179
indexPath : newPath ,
176
180
})
177
181
}
178
182
if okQuery {
179
- newPath := make ([]int , 0 , len (currentNestingIndexPath )+ 1 )
180
- newPath = append (newPath , currentNestingIndexPath ... )
181
- newPath = append (newPath , i )
182
183
paths = append (paths , taggedFieldIndexPath {
183
184
paramType : paramTypeQuery ,
184
185
paramName : queryParamName ,
@@ -254,8 +255,9 @@ func (p Parser) parseFormParam(r *http.Request, paramName string, v reflect.Valu
254
255
if err := r .ParseForm (); err != nil {
255
256
return fmt .Errorf ("parsing form data: %w" , err )
256
257
}
257
- if values , ok := r .Form [paramName ]; ok && len (values ) > 0 {
258
- err := unmarshalValueOrSlice (values , v )
258
+ paramValues := p .FormParamFunc (r , paramName )
259
+ if len (paramValues ) > 0 {
260
+ err := unmarshalValueOrSlice (paramValues , v )
259
261
if err != nil {
260
262
return fmt .Errorf ("unmarshaling form parameter %s: %w" , paramName , err )
261
263
}
0 commit comments