binding.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // +build !nomsgpack
  5. package binding
  6. import "net/http"
  7. // Content-Type MIME of the most common data formats.
  8. const (
  9. MIMEJSON = "application/json"
  10. MIMEHTML = "text/html"
  11. MIMEXML = "application/xml"
  12. MIMEXML2 = "text/xml"
  13. MIMEPlain = "text/plain"
  14. MIMEPOSTForm = "application/x-www-form-urlencoded"
  15. MIMEMultipartPOSTForm = "multipart/form-data"
  16. MIMEPROTOBUF = "application/x-protobuf"
  17. MIMEMSGPACK = "application/x-msgpack"
  18. MIMEMSGPACK2 = "application/msgpack"
  19. MIMEYAML = "application/x-yaml"
  20. )
  21. // Binding describes the interface which needs to be implemented for binding the
  22. // data present in the request such as JSON request body, query parameters or
  23. // the form POST.
  24. type Binding interface {
  25. Name() string
  26. Bind(*http.Request, interface{}) error
  27. }
  28. // These implement the Binding interface and can be used to bind the data
  29. // present in the request to struct instances.
  30. var (
  31. JSON = jsonBinding{}
  32. XML = xmlBinding{}
  33. Form = formBinding{}
  34. Query = queryBinding{}
  35. FormPost = formPostBinding{}
  36. FormMultipart = formMultipartBinding{}
  37. ProtoBuf = protobufBinding{}
  38. MsgPack = msgpackBinding{}
  39. YAML = yamlBinding{}
  40. URI = uriBinding{}
  41. Header = headerBinding{}
  42. )
  43. // Default returns the appropriate Binding instance based on the HTTP method
  44. // and the content type.
  45. func Default(method, contentType string) Binding {
  46. if method == http.MethodGet {
  47. return Form
  48. }
  49. switch contentType {
  50. case MIMEJSON:
  51. return JSON
  52. case MIMEXML, MIMEXML2:
  53. return XML
  54. case MIMEPROTOBUF:
  55. return ProtoBuf
  56. case MIMEMSGPACK, MIMEMSGPACK2:
  57. return MsgPack
  58. case MIMEYAML:
  59. return YAML
  60. case MIMEMultipartPOSTForm:
  61. return FormMultipart
  62. default: // case MIMEPOSTForm:
  63. return Form
  64. }
  65. }
  66. func validate(obj interface{}) error {
  67. return nil
  68. }