config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. "fmt"
  19. "io"
  20. "os"
  21. )
  22. // ConfigState houses the configuration options used by spew to format and
  23. // display values. There is a global instance, Config, that is used to control
  24. // all top-level Formatter and Dump functionality. Each ConfigState instance
  25. // provides methods equivalent to the top-level functions.
  26. //
  27. // The zero value for ConfigState provides no indentation. You would typically
  28. // want to set it to a space or a tab.
  29. //
  30. // Alternatively, you can use NewDefaultConfig to get a ConfigState instance
  31. // with default settings. See the documentation of NewDefaultConfig for default
  32. // values.
  33. type ConfigState struct {
  34. // Indent specifies the string to use for each indentation level. The
  35. // global config instance that all top-level functions use set this to a
  36. // single space by default. If you would like more indentation, you might
  37. // set this to a tab with "\t" or perhaps two spaces with " ".
  38. Indent string
  39. // MaxDepth controls the maximum number of levels to descend into nested
  40. // data structures. The default, 0, means there is no limit.
  41. //
  42. // NOTE: Circular data structures are properly detected, so it is not
  43. // necessary to set this value unless you specifically want to limit deeply
  44. // nested data structures.
  45. MaxDepth int
  46. // DisableMethods specifies whether or not error and Stringer interfaces are
  47. // invoked for types that implement them.
  48. DisableMethods bool
  49. // DisablePointerMethods specifies whether or not to check for and invoke
  50. // error and Stringer interfaces on types which only accept a pointer
  51. // receiver when the current type is not a pointer.
  52. //
  53. // NOTE: This might be an unsafe action since calling one of these methods
  54. // with a pointer receiver could technically mutate the value, however,
  55. // in practice, types which choose to satisify an error or Stringer
  56. // interface with a pointer receiver should not be mutating their state
  57. // inside these interface methods.
  58. DisablePointerMethods bool
  59. // ContinueOnMethod specifies whether or not recursion should continue once
  60. // a custom error or Stringer interface is invoked. The default, false,
  61. // means it will print the results of invoking the custom error or Stringer
  62. // interface and return immediately instead of continuing to recurse into
  63. // the internals of the data type.
  64. //
  65. // NOTE: This flag does not have any effect if method invocation is disabled
  66. // via the DisableMethods or DisablePointerMethods options.
  67. ContinueOnMethod bool
  68. }
  69. // Config is the active configuration of the top-level functions.
  70. // The configuration can be changed by modifying the contents of spew.Config.
  71. var Config ConfigState = ConfigState{Indent: " "}
  72. // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
  73. // passed with a Formatter interface returned by c.NewFormatter. It returns
  74. // the formatted string as a value that satisfies error. See NewFormatter
  75. // for formatting details.
  76. //
  77. // This function is shorthand for the following syntax:
  78. //
  79. // fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b))
  80. func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) {
  81. return fmt.Errorf(format, c.convertArgs(a)...)
  82. }
  83. // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
  84. // passed with a Formatter interface returned by c.NewFormatter. It returns
  85. // the number of bytes written and any write error encountered. See
  86. // NewFormatter for formatting details.
  87. //
  88. // This function is shorthand for the following syntax:
  89. //
  90. // fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b))
  91. func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
  92. return fmt.Fprint(w, c.convertArgs(a)...)
  93. }
  94. // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
  95. // passed with a Formatter interface returned by c.NewFormatter. It returns
  96. // the number of bytes written and any write error encountered. See
  97. // NewFormatter for formatting details.
  98. //
  99. // This function is shorthand for the following syntax:
  100. //
  101. // fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b))
  102. func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
  103. return fmt.Fprintf(w, format, c.convertArgs(a)...)
  104. }
  105. // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
  106. // passed with a Formatter interface returned by c.NewFormatter. See
  107. // NewFormatter for formatting details.
  108. //
  109. // This function is shorthand for the following syntax:
  110. //
  111. // fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b))
  112. func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
  113. return fmt.Fprintln(w, c.convertArgs(a)...)
  114. }
  115. // Print is a wrapper for fmt.Print that treats each argument as if it were
  116. // passed with a Formatter interface returned by c.NewFormatter. It returns
  117. // the number of bytes written and any write error encountered. See
  118. // NewFormatter for formatting details.
  119. //
  120. // This function is shorthand for the following syntax:
  121. //
  122. // fmt.Print(c.NewFormatter(a), c.NewFormatter(b))
  123. func (c *ConfigState) Print(a ...interface{}) (n int, err error) {
  124. return fmt.Print(c.convertArgs(a)...)
  125. }
  126. // Printf is a wrapper for fmt.Printf that treats each argument as if it were
  127. // passed with a Formatter interface returned by c.NewFormatter. It returns
  128. // the number of bytes written and any write error encountered. See
  129. // NewFormatter for formatting details.
  130. //
  131. // This function is shorthand for the following syntax:
  132. //
  133. // fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b))
  134. func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) {
  135. return fmt.Printf(format, c.convertArgs(a)...)
  136. }
  137. // Println is a wrapper for fmt.Println that treats each argument as if it were
  138. // passed with a Formatter interface returned by c.NewFormatter. It returns
  139. // the number of bytes written and any write error encountered. See
  140. // NewFormatter for formatting details.
  141. //
  142. // This function is shorthand for the following syntax:
  143. //
  144. // fmt.Println(c.NewFormatter(a), c.NewFormatter(b))
  145. func (c *ConfigState) Println(a ...interface{}) (n int, err error) {
  146. return fmt.Println(c.convertArgs(a)...)
  147. }
  148. // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
  149. // passed with a Formatter interface returned by c.NewFormatter. It returns
  150. // the resulting string. See NewFormatter for formatting details.
  151. //
  152. // This function is shorthand for the following syntax:
  153. //
  154. // fmt.Sprint(c.NewFormatter(a), c.NewFormatter(b))
  155. func (c *ConfigState) Sprint(a ...interface{}) string {
  156. return fmt.Sprint(c.convertArgs(a)...)
  157. }
  158. // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
  159. // passed with a Formatter interface returned by c.NewFormatter. It returns
  160. // the resulting string. See NewFormatter for formatting details.
  161. //
  162. // This function is shorthand for the following syntax:
  163. //
  164. // fmt.Sprintf(format, c.NewFormatter(a), c.NewFormatter(b))
  165. func (c *ConfigState) Sprintf(format string, a ...interface{}) string {
  166. return fmt.Sprintf(format, c.convertArgs(a)...)
  167. }
  168. // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
  169. // were passed with a Formatter interface returned by c.NewFormatter. It
  170. // returns the resulting string. See NewFormatter for formatting details.
  171. //
  172. // This function is shorthand for the following syntax:
  173. //
  174. // fmt.Sprintln(c.NewFormatter(a), c.NewFormatter(b))
  175. func (c *ConfigState) Sprintln(a ...interface{}) string {
  176. return fmt.Sprintln(c.convertArgs(a)...)
  177. }
  178. /*
  179. NewFormatter returns a custom formatter that satisfies the fmt.Formatter
  180. interface. As a result, it integrates cleanly with standard fmt package
  181. printing functions. The formatter is useful for inline printing of smaller data
  182. types similar to the standard %v format specifier.
  183. The custom formatter only responds to the %v (most compact), %+v (adds pointer
  184. addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb
  185. combinations. Any other verbs such as %x and %q will be sent to the the
  186. standard fmt package for formatting. In addition, the custom formatter ignores
  187. the width and precision arguments (however they will still work on the format
  188. specifiers not handled by the custom formatter).
  189. Typically this function shouldn't be called directly. It is much easier to make
  190. use of the custom formatter by calling one of the convenience functions such as
  191. c.Printf, c.Println, or c.Printf.
  192. */
  193. func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter {
  194. return newFormatter(c, v)
  195. }
  196. // Fdump formats and displays the passed arguments to io.Writer w. It formats
  197. // exactly the same as Dump.
  198. func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) {
  199. fdump(c, w, a...)
  200. }
  201. /*
  202. Dump displays the passed parameters to standard out with newlines, customizable
  203. indentation, and additional debug information such as complete types and all
  204. pointer addresses used to indirect to the final value. It provides the
  205. following features over the built-in printing facilities provided by the fmt
  206. package:
  207. * Pointers are dereferenced and followed
  208. * Circular data structures are detected and handled properly
  209. * Custom Stringer/error interfaces are optionally invoked, including
  210. on unexported types
  211. * Custom types which only implement the Stringer/error interfaces via
  212. a pointer receiver are optionally invoked when passing non-pointer
  213. variables
  214. The configuration options are controlled by modifying the public members
  215. of c. See ConfigState for options documentation.
  216. See Fdump if you would prefer dumping to an arbitrary io.Writer.
  217. */
  218. func (c *ConfigState) Dump(a ...interface{}) {
  219. fdump(c, os.Stdout, a...)
  220. }
  221. // convertArgs accepts a slice of arguments and returns a slice of the same
  222. // length with each argument converted to a spew Formatter interface using
  223. // the ConfigState associated with s.
  224. func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) {
  225. formatters = make([]interface{}, len(args))
  226. for index, arg := range args {
  227. formatters[index] = newFormatter(c, arg)
  228. }
  229. return formatters
  230. }
  231. // NewDefaultConfig returns a ConfigState with the following default settings.
  232. //
  233. // Indent: " "
  234. // MaxDepth: 0
  235. // DisableMethods: false
  236. // DisablePointerMethods: false
  237. // ContinueOnMethod: false
  238. func NewDefaultConfig() *ConfigState {
  239. return &ConfigState{Indent: " "}
  240. }