format.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (c) 2013 Dave Collins <dave@davec.name>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package spew
  17. import (
  18. "bytes"
  19. "fmt"
  20. "reflect"
  21. "strconv"
  22. "strings"
  23. )
  24. // supportedFlags is a list of all the character flags supported by fmt package.
  25. const supportedFlags = "0-+# "
  26. // formatState implements the fmt.Formatter interface and contains information
  27. // about the state of a formatting operation. The NewFormatter function can
  28. // be used to get a new Formatter which can be used directly as arguments
  29. // in standard fmt package printing calls.
  30. type formatState struct {
  31. value interface{}
  32. buffer bytes.Buffer
  33. depth int
  34. pointers map[uintptr]int // Holds map of points and depth they were seen at
  35. fs fmt.State
  36. }
  37. // buildDefaultFormat recreates the original format string without precision
  38. // and width information to pass in to fmt.Sprintf in the case of an
  39. // unrecognized type. Unless new types are added to the language, this
  40. // function won't ever be called.
  41. func (f *formatState) buildDefaultFormat() (format string) {
  42. buf := bytes.NewBuffer(percentBytes)
  43. for _, flag := range supportedFlags {
  44. if f.fs.Flag(int(flag)) {
  45. buf.WriteRune(flag)
  46. }
  47. }
  48. buf.WriteRune('v')
  49. format = buf.String()
  50. return format
  51. }
  52. // constructOrigFormat recreates the original format string including precision
  53. // and width information to pass along to the standard fmt package. This allows
  54. // automatic deferral of all format strings this package doesn't support.
  55. func (f *formatState) constructOrigFormat(verb rune) (format string) {
  56. buf := bytes.NewBuffer(percentBytes)
  57. for _, flag := range supportedFlags {
  58. if f.fs.Flag(int(flag)) {
  59. buf.WriteRune(flag)
  60. }
  61. }
  62. if width, ok := f.fs.Width(); ok {
  63. buf.WriteString(strconv.Itoa(width))
  64. }
  65. if precision, ok := f.fs.Precision(); ok {
  66. buf.Write(precisionBytes)
  67. buf.WriteString(strconv.Itoa(precision))
  68. }
  69. buf.WriteRune(verb)
  70. format = buf.String()
  71. return format
  72. }
  73. // formatPtr handles formatting of pointers by indirecting them as necessary.
  74. func (f *formatState) formatPtr(v reflect.Value) {
  75. // Display nil if top level poiner is nil.
  76. if v.IsNil() {
  77. f.buffer.Write(nilAngleBytes)
  78. return
  79. }
  80. // Remove pointers at or below the current depth from map used to detect
  81. // circular refs.
  82. for k, depth := range f.pointers {
  83. if depth >= f.depth {
  84. delete(f.pointers, k)
  85. }
  86. }
  87. plusSyntax := f.fs.Flag('+')
  88. // Keep list of all dereferenced pointers to possibly show later.
  89. pointerChain := make([]uintptr, 0)
  90. // Figure out how many levels of indirection there are by derferencing
  91. // pointers and unpacking interfaces down the chain while detecting circular
  92. // references.
  93. nilFound := false
  94. cycleFound := false
  95. indirects := 0
  96. ve := v
  97. for ve.Kind() == reflect.Ptr {
  98. indirects++
  99. if ve.IsNil() {
  100. nilFound = true
  101. break
  102. }
  103. addr := ve.Pointer()
  104. pointerChain = append(pointerChain, addr)
  105. if pd, ok := f.pointers[addr]; ok && pd < f.depth {
  106. cycleFound = true
  107. break
  108. }
  109. f.pointers[addr] = f.depth
  110. ve = ve.Elem()
  111. if ve.Kind() == reflect.Interface {
  112. if ve.IsNil() {
  113. nilFound = true
  114. break
  115. }
  116. ve = ve.Elem()
  117. }
  118. }
  119. // Display indirection level.
  120. f.buffer.Write(openAngleBytes)
  121. f.buffer.WriteString(strings.Repeat("*", indirects))
  122. f.buffer.Write(closeAngleBytes)
  123. // Display pointer information depending on flags.
  124. if plusSyntax && (len(pointerChain) > 0) {
  125. f.buffer.Write(openParenBytes)
  126. for i, addr := range pointerChain {
  127. if i > 0 {
  128. f.buffer.Write(pointerChainBytes)
  129. }
  130. printHexPtr(&f.buffer, addr)
  131. }
  132. f.buffer.Write(closeParenBytes)
  133. }
  134. // Display dereferenced value.
  135. switch {
  136. case nilFound == true:
  137. f.buffer.Write(nilAngleBytes)
  138. case cycleFound == true:
  139. f.buffer.Write(circularShortBytes)
  140. default:
  141. f.format(ve)
  142. }
  143. }
  144. // format is the main workhorse for providing the Formatter interface. It
  145. // uses the passed reflect value to figure out what kind of object we are
  146. // dealing with and formats it appropriately. It is a recursive function,
  147. // however circular data structures are detected and handled properly.
  148. func (f *formatState) format(v reflect.Value) {
  149. // Call error/Stringer interfaces if they exist and the handle methods
  150. // flag is enabled.
  151. kind := v.Kind()
  152. if !Config.DisableMethods {
  153. if (kind != reflect.Invalid) && (kind != reflect.Interface) {
  154. if handled := handleMethods(&f.buffer, v); handled {
  155. return
  156. }
  157. }
  158. }
  159. switch kind {
  160. case reflect.Invalid:
  161. f.buffer.Write(invalidAngleBytes)
  162. case reflect.Bool:
  163. printBool(&f.buffer, v.Bool())
  164. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  165. printInt(&f.buffer, v.Int())
  166. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  167. printUint(&f.buffer, v.Uint())
  168. case reflect.Float32:
  169. printFloat(&f.buffer, v.Float(), 32)
  170. case reflect.Float64:
  171. printFloat(&f.buffer, v.Float(), 64)
  172. case reflect.Complex64:
  173. printComplex(&f.buffer, v.Complex(), 32)
  174. case reflect.Complex128:
  175. printComplex(&f.buffer, v.Complex(), 64)
  176. case reflect.Array, reflect.Slice:
  177. f.buffer.WriteRune('[')
  178. f.depth++
  179. if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
  180. f.buffer.Write(maxShortBytes)
  181. } else {
  182. numEntries := v.Len()
  183. for i := 0; i < numEntries; i++ {
  184. if i > 0 {
  185. f.buffer.WriteRune(' ')
  186. }
  187. f.format(unpackValue(v.Index(i)))
  188. }
  189. }
  190. f.depth--
  191. f.buffer.WriteRune(']')
  192. case reflect.String:
  193. f.buffer.WriteString(v.String())
  194. case reflect.Interface:
  195. // Do nothing. We should never get here due to unpackValue calls
  196. case reflect.Map:
  197. f.buffer.Write(openMapBytes)
  198. f.depth++
  199. if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
  200. f.buffer.Write(maxShortBytes)
  201. } else {
  202. keys := v.MapKeys()
  203. for i, key := range keys {
  204. if i > 0 {
  205. f.buffer.WriteRune(' ')
  206. }
  207. f.format(unpackValue(key))
  208. f.buffer.WriteRune(':')
  209. f.format(unpackValue(v.MapIndex(key)))
  210. }
  211. }
  212. f.depth--
  213. f.buffer.Write(closeMapBytes)
  214. case reflect.Ptr:
  215. f.formatPtr(v)
  216. case reflect.Struct:
  217. numFields := v.NumField()
  218. f.buffer.WriteRune('{')
  219. f.depth++
  220. if (Config.MaxDepth != 0) && (f.depth > Config.MaxDepth) {
  221. f.buffer.Write(maxShortBytes)
  222. } else {
  223. vt := v.Type()
  224. for i := 0; i < numFields; i++ {
  225. if i > 0 {
  226. f.buffer.WriteRune(' ')
  227. }
  228. vtf := vt.Field(i)
  229. if f.fs.Flag('+') {
  230. f.buffer.WriteString(vtf.Name)
  231. f.buffer.WriteRune(':')
  232. }
  233. f.format(unpackValue(v.Field(i)))
  234. }
  235. }
  236. f.depth--
  237. f.buffer.WriteRune('}')
  238. case reflect.Uintptr:
  239. printHexPtr(&f.buffer, uintptr(v.Uint()))
  240. case reflect.UnsafePointer, reflect.Chan, reflect.Func:
  241. printHexPtr(&f.buffer, v.Pointer())
  242. // There were not any other types at the time this code was written, but
  243. // fall back to letting the default fmt package handle it if any get added.
  244. default:
  245. format := f.buildDefaultFormat()
  246. if v.CanInterface() {
  247. f.buffer.WriteString(fmt.Sprintf(format, v.Interface()))
  248. } else {
  249. f.buffer.WriteString(fmt.Sprintf(format, v.String()))
  250. }
  251. }
  252. }
  253. // Format satisfies the fmt.Formatter interface. See NewFormatter for usage
  254. // details.
  255. func (f *formatState) Format(fs fmt.State, verb rune) {
  256. f.fs = fs
  257. // Use standard formatting for verbs that are not v or #v.
  258. if (verb != 'v') || (verb == 'v' && fs.Flag('#')) {
  259. format := f.constructOrigFormat(verb)
  260. fmt.Fprintf(fs, format, f.value)
  261. return
  262. }
  263. if f.value == nil {
  264. fmt.Fprint(fs, string(nilAngleBytes))
  265. return
  266. }
  267. f.format(reflect.ValueOf(f.value))
  268. f.buffer.WriteTo(fs)
  269. }
  270. /*
  271. NewFormatter returns a custom formatter that satisfies the fmt.Formatter
  272. interface. As a result, it integrates cleanly with standard fmt package
  273. printing functions. The formatter is useful for inline printing of smaller data
  274. types similar to the standard %v format specifier.
  275. The custom formatter only responds to the %v and %+v verb combinations. Any
  276. other variations such as %x, %q, and %#v will be sent to the the standard fmt
  277. package for formatting. In addition, the custom formatter ignores the width and
  278. precision arguments (however they will still work on the format specifiers not
  279. handled by the custom formatter).
  280. Typically this function shouldn't be called directly. It is much easier to make
  281. use of the custom formatter is to call one of the convenience functions such as
  282. Printf, Println, or Printf.
  283. */
  284. func NewFormatter(v interface{}) (f fmt.Formatter) {
  285. fs := &formatState{value: v}
  286. fs.pointers = make(map[uintptr]int)
  287. return fs
  288. }