forked from Jonathan-Rosenberg/oasdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_diff.go
140 lines (108 loc) · 2.64 KB
/
value_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
package diff
import (
"fmt"
"reflect"
)
// ValueDiff describes the changes between a pair of values
type ValueDiff struct {
From interface{} `json:"from" yaml:"from"`
To interface{} `json:"to" yaml:"to"`
}
// Empty indicates whether a change was found in this element
func (diff *ValueDiff) Empty() bool {
return diff == nil
}
func getValueDiff(value1, value2 interface{}) *ValueDiff {
diff := getValueDiffInternal(value1, value2)
if diff.Empty() {
return nil
}
return diff
}
func getValueDiffInternal(value1, value2 interface{}) *ValueDiff {
if reflect.DeepEqual(value1, value2) {
return nil
}
return &ValueDiff{
From: value1,
To: value2,
}
}
func getValueDiffConditional(exclude bool, value1, value2 interface{}) *ValueDiff {
if exclude {
return nil
}
return getValueDiff(value1, value2)
}
func getFloat64RefDiff(valueRef1, valueRef2 *float64) *ValueDiff {
return getValueDiff(derefFloat64(valueRef1), derefFloat64(valueRef2))
}
func getBoolRefDiff(valueRef1, valueRef2 *bool) *ValueDiff {
return getValueDiff(derefBool(valueRef1), derefBool(valueRef2))
}
func getStringRefDiffConditional(exclude bool, valueRef1, valueRef2 *string) *ValueDiff {
return getValueDiffConditional(exclude, derefString(valueRef1), derefString(valueRef2))
}
func getUInt64RefDiff(valueRef1, valueRef2 *uint64) *ValueDiff {
return getValueDiff(derefUInt64(valueRef1), derefUInt64(valueRef2))
}
func derefString(ref *string) interface{} {
if ref == nil {
return nil
}
return *ref
}
func derefBool(ref *bool) interface{} {
if ref == nil {
return nil
}
return *ref
}
func derefFloat64(ref *float64) interface{} {
if ref == nil {
return nil
}
return *ref
}
func derefUInt64(ref *uint64) interface{} {
if ref == nil {
return nil
}
return *ref
}
func (diff *ValueDiff) patchStringCB(cb func(string)) error {
if diff.Empty() {
return nil
}
if diff.To == nil {
return fmt.Errorf("diff value is nil instead of string")
}
switch diff.To.(type) {
case string:
cb(diff.To.(string))
default:
return fmt.Errorf("diff value type mismatch: string vs. %q", reflect.TypeOf(diff.To))
}
return nil
}
// patchString applies the patch to a string value
func (diff *ValueDiff) patchString(value *string) error {
return diff.patchStringCB(func(s string) { *value = s })
}
// patchUInt64Ref applies the patch to a *unit64 value
func (diff *ValueDiff) patchUInt64Ref(value **uint64) error {
if diff.Empty() {
return nil
}
if diff.To == nil {
*value = nil
return nil
}
switch t := diff.To.(type) {
case uint64:
*value = &t
default:
return fmt.Errorf("diff value type mismatch: uint64 vs. %q", reflect.TypeOf(diff.To))
}
return nil
}