Skip to content

Commit ac7a3a3

Browse files
committed
chore: address review comments
- use FormParamFunc - use PostForm instead of Form - simplify newPath Signed-off-by: Marek Cermak <[email protected]>
1 parent 9958824 commit ac7a3a3

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

http/param/param.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ func TagNameResolver(tagName string) TagResolver {
3636
type PathParamFunc func(r *http.Request, key string) string
3737

3838
// 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+
}
4048

4149
// Parser can Parse query and path parameters from http.Request into a struct.
4250
// Fields struct have to be tagged such that either QueryParamTagResolver or PathParamTagResolver returns
@@ -56,7 +64,7 @@ func DefaultParser() Parser {
5664
return Parser{
5765
ParamTagResolver: TagNameResolver(defaultTagName),
5866
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,
6068
}
6169
}
6270

@@ -155,30 +163,23 @@ func (p Parser) findTaggedIndexPaths(typ reflect.Type, currentNestingIndexPath [
155163
pathParamName, okPath := p.resolvePath(tag)
156164
formParamName, okForm := p.resolveForm(tag)
157165
queryParamName, okQuery := p.resolveQuery(tag)
166+
167+
newPath := append(append([]int{}, currentNestingIndexPath...), i)
158168
if okPath {
159-
newPath := make([]int, 0, len(currentNestingIndexPath)+1)
160-
newPath = append(newPath, currentNestingIndexPath...)
161-
newPath = append(newPath, i)
162169
paths = append(paths, taggedFieldIndexPath{
163170
paramType: paramTypePath,
164171
paramName: pathParamName,
165172
indexPath: newPath,
166173
})
167174
}
168175
if okForm {
169-
newPath := make([]int, 0, len(currentNestingIndexPath)+1)
170-
newPath = append(newPath, currentNestingIndexPath...)
171-
newPath = append(newPath, i)
172176
paths = append(paths, taggedFieldIndexPath{
173177
paramType: paramTypeForm,
174178
paramName: formParamName,
175179
indexPath: newPath,
176180
})
177181
}
178182
if okQuery {
179-
newPath := make([]int, 0, len(currentNestingIndexPath)+1)
180-
newPath = append(newPath, currentNestingIndexPath...)
181-
newPath = append(newPath, i)
182183
paths = append(paths, taggedFieldIndexPath{
183184
paramType: paramTypeQuery,
184185
paramName: queryParamName,
@@ -254,8 +255,9 @@ func (p Parser) parseFormParam(r *http.Request, paramName string, v reflect.Valu
254255
if err := r.ParseForm(); err != nil {
255256
return fmt.Errorf("parsing form data: %w", err)
256257
}
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)
259261
if err != nil {
260262
return fmt.Errorf("unmarshaling form parameter %s: %w", paramName, err)
261263
}

0 commit comments

Comments
 (0)