binding.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "net/http"
  6. // Content-Type MIME of the most common data formats.
  7. const (
  8. MIMEJSON = "application/json"
  9. MIMEHTML = "text/html"
  10. MIMEXML = "application/xml"
  11. MIMEXML2 = "text/xml"
  12. MIMEPlain = "text/plain"
  13. MIMEPOSTForm = "application/x-www-form-urlencoded"
  14. MIMEMultipartPOSTForm = "multipart/form-data"
  15. MIMEPROTOBUF = "application/x-protobuf"
  16. MIMEMSGPACK = "application/x-msgpack"
  17. MIMEMSGPACK2 = "application/msgpack"
  18. MIMEYAML = "application/x-yaml"
  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. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  34. // but it read the Params.
  35. type BindingUri interface {
  36. Name() string
  37. BindUri(map[string][]string, interface{}) error
  38. }
  39. // StructValidator is the minimal interface which needs to be implemented in
  40. // order for it to be used as the validator engine for ensuring the correctness
  41. // of the request. Gin provides a default implementation for this using
  42. // https://github.com/go-playground/validator/tree/v8.18.2.
  43. type StructValidator interface {
  44. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  45. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  46. // If the received type is a struct or pointer to a struct, the validation should be performed.
  47. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  48. // Otherwise nil must be returned.
  49. ValidateStruct(interface{}) error
  50. // Engine returns the underlying validator engine which powers the
  51. // StructValidator implementation.
  52. Engine() interface{}
  53. }
  54. // Validator is the default validator which implements the StructValidator
  55. // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
  56. // under the hood.
  57. //var Validator StructValidator = &defaultValidator{}
  58. // These implement the Binding interface and can be used to bind the data
  59. // present in the request to struct instances.
  60. var (
  61. JSON = jsonBinding{}
  62. XML = xmlBinding{}
  63. Form = formBinding{}
  64. Query = queryBinding{}
  65. FormPost = formPostBinding{}
  66. FormMultipart = formMultipartBinding{}
  67. ProtoBuf = protobufBinding{}
  68. MsgPack = msgpackBinding{}
  69. YAML = yamlBinding{}
  70. Uri = uriBinding{}
  71. )
  72. // Default returns the appropriate Binding instance based on the HTTP method
  73. // and the content type.
  74. func Default(method, contentType string) Binding {
  75. if method == "GET" {
  76. return Form
  77. }
  78. switch contentType {
  79. case MIMEJSON:
  80. return JSON
  81. case MIMEXML, MIMEXML2:
  82. return XML
  83. case MIMEPROTOBUF:
  84. return ProtoBuf
  85. case MIMEMSGPACK, MIMEMSGPACK2:
  86. return MsgPack
  87. case MIMEYAML:
  88. return YAML
  89. case MIMEMultipartPOSTForm:
  90. return FormMultipart
  91. default: // case MIMEPOSTForm:
  92. return Form
  93. }
  94. }
  95. func validate(obj interface{}) error {
  96. return nil
  97. /*
  98. if Validator == nil {
  99. return nil
  100. }
  101. return Validator.ValidateStruct(obj)
  102. // */
  103. }
  104. // Bind checks the Content-Type to select a binding engine automatically,
  105. // Depending the "Content-Type" header different bindings are used:
  106. // "application/json" --> JSON binding
  107. // "application/xml" --> XML binding
  108. // otherwise --> returns an error.
  109. // It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
  110. // It decodes the json payload into the struct specified as a pointer.
  111. // It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
  112. func Bind(req *http.Request, obj interface{}) error {
  113. b := Default(req.Method, ContentType(req))
  114. return MustBindWith(req, obj, b)
  115. }
  116. // BindQuery query to struct
  117. func BindQuery(req *http.Request, obj interface{}) error {
  118. return MustBindWith(req, obj, Form)
  119. }
  120. // MustBindWith binds the passed struct pointer using the specified binding engine.
  121. // It will abort the request with HTTP 400 if any error occurs.
  122. // See the binding package.
  123. func MustBindWith(req *http.Request, obj interface{}, b Binding) (err error) {
  124. return b.Bind(req, obj)
  125. }
  126. // ContentType returns the Content-Type header of the request.
  127. func ContentType(req *http.Request) string {
  128. return filterFlags(req.Header.Get("Content-Type"))
  129. }
  130. func filterFlags(content string) string {
  131. for i, char := range content {
  132. if char == ' ' || char == ';' {
  133. return content[:i]
  134. }
  135. }
  136. return content
  137. }