2016-03-06 20:32:10 +00:00
|
|
|
package messagediff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-09-13 19:50:14 +00:00
|
|
|
"unsafe"
|
2016-03-06 20:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrettyDiff does a deep comparison and returns the nicely formated results.
|
|
|
|
func PrettyDiff(a, b interface{}) (string, bool) {
|
|
|
|
d, equal := DeepDiff(a, b)
|
|
|
|
var dstr []string
|
|
|
|
for path, added := range d.Added {
|
|
|
|
dstr = append(dstr, fmt.Sprintf("added: %s = %#v\n", path.String(), added))
|
|
|
|
}
|
|
|
|
for path, removed := range d.Removed {
|
|
|
|
dstr = append(dstr, fmt.Sprintf("removed: %s = %#v\n", path.String(), removed))
|
|
|
|
}
|
|
|
|
for path, modified := range d.Modified {
|
|
|
|
dstr = append(dstr, fmt.Sprintf("modified: %s = %#v\n", path.String(), modified))
|
|
|
|
}
|
|
|
|
sort.Strings(dstr)
|
|
|
|
return strings.Join(dstr, ""), equal
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepDiff does a deep comparison and returns the results.
|
|
|
|
func DeepDiff(a, b interface{}) (*Diff, bool) {
|
2016-09-13 19:50:14 +00:00
|
|
|
d := newDiff()
|
|
|
|
return d, d.diff(reflect.ValueOf(a), reflect.ValueOf(b), nil)
|
2016-03-06 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 19:50:14 +00:00
|
|
|
func newDiff() *Diff {
|
2016-03-06 20:32:10 +00:00
|
|
|
return &Diff{
|
|
|
|
Added: make(map[*Path]interface{}),
|
|
|
|
Removed: make(map[*Path]interface{}),
|
|
|
|
Modified: make(map[*Path]interface{}),
|
2016-09-13 19:50:14 +00:00
|
|
|
visited: make(map[visit]bool),
|
2016-03-06 20:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:50:14 +00:00
|
|
|
func (d *Diff) diff(aVal, bVal reflect.Value, path Path) bool {
|
2017-04-05 14:34:41 +00:00
|
|
|
// The array underlying `path` could be modified in subsequent
|
|
|
|
// calls. Make sure we have a local copy.
|
|
|
|
localPath := make(Path, len(path))
|
|
|
|
copy(localPath, path)
|
|
|
|
|
2016-09-13 19:50:14 +00:00
|
|
|
// Validity checks. Should only trigger if nil is one of the original arguments.
|
2016-03-06 20:32:10 +00:00
|
|
|
if !aVal.IsValid() && !bVal.IsValid() {
|
|
|
|
return true
|
|
|
|
}
|
2016-09-13 19:50:14 +00:00
|
|
|
if !bVal.IsValid() {
|
2017-04-05 14:34:41 +00:00
|
|
|
d.Modified[&localPath] = nil
|
2016-09-13 19:50:14 +00:00
|
|
|
return false
|
|
|
|
} else if !aVal.IsValid() {
|
2017-04-05 14:34:41 +00:00
|
|
|
d.Modified[&localPath] = bVal.Interface()
|
2016-03-06 20:32:10 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-09-13 19:50:14 +00:00
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
if aVal.Type() != bVal.Type() {
|
2017-04-05 14:34:41 +00:00
|
|
|
d.Modified[&localPath] = bVal.Interface()
|
2016-03-06 20:32:10 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-09-13 19:50:14 +00:00
|
|
|
kind := aVal.Kind()
|
|
|
|
|
|
|
|
// Borrowed from the reflect package to handle recursive data structures.
|
|
|
|
hard := func(k reflect.Kind) bool {
|
|
|
|
switch k {
|
|
|
|
case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if aVal.CanAddr() && bVal.CanAddr() && hard(kind) {
|
|
|
|
addr1 := unsafe.Pointer(aVal.UnsafeAddr())
|
|
|
|
addr2 := unsafe.Pointer(bVal.UnsafeAddr())
|
|
|
|
if uintptr(addr1) > uintptr(addr2) {
|
|
|
|
// Canonicalize order to reduce number of entries in visited.
|
|
|
|
// Assumes non-moving garbage collector.
|
|
|
|
addr1, addr2 = addr2, addr1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Short circuit if references are already seen.
|
|
|
|
typ := aVal.Type()
|
|
|
|
v := visit{addr1, addr2, typ}
|
|
|
|
if d.visited[v] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember for later.
|
|
|
|
d.visited[v] = true
|
|
|
|
}
|
|
|
|
// End of borrowed code.
|
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
equal := true
|
2016-09-13 19:50:14 +00:00
|
|
|
switch kind {
|
|
|
|
case reflect.Map, reflect.Ptr, reflect.Func, reflect.Chan, reflect.Slice:
|
|
|
|
if aVal.IsNil() && bVal.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if aVal.IsNil() || bVal.IsNil() {
|
2017-04-05 14:34:41 +00:00
|
|
|
d.Modified[&localPath] = bVal.Interface()
|
2016-09-13 19:50:14 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
switch kind {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
aLen := aVal.Len()
|
|
|
|
bLen := bVal.Len()
|
|
|
|
for i := 0; i < min(aLen, bLen); i++ {
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, SliceIndex(i))
|
2016-09-13 19:50:14 +00:00
|
|
|
if eq := d.diff(aVal.Index(i), bVal.Index(i), localPath); !eq {
|
2016-03-06 20:32:10 +00:00
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aLen > bLen {
|
|
|
|
for i := bLen; i < aLen; i++ {
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, SliceIndex(i))
|
2016-03-06 20:32:10 +00:00
|
|
|
d.Removed[&localPath] = aVal.Index(i).Interface()
|
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
} else if aLen < bLen {
|
|
|
|
for i := aLen; i < bLen; i++ {
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, SliceIndex(i))
|
2016-03-06 20:32:10 +00:00
|
|
|
d.Added[&localPath] = bVal.Index(i).Interface()
|
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
for _, key := range aVal.MapKeys() {
|
|
|
|
aI := aVal.MapIndex(key)
|
|
|
|
bI := bVal.MapIndex(key)
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, MapKey{key.Interface()})
|
2016-03-06 20:32:10 +00:00
|
|
|
if !bI.IsValid() {
|
|
|
|
d.Removed[&localPath] = aI.Interface()
|
|
|
|
equal = false
|
2016-09-13 19:50:14 +00:00
|
|
|
} else if eq := d.diff(aI, bI, localPath); !eq {
|
2016-03-06 20:32:10 +00:00
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, key := range bVal.MapKeys() {
|
|
|
|
aI := aVal.MapIndex(key)
|
|
|
|
if !aI.IsValid() {
|
|
|
|
bI := bVal.MapIndex(key)
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, MapKey{key.Interface()})
|
2016-03-06 20:32:10 +00:00
|
|
|
d.Added[&localPath] = bI.Interface()
|
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Struct:
|
|
|
|
typ := aVal.Type()
|
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
|
|
index := []int{i}
|
|
|
|
field := typ.FieldByIndex(index)
|
2017-04-05 14:34:41 +00:00
|
|
|
localPath := append(localPath, StructField(field.Name))
|
2016-09-13 19:50:14 +00:00
|
|
|
aI := unsafeReflectValue(aVal.FieldByIndex(index))
|
|
|
|
bI := unsafeReflectValue(bVal.FieldByIndex(index))
|
|
|
|
if eq := d.diff(aI, bI, localPath); !eq {
|
2016-03-06 20:32:10 +00:00
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
2017-04-05 14:34:41 +00:00
|
|
|
equal = d.diff(aVal.Elem(), bVal.Elem(), localPath)
|
2016-03-06 20:32:10 +00:00
|
|
|
default:
|
2016-09-13 19:50:14 +00:00
|
|
|
if reflect.DeepEqual(aVal.Interface(), bVal.Interface()) {
|
2016-03-06 20:32:10 +00:00
|
|
|
equal = true
|
|
|
|
} else {
|
2017-04-05 14:34:41 +00:00
|
|
|
d.Modified[&localPath] = bVal.Interface()
|
2016-03-06 20:32:10 +00:00
|
|
|
equal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:50:14 +00:00
|
|
|
// During deepValueEqual, must keep track of checks that are
|
|
|
|
// in progress. The comparison algorithm assumes that all
|
|
|
|
// checks in progress are true when it reencounters them.
|
|
|
|
// Visited comparisons are stored in a map indexed by visit.
|
|
|
|
// This is borrowed from the reflect package.
|
|
|
|
type visit struct {
|
|
|
|
a1 unsafe.Pointer
|
|
|
|
a2 unsafe.Pointer
|
|
|
|
typ reflect.Type
|
|
|
|
}
|
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
// Diff represents a change in a struct.
|
|
|
|
type Diff struct {
|
|
|
|
Added, Removed, Modified map[*Path]interface{}
|
2016-09-13 19:50:14 +00:00
|
|
|
visited map[visit]bool
|
2016-03-06 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Path represents a path to a changed datum.
|
|
|
|
type Path []PathNode
|
|
|
|
|
|
|
|
func (p Path) String() string {
|
|
|
|
var out string
|
|
|
|
for _, n := range p {
|
|
|
|
out += n.String()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathNode represents one step in the path.
|
|
|
|
type PathNode interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// StructField is a path element representing a field of a struct.
|
|
|
|
type StructField string
|
|
|
|
|
|
|
|
func (n StructField) String() string {
|
|
|
|
return fmt.Sprintf(".%s", string(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MapKey is a path element representing a key of a map.
|
|
|
|
type MapKey struct {
|
|
|
|
Key interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n MapKey) String() string {
|
|
|
|
return fmt.Sprintf("[%#v]", n.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SliceIndex is a path element representing a index of a slice.
|
|
|
|
type SliceIndex int
|
|
|
|
|
|
|
|
func (n SliceIndex) String() string {
|
|
|
|
return fmt.Sprintf("[%d]", n)
|
|
|
|
}
|