format.go 11 KB

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