-
Notifications
You must be signed in to change notification settings - Fork 0
/
equal.go
269 lines (230 loc) · 6.95 KB
/
equal.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package protocmp
import (
"bytes"
"fmt"
"math"
"reflect"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/encoding/protowire"
"google.golang.org/protobuf/reflect/protoreflect"
)
func Equal(x, y proto.Message) *DiffError {
if err := equal(proto.MessageV2(x), proto.MessageV2(y)); err != nil {
return err.Diff()
}
return nil
}
func equal(x, y protoreflect.ProtoMessage) *matchErr {
vx := reflect.ValueOf(x)
vy := reflect.ValueOf(y)
xNil := !vx.IsValid() || vx.IsNil()
yNil := !vy.IsValid() || vy.IsNil()
if xNil || yNil {
if xNil && yNil {
return nil
}
if yNil {
return newMatchError("value mismatch").Values(fmtMessage(x.ProtoReflect()), y).Field(x.ProtoReflect().Descriptor().Name())
}
return newMatchError("value mismatch").Values(x, fmtMessage(y.ProtoReflect())).Field(y.ProtoReflect().Descriptor().Name())
}
mx := x.ProtoReflect()
my := y.ProtoReflect()
if mx.IsValid() != my.IsValid() {
if mx.IsValid() {
return newMatchError("value mismatch").Values(fmtMessage(mx), nil).Field(mx.Descriptor().Name())
}
return newMatchError("value mismatch").Values(nil, fmtMessage(my)).Field(my.Descriptor().Name())
}
if err := equalMessage(mx, my); err != nil {
return err
}
return nil
}
func fmtError(v protoreflect.Value, fd protoreflect.FieldDescriptor) *matchErr {
switch {
case fd.IsList():
return newMatchError("value mismatch").Field(fd.Name()).Values(fmtList(v.List(), fd), nil)
case fd.IsMap():
return newMatchError("value mismatch").Field(fd.Name()).Values(fmtMap(v.Map(), fd), nil)
default:
switch fd.Kind() {
case protoreflect.MessageKind, protoreflect.GroupKind:
return newMatchError("value mismatch").Field(fd.Name()).Values(fmtMessage(v.Message()), nil)
case protoreflect.StringKind:
return newMatchError("value mismatch").Field(fd.Name()).ValueExpected(quoteString(v.Interface()))
}
return newMatchError("value mismatch").Field(fd.Name()).Values(v.Interface(), nil)
}
}
func fmtMissingFieldError(fd protoreflect.FieldDescriptor, vx, vy protoreflect.Value) *matchErr {
switch {
case fd.IsList() || fd.IsMap():
fallthrough
case fd.Kind() == protoreflect.MessageKind || fd.Kind() == protoreflect.GroupKind:
return fmtError(vx, fd).ValueActual(nil)
case fd.Kind() == protoreflect.StringKind:
return fmtError(vx, fd).ValueActual(quoteString(""))
default:
return fmtError(vx, fd).ValueActual(vy.Interface())
}
}
// equalMessage compares two messages.
func equalMessage(mx, my protoreflect.Message) *matchErr {
if mx.Descriptor() != my.Descriptor() {
return newMatchError("descriptors don't match")
}
if mx.IsValid() && !my.IsValid() {
return newMatchError("value mismatch").Values(fmtMessage(mx), nil)
}
if !mx.IsValid() && my.IsValid() {
return newMatchError("value mismatch").Values(nil, fmtMessage(my))
}
nx := 0
var equalErr *matchErr
mx.Range(func(fd protoreflect.FieldDescriptor, vx protoreflect.Value) bool {
nx++
vy := my.Get(fd)
if !my.Has(fd) {
equalErr = fmtMissingFieldError(fd, vx, vy)
return false
}
if err := equalField(fd, vx, vy); err != nil {
equalErr = err
return false
}
return true
})
if equalErr != nil {
return equalErr
}
ny := 0
my.Range(func(fd protoreflect.FieldDescriptor, vy protoreflect.Value) bool {
ny++
vx := mx.Get(fd)
if !mx.Has(fd) {
equalErr = fmtMissingFieldError(fd, vy, vx).ValuesSwap()
return false
}
return true
})
if equalErr != nil {
return equalErr
}
return equalUnknown(mx.GetUnknown(), my.GetUnknown())
}
// equalField compares two fields.
func equalField(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) *matchErr {
switch {
case fd.IsList():
if err := equalList(fd, x.List(), y.List()); err != nil {
return err.Field(fd.Name())
}
case fd.IsMap():
if err := equalMap(fd, x.Map(), y.Map()); err != nil {
return err.Field(fd.Name())
}
default:
if err := equalValue(fd, x, y); err != nil {
return err.Field(fd.Name())
}
}
return nil
}
// equalMap compares two maps.
func equalMap(fd protoreflect.FieldDescriptor, x, y protoreflect.Map) *matchErr {
if x.Len() != y.Len() {
return newMatchError("length mismatch").Values(x.Len(), y.Len())
}
var equalErr *matchErr
x.Range(func(k protoreflect.MapKey, vx protoreflect.Value) bool {
vy := y.Get(k)
if !y.Has(k) {
equalErr = newMatchError("missing key").Field(protoreflect.Name(fmt.Sprintf("[%s]", k.String())))
return false
}
if err := equalValue(fd.MapValue(), vx, vy); err != nil {
equalErr = err.Field(protoreflect.Name(fmt.Sprintf("[%s]", k.String())))
return false
}
return true
})
return equalErr
}
// equalList compares two lists.
func equalList(fd protoreflect.FieldDescriptor, x, y protoreflect.List) *matchErr {
if x.Len() != y.Len() {
return newMatchError("length mismatch").Values(x.Len(), y.Len())
}
for i := x.Len() - 1; i >= 0; i-- {
if err := equalValue(fd, x.Get(i), y.Get(i)); err != nil {
return err.Field(protoreflect.Name(fmt.Sprintf("[%d]", i)))
}
}
return nil
}
// equalValue compares two singular values.
func equalValue(fd protoreflect.FieldDescriptor, x, y protoreflect.Value) *matchErr {
switch {
case fd.Message() != nil:
if err := equalMessage(x.Message(), y.Message()); err != nil {
return err
}
return nil
case fd.Kind() == protoreflect.BytesKind:
if !bytes.Equal(x.Bytes(), y.Bytes()) {
return newMatchError("value mismatch").Values(x.Bytes(), y.Bytes())
}
case fd.Kind() == protoreflect.FloatKind, fd.Kind() == protoreflect.DoubleKind:
fx := x.Float()
fy := y.Float()
if math.IsNaN(fx) || math.IsNaN(fy) {
if !math.IsNaN(fx) && math.IsNaN(fy) {
return newMatchError("value mismatch").Values(fx, fy)
}
if math.IsNaN(fx) && !math.IsNaN(fy) {
return newMatchError("value mismatch").Values(fx, fy)
}
return nil
}
if fx != fy {
return newMatchError("value mismatch").Values(fx, fy)
}
return nil
case fd.Kind() == protoreflect.StringKind:
if x.Interface() != y.Interface() {
return newMatchError("value mismatch").Values(quoteString(x.Interface()), quoteString(y.Interface()))
}
default:
if x.Interface() != y.Interface() {
return newMatchError("value mismatch").Values(x.Interface(), y.Interface())
}
}
return nil
}
// equalUnknown compares unknown fields by direct comparison on the raw bytes
// of each individual field number.
func equalUnknown(x, y protoreflect.RawFields) *matchErr {
if len(x) != len(y) {
return newMatchError("length mismatch").Values(len(x), len(y))
}
if !bytes.Equal(x, y) {
return newMatchError("value mismatch").Values(x, y)
}
mx := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
my := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
for len(x) > 0 {
fnum, _, n := protowire.ConsumeField(x)
mx[fnum] = append(mx[fnum], x[:n]...)
x = x[n:]
}
for len(y) > 0 {
fnum, _, n := protowire.ConsumeField(y)
my[fnum] = append(my[fnum], y[:n]...)
y = y[n:]
}
if !reflect.DeepEqual(mx, my) {
return newMatchError("value mismatch").Values(mx, my)
}
return nil
}