binding.go 3.9 KB

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