run 'go get -u; make revendor'

Signed-off-by: Stephan Renatus <srenatus@chef.io>
This commit is contained in:
Stephan Renatus
2019-07-31 08:09:38 +02:00
parent 7c1b4b3005
commit 076cd77469
975 changed files with 347835 additions and 77390 deletions

View File

@@ -18,7 +18,9 @@ import (
"bytes"
"fmt"
"io"
"net"
"reflect"
"time"
"github.com/kylelemons/godebug/diff"
)
@@ -40,26 +42,80 @@ type Config struct {
// Output transforms
ShortList int // Maximum character length for short lists if nonzero.
// Type-specific overrides
//
// Formatter maps a type to a function that will provide a one-line string
// representation of the input value. Conceptually:
// Formatter[reflect.TypeOf(v)](v) = "v as a string"
//
// Note that the first argument need not explicitly match the type, it must
// merely be callable with it.
//
// When processing an input value, if its type exists as a key in Formatter:
// 1) If the value is nil, no stringification is performed.
// This allows overriding of PrintStringers and PrintTextMarshalers.
// 2) The value will be called with the input as its only argument.
// The function must return a string as its first return value.
//
// In addition to func literals, two common values for this will be:
// fmt.Sprint (function) func Sprint(...interface{}) string
// Type.String (method) func (Type) String() string
//
// Note that neither of these work if the String method is a pointer
// method and the input will be provided as a value. In that case,
// use a function that calls .String on the formal value parameter.
Formatter map[reflect.Type]interface{}
// If TrackCycles is enabled, pretty will detect and track
// self-referential structures. If a self-referential structure (aka a
// "recursive" value) is detected, numbered placeholders will be emitted.
//
// Pointer tracking is disabled by default for performance reasons.
TrackCycles bool
}
// Default Config objects
var (
// DefaultFormatter is the default set of overrides for stringification.
DefaultFormatter = map[reflect.Type]interface{}{
reflect.TypeOf(time.Time{}): fmt.Sprint,
reflect.TypeOf(net.IP{}): fmt.Sprint,
reflect.TypeOf((*error)(nil)).Elem(): fmt.Sprint,
}
// CompareConfig is the default configuration used for Compare.
CompareConfig = &Config{
Diffable: true,
IncludeUnexported: true,
Formatter: DefaultFormatter,
}
// DefaultConfig is the default configuration used for all other top-level functions.
DefaultConfig = &Config{}
DefaultConfig = &Config{
Formatter: DefaultFormatter,
}
// CycleTracker is a convenience config for formatting and comparing recursive structures.
CycleTracker = &Config{
Diffable: true,
Formatter: DefaultFormatter,
TrackCycles: true,
}
)
func (cfg *Config) fprint(buf *bytes.Buffer, vals ...interface{}) {
ref := &reflector{
Config: cfg,
}
if cfg.TrackCycles {
ref.pointerTracker = new(pointerTracker)
}
for i, val := range vals {
if i > 0 {
buf.WriteByte('\n')
}
cfg.val2node(reflect.ValueOf(val)).WriteTo(buf, "", cfg)
newFormatter(cfg, buf).write(ref.val2node(reflect.ValueOf(val)))
}
}
@@ -98,19 +154,35 @@ func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error)
}
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want, using the CompareConfig.
// values in a and b, using the CompareConfig.
//
// Each line in the output is prefixed with '+', '-', or ' ' to indicate if it
// should be added to, removed from, or is correct for the "got" value with
// respect to the "want" value.
func Compare(got, want interface{}) string {
return CompareConfig.Compare(got, want)
// Each line in the output is prefixed with '+', '-', or ' ' to indicate which
// side it's from. Lines from the a side are marked with '-', lines from the
// b side are marked with '+' and lines that are the same on both sides are
// marked with ' '.
//
// The comparison is based on the intentionally-untyped output of Print, and as
// such this comparison is pretty forviving. In particular, if the types of or
// types within in a and b are different but have the same representation,
// Compare will not indicate any differences between them.
func Compare(a, b interface{}) string {
return CompareConfig.Compare(a, b)
}
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want according to the cfg.
func (cfg *Config) Compare(got, want interface{}) string {
//
// Each line in the output is prefixed with '+', '-', or ' ' to indicate which
// side it's from. Lines from the a side are marked with '-', lines from the
// b side are marked with '+' and lines that are the same on both sides are
// marked with ' '.
//
// The comparison is based on the intentionally-untyped output of Print, and as
// such this comparison is pretty forviving. In particular, if the types of or
// types within in a and b are different but have the same representation,
// Compare will not indicate any differences between them.
func (cfg *Config) Compare(a, b interface{}) string {
diffCfg := *cfg
diffCfg.Diffable = true
return diff.Diff(cfg.Sprint(got), cfg.Sprint(want))
return diff.Diff(cfg.Sprint(a), cfg.Sprint(b))
}