-
Notifications
You must be signed in to change notification settings - Fork 40
/
difftracker.go
240 lines (188 loc) · 4.78 KB
/
difftracker.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package bongo
import (
"errors"
"fmt"
"github.com/go-bongo/go-dotaccess"
// "github.com/go-bongo/mgo/bson"
"reflect"
"strings"
)
type DiffTracker struct {
original interface{}
current interface{}
}
type Trackable interface {
GetDiffTracker() *DiffTracker
}
func NewDiffTracker(doc interface{}) *DiffTracker {
c := &DiffTracker{
current: doc,
original: nil,
}
return c
}
type DiffTrackingSession struct {
ChangedFields []string
IsNew bool
}
func (d *DiffTracker) NewSession(useBsonTags bool) (*DiffTrackingSession, error) {
sess := &DiffTrackingSession{}
isNew, changedFields, err := d.Compare(useBsonTags)
sess.IsNew = isNew
sess.ChangedFields = changedFields
return sess, err
}
func (d *DiffTracker) Reset() {
// Store a copy of current
d.original = reflect.Indirect(reflect.ValueOf(d.current)).Interface()
}
func (s *DiffTrackingSession) Modified(field string) bool {
if s.IsNew {
return true
} else {
for _, d := range s.ChangedFields {
if d == field || strings.HasPrefix(d, field+".") {
return true
}
}
return false
}
}
func (d *DiffTracker) Modified(field string) bool {
sess, _ := d.NewSession(false)
return sess.Modified(field)
}
func (d *DiffTracker) GetModified(useBson bool) (bool, []string) {
isNew, diffs, _ := d.Compare(useBson)
return isNew, diffs
}
func (d *DiffTracker) GetOriginalValue(field string) (interface{}, error) {
if d.original != nil {
return dotaccess.Get(d.original, field)
}
return nil, nil
}
func (d *DiffTracker) SetOriginal(orig interface{}) {
d.original = reflect.Indirect(reflect.ValueOf(orig)).Interface()
}
func (d *DiffTracker) Clear() {
d.original = nil
}
func (d *DiffTracker) Compare(useBson bool) (bool, []string, error) {
defer func() {
if r := recover(); r != nil {
fmt.Println("You probably forgot to initialize the DiffTracker instance on your model")
panic(r)
}
}()
if d.original != nil {
diffs, err := GetChangedFields(d.original, d.current, useBson)
return false, diffs, err
} else {
return true, []string{}, nil
}
}
func getFields(t reflect.Type) []string {
fields := []string{}
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fields = append(fields, field.Name)
}
return fields
}
func isNilOrInvalid(f reflect.Value) bool {
if f.Kind() == reflect.Ptr && f.IsNil() {
return true
}
return (!f.IsValid())
}
type Stringer interface {
String() string
}
func GetChangedFields(struct1 interface{}, struct2 interface{}, useBson bool) ([]string, error) {
diffs := make([]string, 0)
val1 := reflect.ValueOf(struct1)
type1 := val1.Type()
val2 := reflect.ValueOf(struct2)
type2 := val2.Type()
if type1.Kind() == reflect.Ptr {
type1 = type1.Elem()
val1 = val1.Elem()
}
if type2.Kind() == reflect.Ptr {
type2 = type2.Elem()
val2 = val2.Elem()
}
if type1.String() != type2.String() {
return diffs, errors.New(fmt.Sprintf("Cannot compare two structs of different types %s and %s", type1.String(), type2.String()))
}
if type1.Kind() != reflect.Struct || type2.Kind() != reflect.Struct {
return diffs, errors.New(fmt.Sprintf("Can only compare two structs or two pointers to structs", type1.Kind(), type2.Kind()))
}
for i := 0; i < type1.NumField(); i++ {
field1 := val1.Field(i)
field2 := val2.Field(i)
field := type1.Field(i)
tags := strings.Split(field.Tag.Get("bson"), ",")
inline := false
for _, t := range tags {
if t == "inline" {
inline = true
break
}
}
var fieldName string
if useBson {
fieldName = GetBsonName(field)
} else {
fieldName = field.Name
}
childType := field1.Type()
// Recurse?
if childType.Kind() == reflect.Ptr {
childType = childType.Elem()
}
// Skip if not exported
if len(field.PkgPath) > 0 {
continue
}
if childType.Kind() == reflect.Struct {
var childDiffs []string
var err error
// Make sure they aren't zero-value. Skip if so
if isNilOrInvalid(field1) && isNilOrInvalid(field2) {
continue
} else if isNilOrInvalid(field1) || isNilOrInvalid(field2) {
childDiffs = getFields(childType)
} else {
if _, ok := field1.Interface().(Stringer); ok {
if fmt.Sprint(field1.Interface()) != fmt.Sprint(field2.Interface()) {
diffs = append(diffs, fieldName)
}
} else {
childDiffs, err = GetChangedFields(field1.Interface(), field2.Interface(), useBson)
if err != nil {
return diffs, err
}
}
}
if len(childDiffs) > 0 {
for _, diff := range childDiffs {
if inline {
diffs = append(diffs, diff)
} else {
diffs = append(diffs, strings.Join([]string{fieldName, diff}, "."))
}
}
}
} else {
if fmt.Sprint(field1.Interface()) != fmt.Sprint(field2.Interface()) {
diffs = append(diffs, fieldName)
}
}
}
return diffs, nil
}