-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
47 lines (40 loc) · 1.27 KB
/
diff.go
File metadata and controls
47 lines (40 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package structo
import (
"reflect"
"github.com/Lucifer07/Structo/errdefs"
)
// Diff returns a map of field names and their [old, new] values that differ.
func Diff(oldStruct, newStruct interface{}) (map[string][2]interface{}, error) {
oldVal, newVal, err := getComparableValues(oldStruct, newStruct)
if err != nil {
return nil, err
}
differences := make(map[string][2]interface{})
for i := 0; i < oldVal.NumField(); i++ {
field := oldVal.Type().Field(i).Name
oldValue := oldVal.Field(i).Interface()
newValue := newVal.Field(i).Interface()
if !reflect.DeepEqual(oldValue, newValue) {
differences[field] = [2]interface{}{oldValue, newValue}
}
}
return differences, nil
}
// getComparableValues returns reflect.Value of two structs after dereferencing pointers.
func getComparableValues(a, b interface{}) (reflect.Value, reflect.Value, error) {
va := reflect.ValueOf(a)
vb := reflect.ValueOf(b)
if va.Kind() == reflect.Ptr {
va = va.Elem()
}
if vb.Kind() == reflect.Ptr {
vb = vb.Elem()
}
if va.Kind() != reflect.Struct || vb.Kind() != reflect.Struct {
return reflect.Value{}, reflect.Value{}, errdefs.ErrInvalidStructType
}
if va.Type() != vb.Type() {
return reflect.Value{}, reflect.Value{}, errdefs.ErrMismatchedStructTypes
}
return va, vb, nil
}