binding.go 3.0 KB

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