binding.go 3.8 KB

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