binding_nomsgpack.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2020 Gin Core Team. 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. 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. YAML = yamlBinding{}
  69. Uri = uriBinding{}
  70. Header = headerBinding{}
  71. )
  72. // Default returns the appropriate Binding instance based on the HTTP method
  73. // and the content type.
  74. func Default(method, contentType string) Binding {
  75. if method == "GET" {
  76. return Form
  77. }
  78. switch contentType {
  79. case MIMEJSON:
  80. return JSON
  81. case MIMEXML, MIMEXML2:
  82. return XML
  83. case MIMEPROTOBUF:
  84. return ProtoBuf
  85. case MIMEYAML:
  86. return YAML
  87. case MIMEMultipartPOSTForm:
  88. return FormMultipart
  89. default: // case MIMEPOSTForm:
  90. return Form
  91. }
  92. }
  93. func validate(obj interface{}) error {
  94. if Validator == nil {
  95. return nil
  96. }
  97. return Validator.ValidateStruct(obj)
  98. }