utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 gin
  5. import (
  6. "encoding/xml"
  7. "net/http"
  8. "path"
  9. "reflect"
  10. "runtime"
  11. "strings"
  12. )
  13. const BindKey = "_gin-gonic/gin/bindkey"
  14. func Bind(val interface{}) HandlerFunc {
  15. typ := reflect.ValueOf(val).Type()
  16. return func(c *Context) {
  17. obj := reflect.New(typ).Interface()
  18. if c.Bind(obj) == nil {
  19. c.Set(BindKey, obj)
  20. }
  21. }
  22. }
  23. func WrapF(f http.HandlerFunc) HandlerFunc {
  24. return func(c *Context) {
  25. f(c.Writer, c.Request)
  26. }
  27. }
  28. func WrapH(h http.Handler) HandlerFunc {
  29. return func(c *Context) {
  30. h.ServeHTTP(c.Writer, c.Request)
  31. }
  32. }
  33. type H map[string]interface{}
  34. // Allows type H to be used with xml.Marshal
  35. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  36. start.Name = xml.Name{
  37. Space: "",
  38. Local: "map",
  39. }
  40. if err := e.EncodeToken(start); err != nil {
  41. return err
  42. }
  43. for key, value := range h {
  44. elem := xml.StartElement{
  45. Name: xml.Name{Space: "", Local: key},
  46. Attr: []xml.Attr{},
  47. }
  48. if err := e.EncodeElement(value, elem); err != nil {
  49. return err
  50. }
  51. }
  52. if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. func filterFlags(content string) string {
  58. for i, char := range content {
  59. if char == ' ' || char == ';' {
  60. return content[:i]
  61. }
  62. }
  63. return content
  64. }
  65. func chooseData(custom, wildcard interface{}) interface{} {
  66. if custom == nil {
  67. if wildcard == nil {
  68. panic("negotiation config is invalid")
  69. }
  70. return wildcard
  71. }
  72. return custom
  73. }
  74. func parseAccept(acceptHeader string) []string {
  75. parts := strings.Split(acceptHeader, ",")
  76. out := make([]string, 0, len(parts))
  77. for _, part := range parts {
  78. index := strings.IndexByte(part, ';')
  79. if index >= 0 {
  80. part = part[0:index]
  81. }
  82. part = strings.TrimSpace(part)
  83. if len(part) > 0 {
  84. out = append(out, part)
  85. }
  86. }
  87. return out
  88. }
  89. func lastChar(str string) uint8 {
  90. size := len(str)
  91. if size == 0 {
  92. panic("The length of the string can't be 0")
  93. }
  94. return str[size-1]
  95. }
  96. func nameOfFunction(f interface{}) string {
  97. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  98. }
  99. func joinPaths(absolutePath, relativePath string) string {
  100. if len(relativePath) == 0 {
  101. return absolutePath
  102. }
  103. finalPath := path.Join(absolutePath, relativePath)
  104. appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
  105. if appendSlash {
  106. return finalPath + "/"
  107. }
  108. return finalPath
  109. }