|
@@ -25,14 +25,20 @@ type (
|
|
|
// XML binding
|
|
// XML binding
|
|
|
xmlBinding struct{}
|
|
xmlBinding struct{}
|
|
|
|
|
|
|
|
- // // form binding
|
|
|
|
|
|
|
+ // form binding
|
|
|
formBinding struct{}
|
|
formBinding struct{}
|
|
|
|
|
+
|
|
|
|
|
+ // multipart form binding
|
|
|
|
|
+ multipartFormBinding struct{}
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+const MAX_MEMORY = 1 * 1024 * 1024
|
|
|
|
|
+
|
|
|
var (
|
|
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 {
|
|
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)
|
|
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 {
|
|
func mapForm(ptr interface{}, form map[string][]string) error {
|
|
|
typ := reflect.TypeOf(ptr).Elem()
|
|
typ := reflect.TypeOf(ptr).Elem()
|
|
|
formStruct := reflect.ValueOf(ptr).Elem()
|
|
formStruct := reflect.ValueOf(ptr).Elem()
|