You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
6 years ago
|
package binding
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
const defaultMemory = 32 * 1024 * 1024
|
||
|
|
||
|
type formBinding struct{}
|
||
|
type formPostBinding struct{}
|
||
|
type formMultipartBinding struct{}
|
||
|
|
||
|
func (f formBinding) Name() string {
|
||
|
return "form"
|
||
|
}
|
||
|
|
||
|
func (f formBinding) Bind(req *http.Request, obj interface{}) error {
|
||
|
if err := req.ParseForm(); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
if err := mapForm(obj, req.Form); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return validate(obj)
|
||
|
}
|
||
|
|
||
|
func (f formPostBinding) Name() string {
|
||
|
return "form-urlencoded"
|
||
|
}
|
||
|
|
||
|
func (f formPostBinding) Bind(req *http.Request, obj interface{}) error {
|
||
|
if err := req.ParseForm(); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
if err := mapForm(obj, req.PostForm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return validate(obj)
|
||
|
}
|
||
|
|
||
|
func (f formMultipartBinding) Name() string {
|
||
|
return "multipart/form-data"
|
||
|
}
|
||
|
|
||
|
func (f formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
|
||
|
if err := req.ParseMultipartForm(defaultMemory); err != nil {
|
||
|
return errors.WithStack(err)
|
||
|
}
|
||
|
if err := mapForm(obj, req.MultipartForm.Value); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return validate(obj)
|
||
|
}
|