Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. +// formatFakePointers controls whether to substitute pointer addresses with nil.
  2. +// This is used for deterministic testing.
  3. +var formatFakePointers = false
  4.  
  5. var stringerIface = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
  6.  
  7. -func prettyPrint(v reflect.Value, useStringer bool) string {
  8. - return formatAny(v, formatConfig{useStringer, true, true, true}, nil)
  9. +// Format formats the value v as a string.
  10. +//
  11. +// This is similar to fmt.Sprintf("%+v", v) except this:
  12. +// * Prints the type unless it can be elided
  13. +// * Avoids printing struct fields that are zero
  14. +// * Prints a nil-slice as being nil, not empty
  15. +// * Prints map entries in deterministic order
  16. +func Format(v reflect.Value, useStringer bool) string {
  17. + return formatAny(v, formatConfig{useStringer, true, true, !formatFakePointers}, nil)
  18. }
  19.  
  20. type formatConfig struct {
  21. @@ -69,12 +36,6 @@ type formatConfig struct {
  22. realPointers bool // Should we print the real address of pointers?
  23. }
  24.  
  25. -// formatAny prints the value v in a pretty formatted manner.
  26. -// This is similar to fmt.Sprintf("%+v", v) except this:
  27. -// * Prints the type unless it can be elided.
  28. -// * Avoids printing struct fields that are zero.
  29. -// * Prints a nil-slice as being nil, not empty.
  30. -// * Prints map entries in deterministic order.
  31. func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) string {
  32. // TODO: Should this be a multi-line printout in certain situations?
  33.  
  34. @@ -90,20 +51,21 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
  35.  
  36. switch v.Kind() {
  37. case reflect.Bool:
  38. - return fmt.Sprint(v.Bool())
  39. + return formatPrimitive(v.Type(), v.Bool(), conf)
  40. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  41. - return fmt.Sprint(v.Int())
  42. + return formatPrimitive(v.Type(), v.Int(), conf)
  43. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  44. if v.Type().PkgPath() == "" || v.Kind() == reflect.Uintptr {
  45. - return formatHex(v.Uint()) // Unnamed uints are usually bytes or words
  46. + // Unnamed uints are usually bytes or words, so use hexadecimal.
  47. + return formatPrimitive(v.Type(), formatHex(v.Uint()), conf)
  48. }
  49. - return fmt.Sprint(v.Uint()) // Named uints are usually enumerations
  50. + return formatPrimitive(v.Type(), v.Uint(), conf)
  51. case reflect.Float32, reflect.Float64:
  52. - return fmt.Sprint(v.Float())
  53. + return formatPrimitive(v.Type(), v.Float(), conf)
  54. case reflect.Complex64, reflect.Complex128:
  55. - return fmt.Sprint(v.Complex())
  56. + return formatPrimitive(v.Type(), v.Complex(), conf)
  57. case reflect.String:
  58. - return fmt.Sprintf("%q", v)
  59. + return formatPrimitive(v.Type(), fmt.Sprintf("%q", v), conf)
  60. case reflect.UnsafePointer, reflect.Chan, reflect.Func:
  61. return formatPointer(v, conf)
  62. case reflect.Ptr:
  63. @@ -166,7 +128,7 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
  64. var ss []string
  65. subConf := conf
  66. subConf.printType = v.Type().Elem().Kind() == reflect.Interface
  67. - for _, k := range sortKeys(v.MapKeys()) {
  68. + for _, k := range SortKeys(v.MapKeys()) {
  69. sk := formatAny(k, formatConfig{realPointers: conf.realPointers}, visited)
  70. sv := formatAny(v.MapIndex(k), subConf, visited)
  71. ss = append(ss, fmt.Sprintf("%s: %s", sk, sv))
  72. @@ -200,6 +162,13 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
  73. }
  74. }
  75.  
  76. +func formatPrimitive(t reflect.Type, v interface{}, conf formatConfig) string {
  77. + if conf.printType && t.PkgPath() != "" {
  78. + return fmt.Sprintf("%v(%v)", t, v)
  79. + }
  80. + return fmt.Sprintf("%v", v)
  81. +}
  82. +
  83. func formatPointer(v reflect.Value, conf formatConfig) string {
  84. p := v.Pointer()
  85. if !conf.realPointers {
  86. @@ -282,100 +251,8 @@ func isZero(v reflect.Value) bool {
  87. return false
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement