Add new features to DiffReport and Diff (#48788)
* simplify String for Diff * add IsAddOperation and IsDeleteOperation to Diff * add method Paths to DiffReport
This commit is contained in:
@@ -78,6 +78,15 @@ func (r DiffReport) String() string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Paths returns the slice of paths of the current DiffReport
|
||||
func (r DiffReport) Paths() []string {
|
||||
var result = make([]string, len(r))
|
||||
for _, diff := range r {
|
||||
result = append(result, diff.Path)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type Diff struct {
|
||||
// Path to the field that has difference separated by period. Array index and key are designated by square brackets.
|
||||
// For example, Annotations[12345].Data.Fields[0].ID
|
||||
@@ -87,15 +96,34 @@ type Diff struct {
|
||||
}
|
||||
|
||||
func (d *Diff) String() string {
|
||||
left := d.Left.String()
|
||||
return fmt.Sprintf("%v:\n\t-: %+v\n\t+: %+v\n", d.Path, describeReflectValue(d.Left), describeReflectValue(d.Right))
|
||||
}
|
||||
|
||||
func describeReflectValue(v reflect.Value) interface{} {
|
||||
// invalid reflect.Value is produced when two collections (slices\maps) are compared and one misses value.
|
||||
// This way go-cmp indicates that an element was added\removed from a list.
|
||||
if !d.Left.IsValid() {
|
||||
left = "<none>"
|
||||
if !v.IsValid() {
|
||||
return "<none>"
|
||||
}
|
||||
right := d.Right.String()
|
||||
if !d.Right.IsValid() {
|
||||
right = "<none>"
|
||||
}
|
||||
return fmt.Sprintf("%v:\n\t-: %+v\n\t+: %+v", d.Path, left, right)
|
||||
return v
|
||||
}
|
||||
|
||||
// IsAddOperation returns true when
|
||||
// - Left does not have value and Right has
|
||||
// - the kind of Left and Right is either reflect.Slice or reflect.Map and the length of Left is less than length of Right
|
||||
// In all other cases it returns false.
|
||||
// NOTE: this is applicable to diff of Maps and Slices only
|
||||
func (d *Diff) IsAddOperation() bool {
|
||||
return !d.Left.IsValid() && d.Right.IsValid() || (d.Left.Kind() == d.Right.Kind() && // cmp reports adding first element to a nil slice as creation of one and therefore Left is valid and it is nil and right is a new slice
|
||||
(d.Left.Kind() == reflect.Slice || d.Left.Kind() == reflect.Map) && d.Left.Len() < d.Right.Len())
|
||||
}
|
||||
|
||||
// IsDeleteOperation returns true when
|
||||
// - Right does not have value and Left has
|
||||
// - the kind of Left and Right is either reflect.Slice or reflect.Map and the length of Right is less than length of Left
|
||||
// In all other cases it returns false.
|
||||
// NOTE: this is applicable to diff of Maps and Slices only
|
||||
func (d *Diff) IsDeleteOperation() bool {
|
||||
return d.Left.IsValid() && !d.Right.IsValid() || (d.Left.Kind() == d.Right.Kind() &&
|
||||
(d.Left.Kind() == reflect.Slice || d.Left.Kind() == reflect.Map) && d.Left.Len() > d.Right.Len())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user