spew.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "fmt"
  19. "io"
  20. )
  21. // Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
  22. // passed with a default Formatter interface returned by NewFormatter. It
  23. // returns the formatted string as a value that satisfies error. See
  24. // NewFormatter for formatting details.
  25. //
  26. // This function is shorthand for the following syntax:
  27. //
  28. // fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b))
  29. func Errorf(format string, a ...interface{}) (err error) {
  30. return fmt.Errorf(format, convertArgs(a)...)
  31. }
  32. // Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
  33. // passed with a default Formatter interface returned by NewFormatter. It
  34. // returns the number of bytes written and any write error encountered. See
  35. // NewFormatter for formatting details.
  36. //
  37. // This function is shorthand for the following syntax:
  38. //
  39. // fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b))
  40. func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
  41. return fmt.Fprint(w, convertArgs(a)...)
  42. }
  43. // Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
  44. // passed with a default Formatter interface returned by NewFormatter. It
  45. // returns the number of bytes written and any write error encountered. See
  46. // NewFormatter for formatting details.
  47. //
  48. // This function is shorthand for the following syntax:
  49. //
  50. // fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b))
  51. func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
  52. return fmt.Fprintf(w, format, convertArgs(a)...)
  53. }
  54. // Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
  55. // passed with a default Formatter interface returned by NewFormatter. See
  56. // NewFormatter for formatting details.
  57. //
  58. // This function is shorthand for the following syntax:
  59. //
  60. // fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b))
  61. func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
  62. return fmt.Fprintln(w, convertArgs(a)...)
  63. }
  64. // Print is a wrapper for fmt.Print that treats each argument as if it were
  65. // passed with a default Formatter interface returned by NewFormatter. It
  66. // returns the number of bytes written and any write error encountered. See
  67. // NewFormatter for formatting details.
  68. //
  69. // This function is shorthand for the following syntax:
  70. //
  71. // fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b))
  72. func Print(a ...interface{}) (n int, err error) {
  73. return fmt.Print(convertArgs(a)...)
  74. }
  75. // Printf is a wrapper for fmt.Printf that treats each argument as if it were
  76. // passed with a default Formatter interface returned by NewFormatter. It
  77. // returns the number of bytes written and any write error encountered. See
  78. // NewFormatter for formatting details.
  79. //
  80. // This function is shorthand for the following syntax:
  81. //
  82. // fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b))
  83. func Printf(format string, a ...interface{}) (n int, err error) {
  84. return fmt.Printf(format, convertArgs(a)...)
  85. }
  86. // Println is a wrapper for fmt.Println that treats each argument as if it were
  87. // passed with a default Formatter interface returned by NewFormatter. It
  88. // returns the number of bytes written and any write error encountered. See
  89. // NewFormatter for formatting details.
  90. //
  91. // This function is shorthand for the following syntax:
  92. //
  93. // fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b))
  94. func Println(a ...interface{}) (n int, err error) {
  95. return fmt.Println(convertArgs(a)...)
  96. }
  97. // Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
  98. // passed with a default Formatter interface returned by NewFormatter. It
  99. // returns the resulting string. See NewFormatter for formatting details.
  100. //
  101. // This function is shorthand for the following syntax:
  102. //
  103. // fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b))
  104. func Sprint(a ...interface{}) string {
  105. return fmt.Sprint(convertArgs(a)...)
  106. }
  107. // Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
  108. // passed with a default Formatter interface returned by NewFormatter. It
  109. // returns the resulting string. See NewFormatter for formatting details.
  110. //
  111. // This function is shorthand for the following syntax:
  112. //
  113. // fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b))
  114. func Sprintf(format string, a ...interface{}) string {
  115. return fmt.Sprintf(format, convertArgs(a)...)
  116. }
  117. // Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
  118. // were passed with a default Formatter interface returned by NewFormatter. It
  119. // returns the resulting string. See NewFormatter for formatting details.
  120. //
  121. // This function is shorthand for the following syntax:
  122. //
  123. // fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b))
  124. func Sprintln(a ...interface{}) string {
  125. return fmt.Sprintln(convertArgs(a)...)
  126. }
  127. // convertArgs accepts a slice of arguments and returns a slice of the same
  128. // length with each argument converted to a default spew Formatter interface.
  129. func convertArgs(args []interface{}) (formatters []interface{}) {
  130. formatters = make([]interface{}, len(args))
  131. for index, arg := range args {
  132. formatters[index] = NewFormatter(arg)
  133. }
  134. return formatters
  135. }