public.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2013 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pretty
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "net"
  20. "reflect"
  21. "time"
  22. "github.com/kylelemons/godebug/diff"
  23. )
  24. // A Config represents optional configuration parameters for formatting.
  25. //
  26. // Some options, notably ShortList, dramatically increase the overhead
  27. // of pretty-printing a value.
  28. type Config struct {
  29. // Verbosity options
  30. Compact bool // One-line output. Overrides Diffable.
  31. Diffable bool // Adds extra newlines for more easily diffable output.
  32. // Field and value options
  33. IncludeUnexported bool // Include unexported fields in output
  34. PrintStringers bool // Call String on a fmt.Stringer
  35. PrintTextMarshalers bool // Call MarshalText on an encoding.TextMarshaler
  36. SkipZeroFields bool // Skip struct fields that have a zero value.
  37. // Output transforms
  38. ShortList int // Maximum character length for short lists if nonzero.
  39. // Type-specific overrides
  40. //
  41. // Formatter maps a type to a function that will provide a one-line string
  42. // representation of the input value. Conceptually:
  43. // Formatter[reflect.TypeOf(v)](v) = "v as a string"
  44. //
  45. // Note that the first argument need not explicitly match the type, it must
  46. // merely be callable with it.
  47. //
  48. // When processing an input value, if its type exists as a key in Formatter:
  49. // 1) If the value is nil, no stringification is performed.
  50. // This allows overriding of PrintStringers and PrintTextMarshalers.
  51. // 2) The value will be called with the input as its only argument.
  52. // The function must return a string as its first return value.
  53. //
  54. // In addition to func literals, two common values for this will be:
  55. // fmt.Sprint (function) func Sprint(...interface{}) string
  56. // Type.String (method) func (Type) String() string
  57. //
  58. // Note that neither of these work if the String method is a pointer
  59. // method and the input will be provided as a value. In that case,
  60. // use a function that calls .String on the formal value parameter.
  61. Formatter map[reflect.Type]interface{}
  62. }
  63. // Default Config objects
  64. var (
  65. // DefaultFormatter is the default set of overrides for stringification.
  66. DefaultFormatter = map[reflect.Type]interface{}{
  67. reflect.TypeOf(time.Time{}): fmt.Sprint,
  68. reflect.TypeOf(net.IP{}): fmt.Sprint,
  69. reflect.TypeOf((*error)(nil)).Elem(): fmt.Sprint,
  70. }
  71. // CompareConfig is the default configuration used for Compare.
  72. CompareConfig = &Config{
  73. Diffable: true,
  74. IncludeUnexported: true,
  75. Formatter: DefaultFormatter,
  76. }
  77. // DefaultConfig is the default configuration used for all other top-level functions.
  78. DefaultConfig = &Config{
  79. Formatter: DefaultFormatter,
  80. }
  81. )
  82. func (cfg *Config) fprint(buf *bytes.Buffer, vals ...interface{}) {
  83. for i, val := range vals {
  84. if i > 0 {
  85. buf.WriteByte('\n')
  86. }
  87. cfg.val2node(reflect.ValueOf(val)).WriteTo(buf, "", cfg)
  88. }
  89. }
  90. // Print writes the DefaultConfig representation of the given values to standard output.
  91. func Print(vals ...interface{}) {
  92. DefaultConfig.Print(vals...)
  93. }
  94. // Print writes the configured presentation of the given values to standard output.
  95. func (cfg *Config) Print(vals ...interface{}) {
  96. fmt.Println(cfg.Sprint(vals...))
  97. }
  98. // Sprint returns a string representation of the given value according to the DefaultConfig.
  99. func Sprint(vals ...interface{}) string {
  100. return DefaultConfig.Sprint(vals...)
  101. }
  102. // Sprint returns a string representation of the given value according to cfg.
  103. func (cfg *Config) Sprint(vals ...interface{}) string {
  104. buf := new(bytes.Buffer)
  105. cfg.fprint(buf, vals...)
  106. return buf.String()
  107. }
  108. // Fprint writes the representation of the given value to the writer according to the DefaultConfig.
  109. func Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
  110. return DefaultConfig.Fprint(w, vals...)
  111. }
  112. // Fprint writes the representation of the given value to the writer according to the cfg.
  113. func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error) {
  114. buf := new(bytes.Buffer)
  115. cfg.fprint(buf, vals...)
  116. return buf.WriteTo(w)
  117. }
  118. // Compare returns a string containing a line-by-line unified diff of the
  119. // values in a and b, using the CompareConfig.
  120. //
  121. // Each line in the output is prefixed with '+', '-', or ' ' to indicate which
  122. // side it's from. Lines from the a side are marked with '-', lines from the
  123. // b side are marked with '+' and lines that are the same on both sides are
  124. // marked with ' '.
  125. func Compare(a, b interface{}) string {
  126. return CompareConfig.Compare(a, b)
  127. }
  128. // Compare returns a string containing a line-by-line unified diff of the
  129. // values in got and want according to the cfg.
  130. func (cfg *Config) Compare(a, b interface{}) string {
  131. diffCfg := *cfg
  132. diffCfg.Diffable = true
  133. return diff.Diff(cfg.Sprint(a), cfg.Sprint(b))
  134. }