github.com/songzhibin97/gkit@v1.2.13/tools/bind/form.go (about)

     1  package bind
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  const defaultMemory = 32 << 20
     8  
     9  type (
    10  	formBinding          struct{}
    11  	formPostBinding      struct{}
    12  	formMultipartBinding struct{}
    13  )
    14  
    15  func (formBinding) Name() string {
    16  	return "form"
    17  }
    18  
    19  func (formBinding) Bind(req *http.Request, obj interface{}) error {
    20  	if err := req.ParseForm(); err != nil {
    21  		return err
    22  	}
    23  	if err := req.ParseMultipartForm(defaultMemory); err != nil {
    24  		if err != http.ErrNotMultipart {
    25  			return err
    26  		}
    27  	}
    28  	return mapForm(obj, req.Form)
    29  }
    30  
    31  func (formPostBinding) Name() string {
    32  	return "form-urlencoded"
    33  }
    34  
    35  func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
    36  	if err := req.ParseForm(); err != nil {
    37  		return err
    38  	}
    39  	return mapForm(obj, req.PostForm)
    40  }
    41  
    42  func (formMultipartBinding) Name() string {
    43  	return "multipart/form-data"
    44  }
    45  
    46  func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
    47  	if err := req.ParseMultipartForm(defaultMemory); err != nil {
    48  		return err
    49  	}
    50  	return mappingByPtr(obj, (*multipartRequest)(req), "form")
    51  }