binding.go 996 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. const (
  7. MIMEJSON = "application/json"
  8. MIMEHTML = "text/html"
  9. MIMEXML = "application/xml"
  10. MIMEXML2 = "text/xml"
  11. MIMEPlain = "text/plain"
  12. MIMEPOSTForm = "application/x-www-form-urlencoded"
  13. MIMEMultipartPOSTForm = "multipart/form-data"
  14. )
  15. type Binding interface {
  16. Name() string
  17. Bind(*http.Request, interface{}) error
  18. }
  19. var (
  20. JSON = jsonBinding{}
  21. XML = xmlBinding{}
  22. GETForm = getFormBinding{}
  23. POSTForm = postFormBinding{}
  24. )
  25. func Default(method, contentType string) Binding {
  26. if method == "GET" {
  27. return GETForm
  28. } else {
  29. switch contentType {
  30. case MIMEPOSTForm:
  31. return POSTForm
  32. case MIMEJSON:
  33. return JSON
  34. case MIMEXML, MIMEXML2:
  35. return XML
  36. default:
  37. return GETForm
  38. }
  39. }
  40. }