util.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "go/format"
  7. "io"
  8. "reflect"
  9. "regexp"
  10. "strings"
  11. "github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil"
  12. )
  13. // GoFmt returns the Go formated string of the input.
  14. //
  15. // Panics if the format fails.
  16. func GoFmt(buf string) string {
  17. formatted, err := format.Source([]byte(buf))
  18. if err != nil {
  19. panic(fmt.Errorf("%s\nOriginal code:\n%s", err.Error(), buf))
  20. }
  21. return string(formatted)
  22. }
  23. var reTrim = regexp.MustCompile(`\s{2,}`)
  24. // Trim removes all leading and trailing white space.
  25. //
  26. // All consecutive spaces will be reduced to a single space.
  27. func Trim(s string) string {
  28. return strings.TrimSpace(reTrim.ReplaceAllString(s, " "))
  29. }
  30. // Capitalize capitalizes the first character of the string.
  31. func Capitalize(s string) string {
  32. if len(s) == 1 {
  33. return strings.ToUpper(s)
  34. }
  35. return strings.ToUpper(s[0:1]) + s[1:]
  36. }
  37. // SortXML sorts the reader's XML elements
  38. func SortXML(r io.Reader) string {
  39. var buf bytes.Buffer
  40. d := xml.NewDecoder(r)
  41. root, _ := xmlutil.XMLToStruct(d, nil)
  42. e := xml.NewEncoder(&buf)
  43. xmlutil.StructToXML(e, root, true)
  44. return buf.String()
  45. }
  46. // PrettyPrint generates a human readable representation of the value v.
  47. // All values of v are recursively found and pretty printed also.
  48. func PrettyPrint(v interface{}) string {
  49. value := reflect.ValueOf(v)
  50. switch value.Kind() {
  51. case reflect.Struct:
  52. str := fullName(value.Type()) + "{\n"
  53. for i := 0; i < value.NumField(); i++ {
  54. l := string(value.Type().Field(i).Name[0])
  55. if strings.ToUpper(l) == l {
  56. str += value.Type().Field(i).Name + ": "
  57. str += PrettyPrint(value.Field(i).Interface())
  58. str += ",\n"
  59. }
  60. }
  61. str += "}"
  62. return str
  63. case reflect.Map:
  64. str := "map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + "{\n"
  65. for _, k := range value.MapKeys() {
  66. str += "\"" + k.String() + "\": "
  67. str += PrettyPrint(value.MapIndex(k).Interface())
  68. str += ",\n"
  69. }
  70. str += "}"
  71. return str
  72. case reflect.Ptr:
  73. if e := value.Elem(); e.IsValid() {
  74. return "&" + PrettyPrint(e.Interface())
  75. }
  76. return "nil"
  77. case reflect.Slice:
  78. str := "[]" + fullName(value.Type().Elem()) + "{\n"
  79. for i := 0; i < value.Len(); i++ {
  80. str += PrettyPrint(value.Index(i).Interface())
  81. str += ",\n"
  82. }
  83. str += "}"
  84. return str
  85. default:
  86. return fmt.Sprintf("%#v", v)
  87. }
  88. }
  89. func pkgName(t reflect.Type) string {
  90. pkg := t.PkgPath()
  91. c := strings.Split(pkg, "/")
  92. return c[len(c)-1]
  93. }
  94. func fullName(t reflect.Type) string {
  95. if pkg := pkgName(t); pkg != "" {
  96. return pkg + "." + t.Name()
  97. }
  98. return t.Name()
  99. }