-
Notifications
You must be signed in to change notification settings - Fork 7
/
location_diff.go
171 lines (149 loc) · 4.15 KB
/
location_diff.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
package yext
import (
"reflect"
)
type Comparable interface {
Equal(Comparable) bool
}
// Diff calculates the differences between a base Location (a) and a proposed set of changes
// represented by a second Location (b). The diffing logic will ignore fields in the proposed
// Location that aren't set (nil). This characteristic makes the function ideal for
// calculting the minimal PUT required to bring an object "up-to-date" via the API.
//
// Example:
// var (
// // Typically, this would come from an authoritative source, like the API
// base = Location{Name: "Yext, Inc.", Address1: "123 Mulberry St"}
// proposed = Location{Name: "Yext, Inc.", Address1: "456 Washington St"}
// )
//
// delta, isDiff := base.Diff(proposed)
// // isDiff -> true
// // delta -> &Location{Address1: "456 Washington St"}
//
// delta, isDiff = base.Diff(base)
// // isDiff -> false
// // delta -> nil
func (y Location) Diff(b *Location) (d *Location, diff bool) {
d = &Location{Id: String(y.GetId())}
var (
aV, bV = reflect.ValueOf(y), reflect.ValueOf(b).Elem()
aT = reflect.TypeOf(y)
numA = aV.NumField()
)
for i := 0; i < numA; i++ {
var (
nameA = aT.Field(i).Name
valA = aV.Field(i)
valB = bV.Field(i)
)
if !valB.CanSet() {
continue
}
if valB.IsNil() || nameA == "Id" {
continue
}
if nameA == "Hours" {
if !valA.IsNil() && !valB.IsNil() && HoursAreEquivalent(getUnderlyingValue(valA.Interface()).(string), getUnderlyingValue(valB.Interface()).(string)) {
continue
}
}
if isZeroValue(valA, y.nilIsEmpty) && isZeroValue(valB, b.nilIsEmpty) {
continue
}
aI, bI := valA.Interface(), valB.Interface()
comparableA, aOk := aI.(Comparable)
comparableB, bOk := bI.(Comparable)
if aOk && bOk {
if !comparableA.Equal(comparableB) {
diff = true
reflect.ValueOf(d).Elem().FieldByName(nameA).Set(valB)
}
} else if !reflect.DeepEqual(aI, bI) {
if nameA == "CustomFields" {
d.CustomFields = make(map[string]interface{})
for field, value := range b.CustomFields {
value = getUnderlyingValue(value)
if aValue, ok := y.CustomFields[field]; ok {
aValue = getUnderlyingValue(aValue)
var valueDiff bool
if v, ok := aValue.(Comparable); ok {
valueDiff = !v.Equal(value.(Comparable))
} else if !reflect.DeepEqual(aValue, value) {
valueDiff = true
}
if valueDiff {
diff = true
d.CustomFields[field] = value
}
} else if !(isZeroValue(reflect.ValueOf(value), b.nilIsEmpty) && y.nilIsEmpty) {
d.CustomFields[field] = value
diff = true
}
}
} else {
diff = true
reflect.ValueOf(d).Elem().FieldByName(nameA).Set(valB)
}
}
}
if diff == false {
// ensure d remains nil if nothing has changed
d = nil
}
return d, diff
}
// getUnderlyingValue unwraps a pointer/interface to get the underlying value.
// If the value is already unwrapped, it returns it as is.
func getUnderlyingValue(v interface{}) interface{} {
if v == nil {
return nil
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Interface:
if rv.IsNil() {
return nil
}
rv = rv.Elem()
}
return rv.Interface()
}
func isZeroValue(v reflect.Value, interpretNilAsZeroValue bool) bool {
if !v.IsValid() {
return true
}
switch v.Kind() {
case reflect.Slice, reflect.Map:
return v.Len() == 0
case reflect.Bool:
return v.Bool() == false
case reflect.String:
return v.String() == ""
case reflect.Float64:
return v.Float() == 0.0
case reflect.Ptr, reflect.Interface:
if v.IsNil() && !interpretNilAsZeroValue {
return false
}
return isZeroValue(v.Elem(), interpretNilAsZeroValue)
case reflect.Struct:
for i, n := 0, v.NumField(); i < n; i++ {
if !isZeroValue(v.Field(i), interpretNilAsZeroValue) {
return false
}
}
return true
default:
return v.IsNil() && interpretNilAsZeroValue
}
}
var closedHoursEquivalents = map[string]struct{}{
"": struct{}{},
HoursClosedAllWeek: struct{}{},
}
func HoursAreEquivalent(a, b string) bool {
_, aIsClosed := closedHoursEquivalents[a]
_, bIsClosed := closedHoursEquivalents[b]
return a == b || aIsClosed && bIsClosed
}