binding.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. const (
  11. MIMEJSON = "application/json"
  12. MIMEHTML = "text/html"
  13. MIMEXML = "application/xml"
  14. MIMEXML2 = "text/xml"
  15. MIMEPlain = "text/plain"
  16. MIMEPOSTForm = "application/x-www-form-urlencoded"
  17. MIMEMultipartPOSTForm = "multipart/form-data"
  18. MIMEPROTOBUF = "application/x-protobuf"
  19. MIMEMSGPACK = "application/x-msgpack"
  20. MIMEMSGPACK2 = "application/msgpack"
  21. )
  22. // Binding bind http request params to struct
  23. type Binding interface {
  24. Name() string
  25. Bind(*http.Request, interface{}) error
  26. }
  27. /*
  28. // StructValidator Validator
  29. type StructValidator interface {
  30. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  31. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  32. // If the received type is a struct or pointer to a struct, the validation should be performed.
  33. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  34. // Otherwise nil must be returned.
  35. ValidateStruct(interface{}) error
  36. // RegisterValidation adds a validation Func to a Validate's map of validators denoted by the key
  37. // NOTE: if the key already exists, the previous validation function will be replaced.
  38. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
  39. RegisterValidation(string, validator.Func) error
  40. }
  41. var Validator StructValidator = &defaultValidator{}
  42. // */
  43. var (
  44. JSON = jsonBinding{}
  45. XML = xmlBinding{}
  46. Form = formBinding{}
  47. Query = queryBinding{}
  48. FormPost = formPostBinding{}
  49. FormMultipart = formMultipartBinding{}
  50. ProtoBuf = protobufBinding{}
  51. MsgPack = msgpackBinding{}
  52. )
  53. // BindDefault default binding
  54. func BindDefault(method, contentType string) Binding {
  55. if method == "GET" {
  56. return Form
  57. }
  58. switch contentType {
  59. case MIMEJSON:
  60. return JSON
  61. case MIMEXML, MIMEXML2:
  62. return XML
  63. case MIMEPROTOBUF:
  64. return ProtoBuf
  65. case MIMEMSGPACK, MIMEMSGPACK2:
  66. return MsgPack
  67. default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
  68. return Form
  69. }
  70. }
  71. func validate(obj interface{}) error {
  72. return nil
  73. /*
  74. if Validator == nil {
  75. return nil
  76. }
  77. return Validator.ValidateStruct(obj)
  78. // */
  79. }
  80. func Bind(req *http.Request, obj interface{}) error {
  81. b := BindDefault(req.Method, ContentType(req))
  82. return MustBindWith(req, obj, b)
  83. }
  84. func MustBindWith(req *http.Request, obj interface{}, b Binding) (err error) {
  85. return b.Bind(req, obj)
  86. }
  87. func ContentType(req *http.Request) string {
  88. return filterFlags(req.Header.Get("Content-Type"))
  89. }
  90. func filterFlags(content string) string {
  91. for i, char := range content {
  92. if char == ' ' || char == ';' {
  93. return content[:i]
  94. }
  95. }
  96. return content
  97. }