form.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "net/http"
  7. )
  8. const defaultMemory = 32 * 1024 * 1024
  9. type formBinding struct{}
  10. type formPostBinding struct{}
  11. type formMultipartBinding struct{}
  12. func (formBinding) Name() string {
  13. return "form"
  14. }
  15. func (formBinding) Bind(req *http.Request, obj interface{}) error {
  16. if err := req.ParseForm(); err != nil {
  17. return err
  18. }
  19. req.ParseMultipartForm(defaultMemory)
  20. if err := mapForm(obj, req.Form); err != nil {
  21. return err
  22. }
  23. return validate(obj)
  24. }
  25. func (formPostBinding) Name() string {
  26. return "form-urlencoded"
  27. }
  28. func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
  29. if err := req.ParseForm(); err != nil {
  30. return err
  31. }
  32. if err := mapForm(obj, req.PostForm); err != nil {
  33. return err
  34. }
  35. return validate(obj)
  36. }
  37. func (formMultipartBinding) Name() string {
  38. return "multipart/form-data"
  39. }
  40. func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
  41. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  42. return err
  43. }
  44. if err := mapForm(obj, req.MultipartForm.Value); err != nil {
  45. return err
  46. }
  47. return validate(obj)
  48. }