format.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. fs fmt.State
  33. depth int
  34. pointers map[uintptr]int
  35. ignoreNextType bool
  36. cs *ConfigState
  37. }
  38. // buildDefaultFormat recreates the original format string without precision
  39. // and width information to pass in to fmt.Sprintf in the case of an
  40. // unrecognized type. Unless new types are added to the language, this
  41. // function won't ever be called.
  42. func (f *formatState) buildDefaultFormat() (format string) {
  43. buf := bytes.NewBuffer(percentBytes)
  44. for _, flag := range supportedFlags {
  45. if f.fs.Flag(int(flag)) {
  46. buf.WriteRune(flag)
  47. }
  48. }
  49. buf.WriteRune('v')
  50. format = buf.String()
  51. return format
  52. }
  53. // constructOrigFormat recreates the original format string including precision
  54. // and width information to pass along to the standard fmt package. This allows
  55. // automatic deferral of all format strings this package doesn't support.
  56. func (f *formatState) constructOrigFormat(verb rune) (format string) {
  57. buf := bytes.NewBuffer(percentBytes)
  58. for _, flag := range supportedFlags {
  59. if f.fs.Flag(int(flag)) {
  60. buf.WriteRune(flag)
  61. }
  62. }
  63. if width, ok := f.fs.Width(); ok {
  64. buf.WriteString(strconv.Itoa(width))
  65. }
  66. if precision, ok := f.fs.Precision(); ok {
  67. buf.Write(precisionBytes)
  68. buf.WriteString(strconv.Itoa(precision))
  69. }
  70. buf.WriteRune(verb)
  71. format = buf.String()
  72. return format
  73. }
  74. // unpackValue returns values inside of non-nil interfaces when possible and
  75. // ensures that types for values which have been unpacked from an interface
  76. // are displayed when the show types flag is also set.
  77. // This is useful for data types like structs, arrays, slices, and maps which
  78. // can contain varying types packed inside an interface.
  79. func (f *formatState) unpackValue(v reflect.Value) reflect.Value {
  80. if v.Kind() == reflect.Interface && !v.IsNil() {
  81. f.ignoreNextType = false
  82. v = v.Elem()
  83. }
  84. return v
  85. }
  86. // formatPtr handles formatting of pointers by indirecting them as necessary.
  87. func (f *formatState) formatPtr(v reflect.Value) {
  88. // Display nil if top level pointer is nil.
  89. showTypes := f.fs.Flag('#')
  90. if v.IsNil() && (!showTypes || f.ignoreNextType) {
  91. f.fs.Write(nilAngleBytes)
  92. return
  93. }
  94. // Remove pointers at or below the current depth from map used to detect
  95. // circular refs.
  96. for k, depth := range f.pointers {
  97. if depth >= f.depth {
  98. delete(f.pointers, k)
  99. }
  100. }
  101. // Keep list of all dereferenced pointers to possibly show later.
  102. pointerChain := make([]uintptr, 0)
  103. // Figure out how many levels of indirection there are by derferencing
  104. // pointers and unpacking interfaces down the chain while detecting circular
  105. // references.
  106. nilFound := false
  107. cycleFound := false
  108. indirects := 0
  109. ve := v
  110. for ve.Kind() == reflect.Ptr {
  111. if ve.IsNil() {
  112. nilFound = true
  113. break
  114. }
  115. indirects++
  116. addr := ve.Pointer()
  117. pointerChain = append(pointerChain, addr)
  118. if pd, ok := f.pointers[addr]; ok && pd < f.depth {
  119. cycleFound = true
  120. indirects--
  121. break
  122. }
  123. f.pointers[addr] = f.depth
  124. ve = ve.Elem()
  125. if ve.Kind() == reflect.Interface {
  126. if ve.IsNil() {
  127. nilFound = true
  128. break
  129. }
  130. ve = ve.Elem()
  131. }
  132. }
  133. // Display type or indirection level depending on flags.
  134. if showTypes && !f.ignoreNextType {
  135. f.fs.Write(openParenBytes)
  136. f.fs.Write(bytes.Repeat(asteriskBytes, indirects))
  137. f.fs.Write([]byte(ve.Type().String()))
  138. f.fs.Write(closeParenBytes)
  139. } else {
  140. if nilFound || cycleFound {
  141. indirects += strings.Count(ve.Type().String(), "*")
  142. }
  143. f.fs.Write(openAngleBytes)
  144. f.fs.Write([]byte(strings.Repeat("*", indirects)))
  145. f.fs.Write(closeAngleBytes)
  146. }
  147. // Display pointer information depending on flags.
  148. if f.fs.Flag('+') && (len(pointerChain) > 0) {
  149. f.fs.Write(openParenBytes)
  150. for i, addr := range pointerChain {
  151. if i > 0 {
  152. f.fs.Write(pointerChainBytes)
  153. }
  154. printHexPtr(f.fs, addr)
  155. }
  156. f.fs.Write(closeParenBytes)
  157. }
  158. // Display dereferenced value.
  159. switch {
  160. case nilFound == true:
  161. f.fs.Write(nilAngleBytes)
  162. case cycleFound == true:
  163. f.fs.Write(circularShortBytes)
  164. default:
  165. f.ignoreNextType = true
  166. f.format(ve)
  167. }
  168. }
  169. // format is the main workhorse for providing the Formatter interface. It
  170. // uses the passed reflect value to figure out what kind of object we are
  171. // dealing with and formats it appropriately. It is a recursive function,
  172. // however circular data structures are detected and handled properly.
  173. func (f *formatState) format(v reflect.Value) {
  174. // Handle pointers specially.
  175. kind := v.Kind()
  176. if kind == reflect.Ptr {
  177. f.formatPtr(v)
  178. return
  179. }
  180. // Print type information unless already handled elsewhere.
  181. if !f.ignoreNextType && f.fs.Flag('#') {
  182. f.fs.Write(openParenBytes)
  183. f.fs.Write([]byte(v.Type().String()))
  184. f.fs.Write(closeParenBytes)
  185. }
  186. f.ignoreNextType = false
  187. // Call Stringer/error interfaces if they exist and the handle methods
  188. // flag is enabled.
  189. if !f.cs.DisableMethods {
  190. if (kind != reflect.Invalid) && (kind != reflect.Interface) {
  191. if handled := handleMethods(f.cs, f.fs, v); handled {
  192. return
  193. }
  194. }
  195. }
  196. switch kind {
  197. case reflect.Invalid:
  198. f.fs.Write(invalidAngleBytes)
  199. case reflect.Bool:
  200. printBool(f.fs, v.Bool())
  201. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
  202. printInt(f.fs, v.Int())
  203. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  204. printUint(f.fs, v.Uint())
  205. case reflect.Float32:
  206. printFloat(f.fs, v.Float(), 32)
  207. case reflect.Float64:
  208. printFloat(f.fs, v.Float(), 64)
  209. case reflect.Complex64:
  210. printComplex(f.fs, v.Complex(), 32)
  211. case reflect.Complex128:
  212. printComplex(f.fs, v.Complex(), 64)
  213. case reflect.Array, reflect.Slice:
  214. f.fs.Write(openBracketBytes)
  215. f.depth++
  216. if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
  217. f.fs.Write(maxShortBytes)
  218. } else {
  219. numEntries := v.Len()
  220. for i := 0; i < numEntries; i++ {
  221. if i > 0 {
  222. f.fs.Write(spaceBytes)
  223. }
  224. f.ignoreNextType = true
  225. f.format(f.unpackValue(v.Index(i)))
  226. }
  227. }
  228. f.depth--
  229. f.fs.Write(closeBracketBytes)
  230. case reflect.String:
  231. f.fs.Write([]byte(v.String()))
  232. case reflect.Interface:
  233. // Do nothing. We should never get here due to unpackValue calls
  234. case reflect.Ptr:
  235. // Do nothing. We should never get here since pointers have already
  236. // been handled above.
  237. case reflect.Map:
  238. f.fs.Write(openMapBytes)
  239. f.depth++
  240. if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
  241. f.fs.Write(maxShortBytes)
  242. } else {
  243. keys := v.MapKeys()
  244. for i, key := range keys {
  245. if i > 0 {
  246. f.fs.Write(spaceBytes)
  247. }
  248. f.ignoreNextType = true
  249. f.format(f.unpackValue(key))
  250. f.fs.Write(colonBytes)
  251. f.ignoreNextType = true
  252. f.format(f.unpackValue(v.MapIndex(key)))
  253. }
  254. }
  255. f.depth--
  256. f.fs.Write(closeMapBytes)
  257. case reflect.Struct:
  258. numFields := v.NumField()
  259. f.fs.Write(openBraceBytes)
  260. f.depth++
  261. if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {
  262. f.fs.Write(maxShortBytes)
  263. } else {
  264. vt := v.Type()
  265. for i := 0; i < numFields; i++ {
  266. if i > 0 {
  267. f.fs.Write(spaceBytes)
  268. }
  269. vtf := vt.Field(i)
  270. if f.fs.Flag('+') || f.fs.Flag('#') {
  271. f.fs.Write([]byte(vtf.Name))
  272. f.fs.Write(colonBytes)
  273. }
  274. f.format(f.unpackValue(v.Field(i)))
  275. }
  276. }
  277. f.depth--
  278. f.fs.Write(closeBraceBytes)
  279. case reflect.Uintptr:
  280. printHexPtr(f.fs, uintptr(v.Uint()))
  281. case reflect.UnsafePointer, reflect.Chan, reflect.Func:
  282. printHexPtr(f.fs, v.Pointer())
  283. // There were not any other types at the time this code was written, but
  284. // fall back to letting the default fmt package handle it if any get added.
  285. default:
  286. format := f.buildDefaultFormat()
  287. if v.CanInterface() {
  288. fmt.Fprintf(f.fs, format, v.Interface())
  289. } else {
  290. fmt.Fprintf(f.fs, format, v.String())
  291. }
  292. }
  293. }
  294. // Format satisfies the fmt.Formatter interface. See NewFormatter for usage
  295. // details.
  296. func (f *formatState) Format(fs fmt.State, verb rune) {
  297. f.fs = fs
  298. // Use standard formatting for verbs that are not v.
  299. if verb != 'v' {
  300. format := f.constructOrigFormat(verb)
  301. fmt.Fprintf(fs, format, f.value)
  302. return
  303. }
  304. if f.value == nil {
  305. if fs.Flag('#') {
  306. fs.Write(interfaceBytes)
  307. }
  308. fs.Write(nilAngleBytes)
  309. return
  310. }
  311. f.format(reflect.ValueOf(f.value))
  312. }
  313. // newFormatter is a helper function to consolidate the logic from the various
  314. // public methods which take varying config states.
  315. func newFormatter(cs *ConfigState, v interface{}) fmt.Formatter {
  316. fs := &formatState{value: v, cs: cs}
  317. fs.pointers = make(map[uintptr]int)
  318. return fs
  319. }
  320. /*
  321. NewFormatter returns a custom formatter that satisfies the fmt.Formatter
  322. interface. As a result, it integrates cleanly with standard fmt package
  323. printing functions. The formatter is useful for inline printing of smaller data
  324. types similar to the standard %v format specifier.
  325. The custom formatter only responds to the %v (most compact), %+v (adds pointer
  326. addresses), %#v (adds types), or %#+v (adds types and pointer addresses) verb
  327. combinations. Any other verbs such as %x and %q will be sent to the the
  328. standard fmt package for formatting. In addition, the custom formatter ignores
  329. the width and precision arguments (however they will still work on the format
  330. specifiers not handled by the custom formatter).
  331. Typically this function shouldn't be called directly. It is much easier to make
  332. use of the custom formatter by calling one of the convenience functions such as
  333. Printf, Println, or Printf.
  334. */
  335. func NewFormatter(v interface{}) fmt.Formatter {
  336. return newFormatter(&Config, v)
  337. }