dump.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "strings"
  8. "time"
  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. if value.CanInterface() {
  52. if t, ok := value.Interface().(time.Time); ok {
  53. fmt.Fprintf(w, "%s /* Type:%s */", t, value.Type())
  54. return
  55. }
  56. }
  57. ntab := tab + tabSpace
  58. fmt.Fprintf(w, "{ /* Type:%s */", value.Type().String())
  59. il := value.NumField()
  60. for i, j := 0, 0; i < il && i < MaxFieldLen; i++ {
  61. tf := value.Type().Field(i)
  62. tag := strings.Split(value.Type().Field(i).Tag.Get("dump"), ",")
  63. if !InStringSlice("ignore", tag...) {
  64. if j > 0 {
  65. fmt.Fprintf(w, ",\n%s\"%s\": ", ntab, tf.Name)
  66. } else {
  67. fmt.Fprintf(w, "\n%s\"%s\": ", ntab, tf.Name)
  68. }
  69. vf := value.Field(i)
  70. fdump(w, rx, rd+1, ntab, vf)
  71. j++
  72. }
  73. }
  74. fmt.Fprintf(w, "\n%s}", tab)
  75. } else if value.Kind() == reflect.Slice {
  76. ntab := tab + tabSpace
  77. if value.Type().String() == "[]uint8" {
  78. il := value.Len()
  79. fmt.Fprintf(w, "[ /* Type:%s Length:%v */\n%s 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n%s0000 ", value.Type().String(), il, ntab, ntab)
  80. for i, j := 0, 0; i < il; i++ {
  81. if j == 16 {
  82. fmt.Fprintf(w, "\n%s%04X ", ntab, i)
  83. j = 1
  84. } else {
  85. j++
  86. }
  87. fmt.Fprintf(w, "%02X ", value.Index(i).Interface())
  88. }
  89. fmt.Fprintf(w, "\n%s]%s", tab, "")
  90. } else {
  91. fmt.Fprintf(w, "[ /* Type:%s Length:%v */\n%s", value.Type().String(), value.Len(), ntab)
  92. il := value.Len()
  93. for i := 0; i < il && i < MaxSliceLen; i++ {
  94. vi := value.Index(i)
  95. if i > 0 {
  96. fmt.Fprintf(w, ",\n%s/* Index:%v */ ", ntab, i)
  97. } else {
  98. fmt.Fprint(w, "/* Index:0 */ ")
  99. }
  100. fdump(w, rx, rd+1, ntab, vi)
  101. }
  102. if il > MaxSliceLen {
  103. fmt.Fprintf(w, "\n%s/* too many items */", ntab)
  104. }
  105. fmt.Fprintf(w, "\n%s]%s", tab, "")
  106. }
  107. } else if value.Kind() == reflect.Map {
  108. ntab := tab + tabSpace
  109. fmt.Fprintf(w, "{ /* Type:%s */\n%s", value.Type().String(), ntab)
  110. keys := value.MapKeys()
  111. for k, kv := range keys {
  112. if k > 0 {
  113. fmt.Fprintf(w, ",\n%s\"%v\": ", ntab, kv)
  114. } else {
  115. fmt.Fprintf(w, "\"%v\": ", kv)
  116. }
  117. fdump(w, rx, rd+1, ntab, value.MapIndex(kv))
  118. }
  119. fmt.Fprintf(w, "\n%s}%s", tab, "")
  120. } else if !value.IsValid() {
  121. fmt.Fprint(w, "/* NOT-VALID */")
  122. } else if value.Type() != nil {
  123. fmt.Fprintf(w, "undefined /* Type:%s Value:[%v] */", value.Type().String(), value)
  124. }
  125. }
  126. func kindIn(value reflect.Kind, list ...reflect.Kind) bool {
  127. for _, v := range list {
  128. if value == v {
  129. return true
  130. }
  131. }
  132. return false
  133. }