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

@@ -31,6 +31,10 @@ type Chunk struct {
Equal []string
}
func (c *Chunk) empty() bool {
return len(c.Added) == 0 && len(c.Deleted) == 0 && len(c.Equal) == 0
}
// Diff returns a string containing a line-by-line unified diff of the linewise
// changes required to make A into B. Each line is prefixed with '+', '-', or
// ' ' to indicate if it should be added, removed, or is correct respectively.
@@ -58,76 +62,125 @@ func Diff(A, B string) string {
// DiffChunks uses an O(D(N+M)) shortest-edit-script algorithm
// to compute the edits required from A to B and returns the
// edit chunks.
func DiffChunks(A, B []string) []Chunk {
func DiffChunks(a, b []string) []Chunk {
// algorithm: http://www.xmailserver.org/diff2.pdf
N, M := len(A), len(B)
MAX := N + M
V := make([]int, 2*MAX+1)
Vs := make([][]int, 0, 8)
// We'll need these quantities a lot.
alen, blen := len(a), len(b) // M, N
var D int
// At most, it will require len(a) deletions and len(b) additions
// to transform a into b.
maxPath := alen + blen // MAX
if maxPath == 0 {
// degenerate case: two empty lists are the same
return nil
}
// Store the endpoint of the path for diagonals.
// We store only the a index, because the b index on any diagonal
// (which we know during the loop below) is aidx-diag.
// endpoint[maxPath] represents the 0 diagonal.
//
// Stated differently:
// endpoint[d] contains the aidx of a furthest reaching path in diagonal d
endpoint := make([]int, 2*maxPath+1) // V
saved := make([][]int, 0, 8) // Vs
save := func() {
dup := make([]int, len(endpoint))
copy(dup, endpoint)
saved = append(saved, dup)
}
var editDistance int // D
dLoop:
for D = 0; D <= MAX; D++ {
for k := -D; k <= D; k += 2 {
var x int
if k == -D || (k != D && V[MAX+k-1] < V[MAX+k+1]) {
x = V[MAX+k+1]
} else {
x = V[MAX+k-1] + 1
for editDistance = 0; editDistance <= maxPath; editDistance++ {
// The 0 diag(onal) represents equality of a and b. Each diagonal to
// the left is numbered one lower, to the right is one higher, from
// -alen to +blen. Negative diagonals favor differences from a,
// positive diagonals favor differences from b. The edit distance to a
// diagonal d cannot be shorter than d itself.
//
// The iterations of this loop cover either odds or evens, but not both,
// If odd indices are inputs, even indices are outputs and vice versa.
for diag := -editDistance; diag <= editDistance; diag += 2 { // k
var aidx int // x
switch {
case diag == -editDistance:
// This is a new diagonal; copy from previous iter
aidx = endpoint[maxPath-editDistance+1] + 0
case diag == editDistance:
// This is a new diagonal; copy from previous iter
aidx = endpoint[maxPath+editDistance-1] + 1
case endpoint[maxPath+diag+1] > endpoint[maxPath+diag-1]:
// diagonal d+1 was farther along, so use that
aidx = endpoint[maxPath+diag+1] + 0
default:
// diagonal d-1 was farther (or the same), so use that
aidx = endpoint[maxPath+diag-1] + 1
}
y := x - k
for x < N && y < M && A[x] == B[y] {
x++
y++
// On diagonal d, we can compute bidx from aidx.
bidx := aidx - diag // y
// See how far we can go on this diagonal before we find a difference.
for aidx < alen && bidx < blen && a[aidx] == b[bidx] {
aidx++
bidx++
}
V[MAX+k] = x
if x >= N && y >= M {
Vs = append(Vs, append(make([]int, 0, len(V)), V...))
// Store the end of the current edit chain.
endpoint[maxPath+diag] = aidx
// If we've found the end of both inputs, we're done!
if aidx >= alen && bidx >= blen {
save() // save the final path
break dLoop
}
}
Vs = append(Vs, append(make([]int, 0, len(V)), V...))
save() // save the current path
}
if D == 0 {
if editDistance == 0 {
return nil
}
chunks := make([]Chunk, D+1)
chunks := make([]Chunk, editDistance+1)
x, y := N, M
for d := D; d > 0; d-- {
V := Vs[d]
k := x - y
insert := k == -d || (k != d && V[MAX+k-1] < V[MAX+k+1])
x, y := alen, blen
for d := editDistance; d > 0; d-- {
endpoint := saved[d]
diag := x - y
insert := diag == -d || (diag != d && endpoint[maxPath+diag-1] < endpoint[maxPath+diag+1])
x1 := V[MAX+k]
x1 := endpoint[maxPath+diag]
var x0, xM, kk int
if insert {
kk = k + 1
x0 = V[MAX+kk]
kk = diag + 1
x0 = endpoint[maxPath+kk]
xM = x0
} else {
kk = k - 1
x0 = V[MAX+kk]
kk = diag - 1
x0 = endpoint[maxPath+kk]
xM = x0 + 1
}
y0 := x0 - kk
var c Chunk
if insert {
c.Added = B[y0:][:1]
c.Added = b[y0:][:1]
} else {
c.Deleted = A[x0:][:1]
c.Deleted = a[x0:][:1]
}
if xM < x1 {
c.Equal = A[xM:][:x1-xM]
c.Equal = a[xM:][:x1-xM]
}
x, y = x0, y0
chunks[d] = c
}
if x > 0 {
chunks[0].Equal = A[:x]
chunks[0].Equal = a[:x]
}
if chunks[0].empty() {
chunks = chunks[1:]
}
if len(chunks) == 0 {
return nil
}
return chunks
}

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))
}

View File

@@ -29,48 +29,164 @@ func isZeroVal(val reflect.Value) bool {
return reflect.DeepEqual(val.Interface(), z)
}
func (c *Config) val2node(val reflect.Value) node {
// TODO(kevlar): pointer tracking?
// pointerTracker is a helper for tracking pointer chasing to detect cycles.
type pointerTracker struct {
addrs map[uintptr]int // addr[address] = seen count
lastID int
ids map[uintptr]int // ids[address] = id
}
// track tracks following a reference (pointer, slice, map, etc). Every call to
// track should be paired with a call to untrack.
func (p *pointerTracker) track(ptr uintptr) {
if p.addrs == nil {
p.addrs = make(map[uintptr]int)
}
p.addrs[ptr]++
}
// untrack registers that we have backtracked over the reference to the pointer.
func (p *pointerTracker) untrack(ptr uintptr) {
p.addrs[ptr]--
if p.addrs[ptr] == 0 {
delete(p.addrs, ptr)
}
}
// seen returns whether the pointer was previously seen along this path.
func (p *pointerTracker) seen(ptr uintptr) bool {
_, ok := p.addrs[ptr]
return ok
}
// keep allocates an ID for the given address and returns it.
func (p *pointerTracker) keep(ptr uintptr) int {
if p.ids == nil {
p.ids = make(map[uintptr]int)
}
if _, ok := p.ids[ptr]; !ok {
p.lastID++
p.ids[ptr] = p.lastID
}
return p.ids[ptr]
}
// id returns the ID for the given address.
func (p *pointerTracker) id(ptr uintptr) (int, bool) {
if p.ids == nil {
p.ids = make(map[uintptr]int)
}
id, ok := p.ids[ptr]
return id, ok
}
// reflector adds local state to the recursive reflection logic.
type reflector struct {
*Config
*pointerTracker
}
// follow handles following a possiblly-recursive reference to the given value
// from the given ptr address.
func (r *reflector) follow(ptr uintptr, val reflect.Value) node {
if r.pointerTracker == nil {
// Tracking disabled
return r.val2node(val)
}
// If a parent already followed this, emit a reference marker
if r.seen(ptr) {
id := r.keep(ptr)
return ref{id}
}
// Track the pointer we're following while on this recursive branch
r.track(ptr)
defer r.untrack(ptr)
n := r.val2node(val)
// If the recursion used this ptr, wrap it with a target marker
if id, ok := r.id(ptr); ok {
return target{id, n}
}
// Otherwise, return the node unadulterated
return n
}
func (r *reflector) val2node(val reflect.Value) node {
if !val.IsValid() {
return rawVal("nil")
}
if val.CanInterface() {
v := val.Interface()
if s, ok := v.(fmt.Stringer); ok && c.PrintStringers {
return stringVal(s.String())
}
if t, ok := v.(encoding.TextMarshaler); ok && c.PrintTextMarshalers {
if raw, err := t.MarshalText(); err == nil { // if NOT an error
return stringVal(string(raw))
if formatter, ok := r.Formatter[val.Type()]; ok {
if formatter != nil {
res := reflect.ValueOf(formatter).Call([]reflect.Value{val})
return rawVal(res[0].Interface().(string))
}
} else {
if s, ok := v.(fmt.Stringer); ok && r.PrintStringers {
return stringVal(s.String())
}
if t, ok := v.(encoding.TextMarshaler); ok && r.PrintTextMarshalers {
if raw, err := t.MarshalText(); err == nil { // if NOT an error
return stringVal(string(raw))
}
}
}
}
switch kind := val.Kind(); kind {
case reflect.Ptr, reflect.Interface:
case reflect.Ptr:
if val.IsNil() {
return rawVal("nil")
}
return c.val2node(val.Elem())
return r.follow(val.Pointer(), val.Elem())
case reflect.Interface:
if val.IsNil() {
return rawVal("nil")
}
return r.val2node(val.Elem())
case reflect.String:
return stringVal(val.String())
case reflect.Slice, reflect.Array:
case reflect.Slice:
n := list{}
length := val.Len()
ptr := val.Pointer()
for i := 0; i < length; i++ {
n = append(n, r.follow(ptr, val.Index(i)))
}
return n
case reflect.Array:
n := list{}
length := val.Len()
for i := 0; i < length; i++ {
n = append(n, c.val2node(val.Index(i)))
n = append(n, r.val2node(val.Index(i)))
}
return n
case reflect.Map:
n := keyvals{}
// Extract the keys and sort them for stable iteration
keys := val.MapKeys()
pairs := make([]mapPair, 0, len(keys))
for _, key := range keys {
// TODO(kevlar): Support arbitrary type keys?
n = append(n, keyval{compactString(c.val2node(key)), c.val2node(val.MapIndex(key))})
pairs = append(pairs, mapPair{
key: new(formatter).compactString(r.val2node(key)), // can't be cyclic
value: val.MapIndex(key),
})
}
sort.Sort(byKey(pairs))
// Process the keys into the final representation
ptr, n := val.Pointer(), keyvals{}
for _, pair := range pairs {
n = append(n, keyval{
key: pair.key,
val: r.follow(ptr, pair.value),
})
}
sort.Sort(n)
return n
case reflect.Struct:
n := keyvals{}
@@ -78,14 +194,14 @@ func (c *Config) val2node(val reflect.Value) node {
fields := typ.NumField()
for i := 0; i < fields; i++ {
sf := typ.Field(i)
if !c.IncludeUnexported && sf.PkgPath != "" {
if !r.IncludeUnexported && sf.PkgPath != "" {
continue
}
field := val.Field(i)
if c.SkipZeroFields && isZeroVal(field) {
if r.SkipZeroFields && isZeroVal(field) {
continue
}
n = append(n, keyval{sf.Name, c.val2node(field)})
n = append(n, keyval{sf.Name, r.val2node(field)})
}
return n
case reflect.Bool:
@@ -112,3 +228,14 @@ func (c *Config) val2node(val reflect.Value) node {
return rawVal(val.String())
}
type mapPair struct {
key string
value reflect.Value
}
type byKey []mapPair
func (v byKey) Len() int { return len(v) }
func (v byKey) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func (v byKey) Less(i, j int) bool { return v[i].key < v[j].key }

View File

@@ -15,16 +15,56 @@
package pretty
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
)
type node interface {
WriteTo(w *bytes.Buffer, indent string, cfg *Config)
// a formatter stores stateful formatting information as well as being
// an io.Writer for simplicity.
type formatter struct {
*bufio.Writer
*Config
// Self-referential structure tracking
tagNumbers map[int]int // tagNumbers[id] = <#n>
}
func compactString(n node) string {
// newFormatter creates a new buffered formatter. For the output to be written
// to the given writer, this must be accompanied by a call to write (or Flush).
func newFormatter(cfg *Config, w io.Writer) *formatter {
return &formatter{
Writer: bufio.NewWriter(w),
Config: cfg,
tagNumbers: make(map[int]int),
}
}
func (f *formatter) write(n node) {
defer f.Flush()
n.format(f, "")
}
func (f *formatter) tagFor(id int) int {
if tag, ok := f.tagNumbers[id]; ok {
return tag
}
if f.tagNumbers == nil {
return 0
}
tag := len(f.tagNumbers) + 1
f.tagNumbers[id] = tag
return tag
}
type node interface {
format(f *formatter, indent string)
}
func (f *formatter) compactString(n node) string {
switch k := n.(type) {
case stringVal:
return string(k)
@@ -33,20 +73,22 @@ func compactString(n node) string {
}
buf := new(bytes.Buffer)
n.WriteTo(buf, "", &Config{Compact: true})
f2 := newFormatter(&Config{Compact: true}, buf)
f2.tagNumbers = f.tagNumbers // reuse tagNumbers just in case
f2.write(n)
return buf.String()
}
type stringVal string
func (str stringVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteString(strconv.Quote(string(str)))
func (str stringVal) format(f *formatter, indent string) {
f.WriteString(strconv.Quote(string(str)))
}
type rawVal string
func (r rawVal) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteString(string(r))
func (r rawVal) format(f *formatter, indent string) {
f.WriteString(string(r))
}
type keyval struct {
@@ -56,36 +98,32 @@ type keyval struct {
type keyvals []keyval
func (l keyvals) Len() int { return len(l) }
func (l keyvals) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l keyvals) Less(i, j int) bool { return l[i].key < l[j].key }
func (l keyvals) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
w.WriteByte('{')
func (l keyvals) format(f *formatter, indent string) {
f.WriteByte('{')
switch {
case cfg.Compact:
case f.Compact:
// All on one line:
for i, kv := range l {
if i > 0 {
w.WriteByte(',')
f.WriteByte(',')
}
w.WriteString(kv.key)
w.WriteByte(':')
kv.val.WriteTo(w, indent, cfg)
f.WriteString(kv.key)
f.WriteByte(':')
kv.val.format(f, indent)
}
case cfg.Diffable:
w.WriteByte('\n')
case f.Diffable:
f.WriteByte('\n')
inner := indent + " "
// Each value gets its own line:
for _, kv := range l {
w.WriteString(inner)
w.WriteString(kv.key)
w.WriteString(": ")
kv.val.WriteTo(w, inner, cfg)
w.WriteString(",\n")
f.WriteString(inner)
f.WriteString(kv.key)
f.WriteString(": ")
kv.val.format(f, inner)
f.WriteString(",\n")
}
w.WriteString(indent)
f.WriteString(indent)
default:
keyWidth := 0
for _, kv := range l {
@@ -99,62 +137,87 @@ func (l keyvals) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
// First and last line shared with bracket:
for i, kv := range l {
if i > 0 {
w.WriteString(",\n")
w.WriteString(alignKey)
f.WriteString(",\n")
f.WriteString(alignKey)
}
w.WriteString(kv.key)
w.WriteString(": ")
w.WriteString(alignValue[len(kv.key):])
kv.val.WriteTo(w, inner, cfg)
f.WriteString(kv.key)
f.WriteString(": ")
f.WriteString(alignValue[len(kv.key):])
kv.val.format(f, inner)
}
}
w.WriteByte('}')
f.WriteByte('}')
}
type list []node
func (l list) WriteTo(w *bytes.Buffer, indent string, cfg *Config) {
if max := cfg.ShortList; max > 0 {
short := compactString(l)
func (l list) format(f *formatter, indent string) {
if max := f.ShortList; max > 0 {
short := f.compactString(l)
if len(short) <= max {
w.WriteString(short)
f.WriteString(short)
return
}
}
w.WriteByte('[')
f.WriteByte('[')
switch {
case cfg.Compact:
case f.Compact:
// All on one line:
for i, v := range l {
if i > 0 {
w.WriteByte(',')
f.WriteByte(',')
}
v.WriteTo(w, indent, cfg)
v.format(f, indent)
}
case cfg.Diffable:
w.WriteByte('\n')
case f.Diffable:
f.WriteByte('\n')
inner := indent + " "
// Each value gets its own line:
for _, v := range l {
w.WriteString(inner)
v.WriteTo(w, inner, cfg)
w.WriteString(",\n")
f.WriteString(inner)
v.format(f, inner)
f.WriteString(",\n")
}
w.WriteString(indent)
f.WriteString(indent)
default:
inner := indent + " "
// First and last line shared with bracket:
for i, v := range l {
if i > 0 {
w.WriteString(",\n")
w.WriteString(inner)
f.WriteString(",\n")
f.WriteString(inner)
}
v.WriteTo(w, inner, cfg)
v.format(f, inner)
}
}
w.WriteByte(']')
f.WriteByte(']')
}
type ref struct {
id int
}
func (r ref) format(f *formatter, indent string) {
fmt.Fprintf(f, "<see #%d>", f.tagFor(r.id))
}
type target struct {
id int
value node
}
func (t target) format(f *formatter, indent string) {
tag := fmt.Sprintf("<#%d> ", f.tagFor(t.id))
switch {
case f.Diffable, f.Compact:
// no indent changes
default:
indent += strings.Repeat(" ", len(tag))
}
f.WriteString(tag)
t.value.format(f, indent)
}