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 (
  6. "net/http"
  7. //validator "gopkg.in/go-playground/validator.v8"
  8. //validator "gopkg.in/go-playground/validator.v8"
  9. )
  10. // Content-Type MIME of the most common data formats.
  11. const (
  12. MIMEJSON = "application/json"
  13. MIMEHTML = "text/html"
  14. MIMEXML = "application/xml"
  15. MIMEXML2 = "text/xml"
  16. MIMEPlain = "text/plain"
  17. MIMEPOSTForm = "application/x-www-form-urlencoded"
  18. MIMEMultipartPOSTForm = "multipart/form-data"
  19. MIMEPROTOBUF = "application/x-protobuf"
  20. MIMEMSGPACK = "application/x-msgpack"
  21. MIMEMSGPACK2 = "application/msgpack"
  22. MIMEYAML = "application/x-yaml"
  23. )
  24. // Binding describes the interface which needs to be implemented for binding the
  25. // data present in the request such as JSON request body, query parameters or
  26. // the form POST.
  27. type Binding interface {
  28. Name() string
  29. Bind(*http.Request, interface{}) error
  30. }
  31. // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
  32. // but it reads the body from supplied bytes instead of req.Body.
  33. type BindingBody interface {
  34. Binding
  35. BindBody([]byte, interface{}) error
  36. }
  37. // BindingURI adds BindUri method to Binding. BindUri is similar with Bind,
  38. // but it read the Params.
  39. type BindingURI interface {
  40. Name() string
  41. BindURI(map[string][]string, interface{}) error
  42. }
  43. /*
  44. // StructValidator is the minimal interface which needs to be implemented in
  45. // order for it to be used as the validator engine for ensuring the correctness
  46. // of the request. Gin provides a default implementation for this using
  47. // https://github.com/go-playground/validator/tree/v8.18.2.
  48. type StructValidator interface {
  49. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  50. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  51. // If the received type is a struct or pointer to a struct, the validation should be performed.
  52. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  53. // Otherwise nil must be returned.
  54. ValidateStruct(interface{}) error
  55. // Engine returns the underlying validator engine which powers the
  56. // StructValidator implementation.
  57. Engine() interface{}
  58. }
  59. // Validator is the default validator which implements the StructValidator
  60. // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
  61. // under the hood.
  62. var Validator StructValidator = &defaultValidator{}
  63. // */
  64. // These implement the Binding interface and can be used to bind the data
  65. // present in the request to struct instances.
  66. var (
  67. JSON = jsonBinding{}
  68. XML = xmlBinding{}
  69. Form = formBinding{}
  70. Query = queryBinding{}
  71. FormPost = formPostBinding{}
  72. FormMultipart = formMultipartBinding{}
  73. ProtoBuf = protobufBinding{}
  74. MsgPack = msgpackBinding{}
  75. YAML = yamlBinding{}
  76. URI = uriBinding{}
  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 == "GET" {
  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. default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
  96. return Form
  97. }
  98. }
  99. func validate(obj interface{}) error {
  100. return nil
  101. /*
  102. if Validator == nil {
  103. return nil
  104. }
  105. return Validator.ValidateStruct(obj)
  106. // */
  107. }
  108. // Bind checks the Content-Type to select a binding engine automatically,
  109. // Depending the "Content-Type" header different bindings are used:
  110. // "application/json" --> JSON binding
  111. // "application/xml" --> XML binding
  112. // otherwise --> returns an error.
  113. // It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
  114. // It decodes the json payload into the struct specified as a pointer.
  115. // It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
  116. func Bind(req *http.Request, obj interface{}) error {
  117. b := Default(req.Method, ContentType(req))
  118. return MustBindWith(req, obj, b)
  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. }