form.go 1.1 KB

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