binding.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package binding
  5. import (
  6. "net/http"
  7. )
  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. )
  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. // StructValidator is the minimal interface which needs to be implemented in
  34. // order for it to be used as the validator engine for ensuring the correctness
  35. // of the reqest. Gin provides a default implementation for this using
  36. // https://github.com/go-playground/validator/tree/v8.18.2.
  37. type StructValidator interface {
  38. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  39. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  40. // If the received type is a struct or pointer to a struct, the validation should be performed.
  41. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  42. // Otherwise nil must be returned.
  43. ValidateStruct(interface{}) error
  44. // Engine returns the underlying validator engine which powers the
  45. // StructValidator implementation.
  46. Engine() interface{}
  47. }
  48. // Validator is the default validator which implements the StructValidator
  49. // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
  50. // under the hood.
  51. var Validator StructValidator = &defaultValidator{}
  52. // These implement the Binding interface and can be used to bind the data
  53. // present in the request to struct instances.
  54. var (
  55. JSON = jsonBinding{}
  56. XML = xmlBinding{}
  57. Form = formBinding{}
  58. Query = queryBinding{}
  59. FormPost = formPostBinding{}
  60. FormMultipart = formMultipartBinding{}
  61. ProtoBuf = protobufBinding{}
  62. MsgPack = msgpackBinding{}
  63. )
  64. // Default returns the appropriate Binding instance based on the HTTP method
  65. // and the content type.
  66. func Default(method, contentType string) Binding {
  67. if method == "GET" {
  68. return Form
  69. }
  70. switch contentType {
  71. case MIMEJSON:
  72. return JSON
  73. case MIMEXML, MIMEXML2:
  74. return XML
  75. case MIMEPROTOBUF:
  76. return ProtoBuf
  77. case MIMEMSGPACK, MIMEMSGPACK2:
  78. return MsgPack
  79. default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
  80. return Form
  81. }
  82. }
  83. func validate(obj interface{}) error {
  84. if Validator == nil {
  85. return nil
  86. }
  87. return Validator.ValidateStruct(obj)
  88. }