config.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 recursion should stop once
  60. //a Stringer or an error interface is encountered.
  61. //
  62. //It defaults to false, meaning that it does not pretty-print
  63. //the internals of Stringers or errors.
  64. ContinueOnMethod bool
  65. }
  66. // Config is the active configuration of the top-level functions.
  67. // The configuration can be changed by modifying the contents of spew.Config.
  68. var Config ConfigState = ConfigState{Indent: " "}
  69. // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
  70. // passed with a Formatter interface returned by c.NewFormatter. It returns
  71. // the formatted string as a value that satisfies error. See NewFormatter
  72. // for formatting details.
  73. //
  74. // This function is shorthand for the following syntax:
  75. //
  76. // fmt.Errorf(format, c.NewFormatter(a), c.NewFormatter(b))
  77. func (c *ConfigState) Errorf(format string, a ...interface{}) (err error) {
  78. return fmt.Errorf(format, c.convertArgs(a)...)
  79. }
  80. // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
  81. // passed with a Formatter interface returned by c.NewFormatter. It returns
  82. // the number of bytes written and any write error encountered. See
  83. // NewFormatter for formatting details.
  84. //
  85. // This function is shorthand for the following syntax:
  86. //
  87. // fmt.Fprint(w, c.NewFormatter(a), c.NewFormatter(b))
  88. func (c *ConfigState) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
  89. return fmt.Fprint(w, c.convertArgs(a)...)
  90. }
  91. // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
  92. // passed with a Formatter interface returned by c.NewFormatter. It returns
  93. // the number of bytes written and any write error encountered. See
  94. // NewFormatter for formatting details.
  95. //
  96. // This function is shorthand for the following syntax:
  97. //
  98. // fmt.Fprintf(w, format, c.NewFormatter(a), c.NewFormatter(b))
  99. func (c *ConfigState) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
  100. return fmt.Fprintf(w, format, c.convertArgs(a)...)
  101. }
  102. // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
  103. // passed with a Formatter interface returned by c.NewFormatter. See
  104. // NewFormatter for formatting details.
  105. //
  106. // This function is shorthand for the following syntax:
  107. //
  108. // fmt.Fprintln(w, c.NewFormatter(a), c.NewFormatter(b))
  109. func (c *ConfigState) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
  110. return fmt.Fprintln(w, c.convertArgs(a)...)
  111. }
  112. // Print is a wrapper for fmt.Print that treats each argument as if it were
  113. // passed with a Formatter interface returned by c.NewFormatter. It returns
  114. // the number of bytes written and any write error encountered. See
  115. // NewFormatter for formatting details.
  116. //
  117. // This function is shorthand for the following syntax:
  118. //
  119. // fmt.Print(c.NewFormatter(a), c.NewFormatter(b))
  120. func (c *ConfigState) Print(a ...interface{}) (n int, err error) {
  121. return fmt.Print(c.convertArgs(a)...)
  122. }
  123. // Printf is a wrapper for fmt.Printf that treats each argument as if it were
  124. // passed with a Formatter interface returned by c.NewFormatter. It returns
  125. // the number of bytes written and any write error encountered. See
  126. // NewFormatter for formatting details.
  127. //
  128. // This function is shorthand for the following syntax:
  129. //
  130. // fmt.Printf(format, c.NewFormatter(a), c.NewFormatter(b))
  131. func (c *ConfigState) Printf(format string, a ...interface{}) (n int, err error) {
  132. return fmt.Printf(format, c.convertArgs(a)...)
  133. }
  134. // Println is a wrapper for fmt.Println that treats each argument as if it were
  135. // passed with a Formatter interface returned by c.NewFormatter. It returns
  136. // the number of bytes written and any write error encountered. See
  137. // NewFormatter for formatting details.
  138. //
  139. // This function is shorthand for the following syntax:
  140. //
  141. // fmt.Println(c.NewFormatter(a), c.NewFormatter(b))
  142. func (c *ConfigState) Println(a ...interface{}) (n int, err error) {
  143. return fmt.Println(c.convertArgs(a)...)
  144. }
  145. /*
  146. NewFormatter returns a custom formatter that satisfies the fmt.Formatter
  147. interface. As a result, it integrates cleanly with standard fmt package
  148. printing functions. The formatter is useful for inline printing of smaller data
  149. types similar to the standard %v format specifier.
  150. The custom formatter only responds to the %v (most compact), %+v (adds pointer
  151. addresses), %#v (adds types), and %#+v (adds types and pointer addresses) verb
  152. combinations. Any other verbs such as %x and %q will be sent to the the
  153. standard fmt package for formatting. In addition, the custom formatter ignores
  154. the width and precision arguments (however they will still work on the format
  155. specifiers not handled by the custom formatter).
  156. Typically this function shouldn't be called directly. It is much easier to make
  157. use of the custom formatter by calling one of the convenience functions such as
  158. c.Printf, c.Println, or c.Printf.
  159. */
  160. func (c *ConfigState) NewFormatter(v interface{}) fmt.Formatter {
  161. return newFormatter(c, v)
  162. }
  163. // Fdump formats and displays the passed arguments to io.Writer w. It formats
  164. // exactly the same as Dump.
  165. func (c *ConfigState) Fdump(w io.Writer, a ...interface{}) {
  166. fdump(c, w, a...)
  167. }
  168. /*
  169. Dump displays the passed parameters to standard out with newlines, customizable
  170. indentation, and additional debug information such as complete types and all
  171. pointer addresses used to indirect to the final value. It provides the
  172. following features over the built-in printing facilities provided by the fmt
  173. package:
  174. * Pointers are dereferenced and followed
  175. * Circular data structures are detected and handled properly
  176. * Custom Stringer/error interfaces are optionally invoked, including
  177. on unexported types
  178. * Custom types which only implement the Stringer/error interfaces via
  179. a pointer receiver are optionally invoked when passing non-pointer
  180. variables
  181. The configuration options are controlled by modifying the public members
  182. of c. See ConfigState for options documentation.
  183. See Fdump if you would prefer dumping to an arbitrary io.Writer.
  184. */
  185. func (c *ConfigState) Dump(a ...interface{}) {
  186. fdump(c, os.Stdout, a...)
  187. }
  188. // convertArgs accepts a slice of arguments and returns a slice of the same
  189. // length with each argument converted to a spew Formatter interface using
  190. // the ConfigState associated with s.
  191. func (c *ConfigState) convertArgs(args []interface{}) (formatters []interface{}) {
  192. formatters = make([]interface{}, len(args))
  193. for index, arg := range args {
  194. formatters[index] = newFormatter(c, arg)
  195. }
  196. return formatters
  197. }
  198. // NewDefaultConfig returns a ConfigState with the following default settings.
  199. //
  200. // Indent: " "
  201. // MaxDepth: 0
  202. // DisableMethods: false
  203. // DisablePointerMethods: false
  204. func NewDefaultConfig() *ConfigState {
  205. return &ConfigState{Indent: " "}
  206. }