Parcourir la source

Merge branch 'alexanderdidenko-develop' into develop

Javier Provecho Fernandez il y a 10 ans
Parent
commit
d0bf5cdb83
4 fichiers modifiés avec 71 ajouts et 11 suppressions
  1. 41 0
      README.md
  2. 20 4
      binding/binding.go
  3. 2 0
      context.go
  4. 8 7
      gin.go

+ 41 - 0
README.md

@@ -172,6 +172,47 @@ func main() {
 }
 ```
 
+###Multipart Form
+```go
+package main
+
+import (
+	"github.com/gin-gonic/gin"
+	"github.com/gin-gonic/gin/binding"
+)
+
+type LoginForm struct {
+	User     string `form:"user" binding:"required"`
+	Password string `form:"password" binding:"required"`
+}
+
+func main() {
+
+	r := gin.Default()
+
+	r.POST("/login", func(c *gin.Context) {
+
+		var form LoginForm
+		c.BindWith(&form, binding.MultipartForm)
+
+		if form.User == "user" && form.Password == "password" {
+			c.JSON(200, gin.H{"status": "you are logged in"})
+		} else {
+			c.JSON(401, gin.H{"status": "unauthorized"})
+		}
+
+	})
+
+	r.Run(":8080")
+
+}
+```
+
+Test it with:
+```bash
+$ curl -v --form user=user --form password=password http://localhost:8080/login
+```
+
 #### Grouping routes
 ```go
 func main() {

+ 20 - 4
binding/binding.go

@@ -25,14 +25,20 @@ type (
 	// XML binding
 	xmlBinding struct{}
 
-	// // form binding
+	// 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()

+ 2 - 0
context.go

@@ -295,6 +295,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:

+ 8 - 7
gin.go

@@ -14,13 +14,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 (