binding.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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. MIMEMSGPACK = "application/x-msgpack"
  18. MIMEMSGPACK2 = "application/msgpack"
  19. MIMEYAML = "application/x-yaml"
  20. MIMETOML = "application/toml"
  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, any) 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, any) error
  34. }
  35. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  36. // but it reads the Params.
  37. type BindingUri interface {
  38. Name() string
  39. BindUri(map[string][]string, any) 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/v10.6.1.
  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(any) error
  53. // Engine returns the underlying validator engine which powers the
  54. // StructValidator implementation.
  55. Engine() any
  56. }
  57. // Validator is the default validator which implements the StructValidator
  58. // interface. It uses https://github.com/go-playground/validator/tree/v10.6.1
  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. TOML = tomlBinding{}
  76. )
  77. // Default returns the appropriate Binding instance based on the HTTP method
  78. // and the content type.
  79. func Default(method, contentType string) Binding {
  80. if method == http.MethodGet {
  81. return Form
  82. }
  83. switch contentType {
  84. case MIMEJSON:
  85. return JSON
  86. case MIMEXML, MIMEXML2:
  87. return XML
  88. case MIMEPROTOBUF:
  89. return ProtoBuf
  90. case MIMEMSGPACK, MIMEMSGPACK2:
  91. return MsgPack
  92. case MIMEYAML:
  93. return YAML
  94. case MIMETOML:
  95. return TOML
  96. case MIMEMultipartPOSTForm:
  97. return FormMultipart
  98. default: // case MIMEPOSTForm:
  99. return Form
  100. }
  101. }
  102. func validate(obj any) error {
  103. if Validator == nil {
  104. return nil
  105. }
  106. return Validator.ValidateStruct(obj)
  107. }