You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sonic/handler/binding/form_binding.go

63 lines
1.3 KiB
Go

2 years ago
package binding
import (
2 years ago
"errors"
2 years ago
"net/http"
"github.com/gin-gonic/gin/binding"
)
const defaultMemory = 32 << 20
// CustomFormBinding If the type implements the UnmarshalJSON interface, use JSON to bind
// For the purpose of support enum string to turn the enum type binding
2 years ago
var (
CustomFormBinding = customFormBinding{}
CustomFormPostBinding = customFormPostBinding{}
)
2 years ago
type (
customFormBinding struct{}
customFormPostBinding struct{}
)
func (customFormBinding) Name() string {
return "form"
}
func (customFormBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseForm(); err != nil {
return err
}
if err := req.ParseMultipartForm(defaultMemory); err != nil {
2 years ago
if !errors.Is(err, http.ErrNotMultipart) {
2 years ago
return err
}
}
if err := mapForm(obj, req.Form); err != nil {
return err
}
return validate(obj)
}
func (customFormPostBinding) Name() string {
return "form-urlencoded"
}
func (customFormPostBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseForm(); err != nil {
return err
}
if err := mapForm(obj, req.PostForm); err != nil {
return err
}
return validate(obj)
}
func validate(obj interface{}) error {
if binding.Validator == nil {
return nil
}
return binding.Validator.ValidateStruct(obj)
}