binding_nomsgpack.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. MIMETOML = "application/toml"
  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, any) error
  27. }
  28. // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
  29. // but it reads the body from supplied bytes instead of req.Body.
  30. type BindingBody interface {
  31. Binding
  32. BindBody([]byte, any) error
  33. }
  34. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  35. // but it reads the Params.
  36. type BindingUri interface {
  37. Name() string
  38. BindUri(map[string][]string, any) error
  39. }
  40. // StructValidator is the minimal interface which needs to be implemented in
  41. // order for it to be used as the validator engine for ensuring the correctness
  42. // of the request. Gin provides a default implementation for this using
  43. // https://github.com/go-playground/validator/tree/v10.6.1.
  44. type StructValidator interface {
  45. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  46. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  47. // If the received type is a struct or pointer to a struct, the validation should be performed.
  48. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  49. // Otherwise nil must be returned.
  50. ValidateStruct(any) error
  51. // Engine returns the underlying validator engine which powers the
  52. // StructValidator implementation.
  53. Engine() any
  54. }
  55. // Validator is the default validator which implements the StructValidator
  56. // interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
  57. // under the hood.
  58. var Validator StructValidator = &defaultValidator{}
  59. // These implement the Binding interface and can be used to bind the data
  60. // present in the request to struct instances.
  61. var (
  62. JSON = jsonBinding{}
  63. XML = xmlBinding{}
  64. Form = formBinding{}
  65. Query = queryBinding{}
  66. FormPost = formPostBinding{}
  67. FormMultipart = formMultipartBinding{}
  68. ProtoBuf = protobufBinding{}
  69. YAML = yamlBinding{}
  70. Uri = uriBinding{}
  71. Header = headerBinding{}
  72. TOML = tomlBinding{}
  73. )
  74. // Default returns the appropriate Binding instance based on the HTTP method
  75. // and the content type.
  76. func Default(method, contentType string) Binding {
  77. if method == "GET" {
  78. return Form
  79. }
  80. switch contentType {
  81. case MIMEJSON:
  82. return JSON
  83. case MIMEXML, MIMEXML2:
  84. return XML
  85. case MIMEPROTOBUF:
  86. return ProtoBuf
  87. case MIMEYAML:
  88. return YAML
  89. case MIMEMultipartPOSTForm:
  90. return FormMultipart
  91. case MIMETOML:
  92. return TOML
  93. default: // case MIMEPOSTForm:
  94. return Form
  95. }
  96. }
  97. func validate(obj any) error {
  98. if Validator == nil {
  99. return nil
  100. }
  101. return Validator.ValidateStruct(obj)
  102. }