binding.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // Content-Type MIME of the most common data formats.
  7. const (
  8. MIMEJSON = "application/json"
  9. MIMEHTML = "text/html"
  10. MIMEXML = "application/xml"
  11. MIMEXML2 = "text/xml"
  12. MIMEPlain = "text/plain"
  13. MIMEPOSTForm = "application/x-www-form-urlencoded"
  14. MIMEMultipartPOSTForm = "multipart/form-data"
  15. MIMEPROTOBUF = "application/x-protobuf"
  16. MIMEMSGPACK = "application/x-msgpack"
  17. MIMEMSGPACK2 = "application/msgpack"
  18. MIMEYAML = "application/x-yaml"
  19. )
  20. // Binding describes the interface which needs to be implemented for binding the
  21. // data present in the request such as JSON request body, query parameters or
  22. // the form POST.
  23. type Binding interface {
  24. Name() string
  25. Bind(*http.Request, interface{}) error
  26. }
  27. // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
  28. // but it reads the body from supplied bytes instead of req.Body.
  29. type BindingBody interface {
  30. Binding
  31. BindBody([]byte, interface{}) error
  32. }
  33. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  34. // but it read the Params.
  35. type BindingUri interface {
  36. Name() string
  37. BindUri(map[string][]string, interface{}) error
  38. }
  39. // StructValidator is the minimal interface which needs to be implemented in
  40. // order for it to be used as the validator engine for ensuring the correctness
  41. // of the request. Gin provides a default implementation for this using
  42. // https://github.com/go-playground/validator/tree/v8.18.2.
  43. type StructValidator interface {
  44. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  45. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  46. // If the received type is a struct or pointer to a struct, the validation should be performed.
  47. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  48. // Otherwise nil must be returned.
  49. ValidateStruct(interface{}) error
  50. // Engine returns the underlying validator engine which powers the
  51. // StructValidator implementation.
  52. Engine() interface{}
  53. }
  54. // Validator is the default validator which implements the StructValidator
  55. // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
  56. // under the hood.
  57. var Validator StructValidator = &defaultValidator{}
  58. // These implement the Binding interface and can be used to bind the data
  59. // present in the request to struct instances.
  60. var (
  61. JSON = jsonBinding{}
  62. XML = xmlBinding{}
  63. Form = formBinding{}
  64. Query = queryBinding{}
  65. FormPost = formPostBinding{}
  66. FormMultipart = formMultipartBinding{}
  67. ProtoBuf = protobufBinding{}
  68. MsgPack = msgpackBinding{}
  69. YAML = yamlBinding{}
  70. Uri = uriBinding{}
  71. Header = headerBinding{}
  72. )
  73. // Default returns the appropriate Binding instance based on the HTTP method
  74. // and the content type.
  75. func Default(method, contentType string) Binding {
  76. if method == "GET" {
  77. return Form
  78. }
  79. switch contentType {
  80. case MIMEJSON:
  81. return JSON
  82. case MIMEXML, MIMEXML2:
  83. return XML
  84. case MIMEPROTOBUF:
  85. return ProtoBuf
  86. case MIMEMSGPACK, MIMEMSGPACK2:
  87. return MsgPack
  88. case MIMEYAML:
  89. return YAML
  90. case MIMEMultipartPOSTForm:
  91. return FormMultipart
  92. default: // case MIMEPOSTForm:
  93. return Form
  94. }
  95. }
  96. func validate(obj interface{}) error {
  97. if Validator == nil {
  98. return nil
  99. }
  100. return Validator.ValidateStruct(obj)
  101. }