dump.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "strings"
  8. util "github.com/senomas/go-util"
  9. )
  10. const (
  11. // MaxFieldLen const
  12. MaxFieldLen = 100
  13. // MaxSliceLen const
  14. MaxSliceLen = 100
  15. tabSpace = " "
  16. )
  17. // Dump value
  18. func Dump(v interface{}) string {
  19. var bb bytes.Buffer
  20. fdump(&bb, 0, 0, "", reflect.ValueOf(v))
  21. return bb.String()
  22. }
  23. // Fdump value
  24. // func Fdump(w io.Writer, v interface{}) {
  25. // fdump(w, 0, 0, "", reflect.ValueOf(v), "", "")
  26. // }
  27. func fdump(w io.Writer, rx int, rd int, tab string, value reflect.Value) {
  28. rx++
  29. if rx > 100 {
  30. fmt.Fprint(w, "/* too many recursive */")
  31. }
  32. if rd > 10 {
  33. fmt.Fprint(w, "/* too deep */")
  34. }
  35. if value.Kind() == reflect.Interface && !value.IsNil() {
  36. elm := value.Elem()
  37. if elm.Kind() == reflect.Ptr && !elm.IsNil() && elm.Elem().Kind() == reflect.Ptr {
  38. value = elm
  39. }
  40. }
  41. if value.Kind() == reflect.Ptr {
  42. value = value.Elem()
  43. }
  44. if value.Kind() == reflect.String {
  45. fmt.Fprintf(w, "\"%s\" /* Type:%s */", value, value.Type().String())
  46. } else if kindIn(value.Kind(), reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64) {
  47. fmt.Fprintf(w, "%+v /* Type:%s */", value, value.Type().String())
  48. } else if kindIn(value.Kind(), reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) {
  49. fmt.Fprintf(w, "%+v /* Type:%s */", value, value.Type().String())
  50. } else if value.Kind() == reflect.Struct {
  51. ntab := tab + tabSpace
  52. fmt.Fprintf(w, "{")
  53. il := value.NumField()
  54. for i, j := 0, 0; i < il && i < MaxFieldLen; i++ {
  55. tf := value.Type().Field(i)
  56. tag := strings.Split(value.Type().Field(i).Tag.Get("dump"), ",")
  57. if !util.InStringSlice("ignore", tag...) {
  58. if j > 0 {
  59. fmt.Fprintf(w, ",\n%s\"%s\": ", ntab, tf.Name)
  60. } else {
  61. fmt.Fprintf(w, "\n%s\"%s\": ", ntab, tf.Name)
  62. }
  63. vf := value.Field(i)
  64. fdump(w, rx, rd+1, ntab, vf)
  65. j++
  66. }
  67. }
  68. fmt.Fprintf(w, "\n%s}", tab)
  69. } else if value.Kind() == reflect.Slice {
  70. ntab := tab + tabSpace
  71. fmt.Fprintf(w, "[ /* Type:%s Length:%v */\n%s", value.Type().String(), value.Len(), ntab)
  72. il := value.Len()
  73. for i := 0; i < il && i < MaxSliceLen; i++ {
  74. vi := value.Index(i)
  75. if i > 0 {
  76. fmt.Fprintf(w, ",\n%s/* Index:%v */ ", ntab, i)
  77. } else {
  78. fmt.Fprint(w, "/* Index:0 */ ")
  79. }
  80. fdump(w, rx, rd+1, ntab, vi)
  81. }
  82. if il > MaxSliceLen {
  83. fmt.Fprintf(w, "\n%s/* too many items */", ntab)
  84. }
  85. fmt.Fprintf(w, "\n%s]%s", tab, "")
  86. // } else if value.CanInterface() {
  87. // fmt.Fprintf(w, "%+v \\\\ Type:%s", value.Interface(), value.Type().String())
  88. } else if value.Kind() == reflect.Map {
  89. ntab := tab + tabSpace
  90. fmt.Fprintf(w, "{ /* Type:%s */\n%s", value.Type().String(), ntab)
  91. keys := value.MapKeys()
  92. for k, kv := range keys {
  93. if k > 0 {
  94. fmt.Fprintf(w, ",\n%s\"%v\": ", ntab, kv)
  95. } else {
  96. fmt.Fprintf(w, "\"%v\": ", kv)
  97. }
  98. fdump(w, rx, rd+1, ntab, value.MapIndex(kv))
  99. }
  100. fmt.Fprintf(w, "\n%s}%s", tab, "")
  101. } else {
  102. fmt.Fprintf(w, "undefined /* Type:%s Value:[%v] */", value.Type().String(), value)
  103. }
  104. }
  105. func kindIn(value reflect.Kind, list ...reflect.Kind) bool {
  106. for _, v := range list {
  107. if value == v {
  108. return true
  109. }
  110. }
  111. return false
  112. }