Added support multipart/form-data #109

This commit is contained in:
Aleksandr Didenko 2014-09-17 13:28:58 +04:00
parent 0808f8a824
commit 85b8fd742b
3 changed files with 29 additions and 10 deletions

View File

@ -27,12 +27,18 @@ type (
// // form binding
formBinding struct{}
// multipart form binding
multipartFormBinding struct{}
)
const MAX_MEMORY = 1 * 1024 * 1024
var (
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{} // todo
JSON = jsonBinding{}
XML = xmlBinding{}
Form = formBinding{} // todo
MultipartForm = multipartFormBinding{}
)
func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
@ -63,6 +69,16 @@ func (_ formBinding) Bind(req *http.Request, obj interface{}) error {
return Validate(obj)
}
func (_ multipartFormBinding) Bind(req *http.Request, obj interface{}) error {
if err := req.ParseMultipartForm(MAX_MEMORY); err != nil {
return err
}
if err := mapForm(obj, req.Form); err != nil {
return err
}
return Validate(obj)
}
func mapForm(ptr interface{}, form map[string][]string) error {
typ := reflect.TypeOf(ptr).Elem()
formStruct := reflect.ValueOf(ptr).Elem()

View File

@ -203,6 +203,8 @@ func (c *Context) Bind(obj interface{}) bool {
switch {
case c.Request.Method == "GET" || ctype == MIMEPOSTForm:
b = binding.Form
case ctype == MIMEMultipartPOSTForm:
b = binding.MultipartForm
case ctype == MIMEJSON:
b = binding.JSON
case ctype == MIMEXML || ctype == MIMEXML2:

15
gin.go
View File

@ -16,13 +16,14 @@ import (
)
const (
AbortIndex = math.MaxInt8 / 2
MIMEJSON = "application/json"
MIMEHTML = "text/html"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
AbortIndex = math.MaxInt8 / 2
MIMEJSON = "application/json"
MIMEHTML = "text/html"
MIMEXML = "application/xml"
MIMEXML2 = "text/xml"
MIMEPlain = "text/plain"
MIMEPOSTForm = "application/x-www-form-urlencoded"
MIMEMultipartPOSTForm = "multipart/form-data"
)
type (