utils.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "log"
  8. "reflect"
  9. "runtime"
  10. "strings"
  11. )
  12. type H map[string]interface{}
  13. // Allows type H to be used with xml.Marshal
  14. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  15. start.Name = xml.Name{
  16. Space: "",
  17. Local: "map",
  18. }
  19. if err := e.EncodeToken(start); err != nil {
  20. return err
  21. }
  22. for key, value := range h {
  23. elem := xml.StartElement{
  24. Name: xml.Name{Space: "", Local: key},
  25. Attr: []xml.Attr{},
  26. }
  27. if err := e.EncodeElement(value, elem); err != nil {
  28. return err
  29. }
  30. }
  31. if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
  32. return err
  33. }
  34. return nil
  35. }
  36. func filterFlags(content string) string {
  37. for i, char := range content {
  38. if char == ' ' || char == ';' {
  39. return content[:i]
  40. }
  41. }
  42. return content
  43. }
  44. func chooseData(custom, wildcard interface{}) interface{} {
  45. if custom == nil {
  46. if wildcard == nil {
  47. log.Panic("negotiation config is invalid")
  48. }
  49. return wildcard
  50. }
  51. return custom
  52. }
  53. func parseAccept(acceptHeader string) (parts []string) {
  54. parts = strings.Split(acceptHeader, ",")
  55. for i, part := range parts {
  56. index := strings.IndexByte(part, ';')
  57. if index >= 0 {
  58. part = part[0:index]
  59. }
  60. parts[i] = strings.TrimSpace(part)
  61. }
  62. return
  63. }
  64. func lastChar(str string) uint8 {
  65. size := len(str)
  66. if size == 0 {
  67. log.Panic("The length of the string can't be 0")
  68. }
  69. return str[size-1]
  70. }
  71. func nameOfFunction(f interface{}) string {
  72. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  73. }