binding.go 3.0 KB

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