-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.go
More file actions
153 lines (133 loc) · 3.4 KB
/
track.go
File metadata and controls
153 lines (133 loc) · 3.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package structo
import (
"reflect"
"strconv"
)
// Actions represent the type of change
type Actions string
const (
Add Actions = "add"
Remove Actions = "remove"
Change Actions = "change"
)
// resultData stores from/to data
type resultData struct {
dataFrom any
dataTo any
}
func (r *resultData) GetData() any {
if r.dataFrom != nil && r.dataTo != nil {
return []any{r.dataFrom, r.dataTo}
} else if r.dataFrom != nil {
return r.dataFrom
}
return r.dataTo
}
// result is the main output for each changed path
type result struct {
Action Actions
Data resultData
}
// TrackWithHistory returns a map of field paths and their detected actions
func TrackWithHistory(oldStruct, newStruct interface{}) (map[string]result, error) {
oldVal, newVal, err := getComparableValues(oldStruct, newStruct)
if err != nil {
return nil, err
}
changes := make(map[string]result)
trackRecursive(oldVal, newVal, "", changes)
return changes, nil
}
func trackRecursive(oldVal, newVal reflect.Value, prefix string, resultMap map[string]result) {
if oldVal.Kind() == reflect.Ptr {
if oldVal.IsNil() || newVal.IsNil() {
if !reflect.DeepEqual(deref(oldVal), deref(newVal)) {
resultMap[prefix] = result{
Action: Change,
Data: resultData{dataFrom: derefInterface(oldVal), dataTo: derefInterface(newVal)},
}
}
return
}
oldVal = oldVal.Elem()
newVal = newVal.Elem()
}
switch oldVal.Kind() {
case reflect.Struct:
for i := 0; i < oldVal.NumField(); i++ {
field := oldVal.Type().Field(i)
if !field.IsExported() {
continue
}
fieldPath := joinKey(prefix, field.Name)
trackRecursive(oldVal.Field(i), newVal.Field(i), fieldPath, resultMap)
}
case reflect.Slice:
oldLen := oldVal.Len()
newLen := newVal.Len()
minLen := oldLen
if newLen < oldLen {
minLen = newLen
}
for i := 0; i < minLen; i++ {
oldItem := oldVal.Index(i)
newItem := newVal.Index(i)
if !reflect.DeepEqual(oldItem.Interface(), newItem.Interface()) {
itemPath := joinKey(prefix, "["+strconv.Itoa(i)+"]")
if isStructLike(oldItem) {
trackRecursive(oldItem, newItem, itemPath, resultMap)
} else {
resultMap[itemPath] = result{
Action: Change,
Data: resultData{
dataFrom: oldItem.Interface(),
dataTo: newItem.Interface(),
},
}
}
}
}
for i := minLen; i < newLen; i++ {
itemPath := joinKey(prefix, "["+strconv.Itoa(i)+"]")
resultMap[itemPath] = result{
Action: Add,
Data: resultData{dataTo: newVal.Index(i).Interface()},
}
}
for i := minLen; i < oldLen; i++ {
itemPath := joinKey(prefix, "["+strconv.Itoa(i)+"]")
resultMap[itemPath] = result{
Action: Remove,
Data: resultData{dataFrom: oldVal.Index(i).Interface()},
}
}
default:
if !reflect.DeepEqual(oldVal.Interface(), newVal.Interface()) {
resultMap[prefix] = result{
Action: Change,
Data: resultData{
dataFrom: oldVal.Interface(),
dataTo: newVal.Interface(),
},
}
}
}
}
func deref(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Ptr && !v.IsNil() {
return v.Elem()
}
return v
}
func derefInterface(v reflect.Value) interface{} {
if v.Kind() == reflect.Ptr && !v.IsNil() {
return v.Elem().Interface()
}
if v.IsValid() {
return v.Interface()
}
return nil
}
func isStructLike(v reflect.Value) bool {
return (v.Kind() == reflect.Struct) || (v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct)
}