/
same.go
225 lines (179 loc) · 5.02 KB
/
same.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
package same
import (
"log"
"math"
"reflect"
)
func isIntKind(k reflect.Kind) bool {
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
default:
return false
}
}
func isFloatKind(k reflect.Kind) bool {
switch k {
case reflect.Float32, reflect.Float64:
return true
default:
return false
}
}
// isCompatibleKinds returns true if the two kinds are compatible -- that
// is that values of two compatible kinds might be equal.
func isCompatibleKinds(k1, k2 reflect.Kind) bool {
if k1 == k2 {
return true
} else if k1 == reflect.Slice && k2 == reflect.Array {
return true
} else if k2 == reflect.Slice && k1 == reflect.Array {
return true
}
if isIntKind(k1) && isIntKind(k2) {
return true
}
if isFloatKind(k1) && isFloatKind(k2) {
return true
}
return false
}
// isSameArray returns true if the two array values are the same length
// and have the same contents as defined by IsSame. This function explicitly
// ignores the declared type of the array.
func isSameArray(x, y reflect.Value) bool {
lx := x.Len()
if lx != y.Len() {
return false
}
for i := 0; i < lx; i++ {
xElement := x.Index(i)
yElement := y.Index(i)
if !IsSame(xElement.Interface(), yElement.Interface()) {
return false
}
}
return true
}
type StringConvertable interface {
String() string
}
// isSameUnknownStruct returns true if the two struct values are the same length
// and have the same contents as defined by IsSame. This function explicitly
// ignores the declared type of the struct.
func isSameStruct(x, y reflect.Value) bool {
sx, okx := x.Interface().(StringConvertable)
sy, oky := x.Interface().(StringConvertable)
if okx && oky {
//log.Println("Stringable", x, y)
return sx.String() == sy.String()
}
numFields := x.NumField()
if numFields != y.NumField() {
return false
}
typeX := x.Type()
for i := 0; i < numFields; i++ {
path := []int{i}
vx := x.FieldByIndex(path)
vy := y.FieldByName(typeX.Field(i).Name)
if vx.CanInterface() && vy.CanInterface() {
if !IsSame(vx.Interface(), vy.Interface()) {
return false
}
}
}
return true
}
// isSameMap returns true if the two map values are the same length
// and have the same contents as defined by IsSame. This function explicitly
// ignores the declared type of the map.
func isSameMap(x, y reflect.Value) bool {
//log.Println("\n\nSame map: x:",x, x.Type(), " y:", y, y.Type())
lx := x.Len()
if lx != y.Len() {
return false
}
// Avoid the typing problems with Go maps by turning the
// two maps into DumbMaps and doing the comparison there.
dmX := NewDumbMap(x.Interface())
//log.Println("Len of map:", dmX, dmX.Count())
dmY := NewDumbMap(y.Interface())
return dmX.IsSame(dmY)
}
// IsSame defines a fairly lose idea of structual equality. Two values
// are the same if they are integers and their int64 values are equal.
// Or if they are floats and their float64 values are equal.
// Or if they are both true or false.
// Or if they are both the same string.
// Or if they are maps that contain the same keys and values.
// Or if they are arrays that contain the same number and contents.
// Or if they are structs with the same public fields. TBD
// Note that IsSame explicitly ignores the declared types of the values
// except as noted above.
func IsSame(x, y interface{}) bool {
if x == nil && y == nil {
return true
}
return IsSameValue(reflect.ValueOf(x), reflect.ValueOf(y))
}
func unwindPointer(x reflect.Value) reflect.Value {
if x.Kind() != reflect.Ptr {
return x
}
for x.Kind() == reflect.Ptr {
x = x.Elem()
}
return reflect.ValueOf(x.Interface())
}
func getInt64(x reflect.Value) int64 {
switch x.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return x.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(x.Uint())
default:
panic("Not an int")
}
}
const Epsilon = 1.0E-5
// IsSameThing compares two non-pointer values.
func IsSameValue(x, y reflect.Value) bool {
x = unwindPointer(x)
y = unwindPointer(y)
kindX := x.Kind()
kindY := y.Kind()
if !isCompatibleKinds(kindX, kindY) {
return false
}
switch kindX {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return getInt64(x) == getInt64(y)
case reflect.Float32, reflect.Float64:
//log.Println("***Same float!!", x, y)
xf := x.Float()
yf := y.Float()
if xf == yf {
return true
} else {
return math.Abs(xf-yf) < Epsilon
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return getInt64(x) == getInt64(y)
case reflect.String:
return x.String() == y.String()
case reflect.Bool:
return x.Bool() == y.Bool()
case reflect.Array, reflect.Slice:
return isSameArray(x, y)
case reflect.Map:
return isSameMap(x, y)
case reflect.Struct:
return isSameStruct(x, y)
default:
log.Println("IsSame: **** What is:", x, kindX)
return false
}
}