format.go 9.2 KB

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