binding_nomsgpack.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. MIMEYAML = "application/x-yaml"
  18. MIMETOML = "application/toml"
  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, any) 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, any) error
  32. }
  33. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  34. // but it reads the Params.
  35. type BindingUri interface {
  36. Name() string
  37. BindUri(map[string][]string, any) 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/v10.6.1.
  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(any) error
  50. // Engine returns the underlying validator engine which powers the
  51. // StructValidator implementation.
  52. Engine() any
  53. }
  54. // Validator is the default validator which implements the StructValidator
  55. // interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
  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. TOML = tomlBinding{}
  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 MIMEYAML:
  87. return YAML
  88. case MIMEMultipartPOSTForm:
  89. return FormMultipart
  90. case MIMETOML:
  91. return TOML
  92. default: // case MIMEPOSTForm:
  93. return Form
  94. }
  95. }
  96. func validate(obj any) error {
  97. if Validator == nil {
  98. return nil
  99. }
  100. return Validator.ValidateStruct(obj)
  101. }