另客网go项目公用的代码库

form.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "net/http"
  6. const defaultMemory = 32 * 1024 * 1024
  7. type formBinding struct{}
  8. type formPostBinding struct{}
  9. type formMultipartBinding struct{}
  10. func (formBinding) Name() string {
  11. return "form"
  12. }
  13. func (formBinding) Bind(req *http.Request, obj interface{}) error {
  14. if err := req.ParseForm(); err != nil {
  15. return err
  16. }
  17. req.ParseMultipartForm(defaultMemory)
  18. if err := mapForm(obj, req.Form); err != nil {
  19. return err
  20. }
  21. return validate(obj)
  22. }
  23. func (formPostBinding) Name() string {
  24. return "form-urlencoded"
  25. }
  26. func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
  27. if err := req.ParseForm(); err != nil {
  28. return err
  29. }
  30. if err := mapForm(obj, req.PostForm); err != nil {
  31. return err
  32. }
  33. return validate(obj)
  34. }
  35. func (formMultipartBinding) Name() string {
  36. return "multipart/form-data"
  37. }
  38. func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
  39. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  40. return err
  41. }
  42. if err := mapForm(obj, req.MultipartForm.Value); err != nil {
  43. return err
  44. }
  45. return validate(obj)
  46. }