Skip to content

Commit dd4f66b

Browse files
committed
chore: use r.PostFormValue as the default form param func
Signed-off-by: Marek Cermak <[email protected]>
1 parent ac7a3a3 commit dd4f66b

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

http/param/param.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ 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
4040

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
41+
func DefaultFormParamFunc(r *http.Request, key string) string {
42+
return r.PostFormValue(key)
4743
}
4844

4945
// Parser can Parse query and path parameters from http.Request into a struct.
@@ -255,9 +251,9 @@ func (p Parser) parseFormParam(r *http.Request, paramName string, v reflect.Valu
255251
if err := r.ParseForm(); err != nil {
256252
return fmt.Errorf("parsing form data: %w", err)
257253
}
258-
paramValues := p.FormParamFunc(r, paramName)
259-
if len(paramValues) > 0 {
260-
err := unmarshalValueOrSlice(paramValues, v)
254+
paramValue := p.FormParamFunc(r, paramName)
255+
if paramValue != "" {
256+
err := unmarshalValue(paramValue, v)
261257
if err != nil {
262258
return fmt.Errorf("unmarshaling form parameter %s: %w", paramName, err)
263259
}

http/param/param_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,7 @@ type structWithFormParams struct {
460460

461461
func TestParser_Parse_FormParam(t *testing.T) {
462462
r := chi.NewRouter()
463-
p := DefaultParser().WithFormParamFunc(func(r *http.Request, key string) string {
464-
return r.FormValue(key)
465-
})
463+
p := DefaultParser()
466464
result := structWithFormParams{
467465
Nothing: "should be replaced",
468466
}

0 commit comments

Comments
 (0)