config.go 9.1 KB

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