blob: 2ed1cf596664d3dedb372fc051a04b79da040f21 [file] [log] [blame]
David Symonds76551212011-08-22 15:59:24 +10001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2011 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
David Symonds76551212011-08-22 15:59:24 +10005//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32// Protocol buffer comparison.
David Symonds76551212011-08-22 15:59:24 +100033
34package proto
35
36import (
37 "bytes"
38 "log"
39 "reflect"
40 "strings"
41)
42
43/*
44Equal returns true iff protocol buffers a and b are equal.
David Symonds9f60f432012-06-14 09:45:25 +100045The arguments must both be pointers to protocol buffer structs.
David Symonds76551212011-08-22 15:59:24 +100046
47Equality is defined in this way:
48 - Two messages are equal iff they are the same type,
49 corresponding fields are equal, unknown field sets
50 are equal, and extensions sets are equal.
51 - Two set scalar fields are equal iff their values are equal.
David Symondsb79d99b2011-08-29 16:38:49 +100052 If the fields are of a floating-point type, remember that
David Symonds4df416c2016-01-06 06:53:00 +110053 NaN != x for all x, including NaN. If the message is defined
54 in a proto3 .proto file, fields are not "set"; specifically,
55 zero length proto3 "bytes" fields are equal (nil == {}).
David Symonds76551212011-08-22 15:59:24 +100056 - Two repeated fields are equal iff their lengths are the same,
hakimef00c022016-08-22 20:00:37 +000057 and their corresponding elements are equal. Note a "bytes" field,
58 although represented by []byte, is not a repeated field and the
59 rule for the scalar fields described above applies.
David Symonds76551212011-08-22 15:59:24 +100060 - Two unset fields are equal.
61 - Two unknown field sets are equal if their current
David Symonds50386d22014-10-28 11:00:50 +110062 encoded state is equal.
David Symonds76551212011-08-22 15:59:24 +100063 - Two extension sets are equal iff they have corresponding
David Symondsa4b61c02011-09-07 14:06:00 +100064 elements that are pairwise equal.
hakimef00c022016-08-22 20:00:37 +000065 - Two map fields are equal iff their lengths are the same,
66 and they contain the same set of elements. Zero-length map
67 fields are equal.
David Symonds76551212011-08-22 15:59:24 +100068 - Every other combination of things are not equal.
69
70The return value is undefined if a and b are not protocol buffers.
71*/
David Symonds9f60f432012-06-14 09:45:25 +100072func Equal(a, b Message) bool {
David Symonds007ed9d2012-07-24 10:59:36 +100073 if a == nil || b == nil {
74 return a == b
75 }
David Symonds76551212011-08-22 15:59:24 +100076 v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
77 if v1.Type() != v2.Type() {
78 return false
79 }
80 if v1.Kind() == reflect.Ptr {
David Symonds007ed9d2012-07-24 10:59:36 +100081 if v1.IsNil() {
82 return v2.IsNil()
83 }
84 if v2.IsNil() {
85 return false
86 }
David Symonds76551212011-08-22 15:59:24 +100087 v1, v2 = v1.Elem(), v2.Elem()
88 }
89 if v1.Kind() != reflect.Struct {
90 return false
91 }
92 return equalStruct(v1, v2)
93}
94
95// v1 and v2 are known to have the same type.
96func equalStruct(v1, v2 reflect.Value) bool {
David Symonds4df416c2016-01-06 06:53:00 +110097 sprop := GetProperties(v1.Type())
David Symonds76551212011-08-22 15:59:24 +100098 for i := 0; i < v1.NumField(); i++ {
99 f := v1.Type().Field(i)
100 if strings.HasPrefix(f.Name, "XXX_") {
101 continue
102 }
103 f1, f2 := v1.Field(i), v2.Field(i)
104 if f.Type.Kind() == reflect.Ptr {
105 if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
106 // both unset
107 continue
108 } else if n1 != n2 {
109 // set/unset mismatch
110 return false
111 }
David Symondse9e7aaf2012-03-23 13:12:33 +1100112 b1, ok := f1.Interface().(raw)
113 if ok {
114 b2 := f2.Interface().(raw)
115 // RawMessage
116 if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
117 return false
118 }
119 continue
120 }
David Symonds76551212011-08-22 15:59:24 +1000121 f1, f2 = f1.Elem(), f2.Elem()
122 }
David Symonds4df416c2016-01-06 06:53:00 +1100123 if !equalAny(f1, f2, sprop.Prop[i]) {
David Symonds76551212011-08-22 15:59:24 +1000124 return false
125 }
126 }
127
matloob@google.come51d0022016-05-23 09:09:04 -0400128 if em1 := v1.FieldByName("XXX_InternalExtensions"); em1.IsValid() {
129 em2 := v2.FieldByName("XXX_InternalExtensions")
130 if !equalExtensions(v1.Type(), em1.Interface().(XXX_InternalExtensions), em2.Interface().(XXX_InternalExtensions)) {
David Symondsa4b61c02011-09-07 14:06:00 +1000131 return false
132 }
133 }
134
matloob@google.com9e6977f2016-05-24 15:56:25 -0400135 if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
136 em2 := v2.FieldByName("XXX_extensions")
137 if !equalExtMap(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
138 return false
139 }
140 }
141
David Symonds6eaeef12012-11-07 11:42:56 +1100142 uf := v1.FieldByName("XXX_unrecognized")
143 if !uf.IsValid() {
144 return true
145 }
146
147 u1 := uf.Bytes()
David Symonds10c93ba2012-08-04 16:38:08 +1000148 u2 := v2.FieldByName("XXX_unrecognized").Bytes()
149 if !bytes.Equal(u1, u2) {
150 return false
151 }
David Symonds76551212011-08-22 15:59:24 +1000152
153 return true
154}
155
156// v1 and v2 are known to have the same type.
David Symonds4df416c2016-01-06 06:53:00 +1100157// prop may be nil.
158func equalAny(v1, v2 reflect.Value, prop *Properties) bool {
David Symonds007ed9d2012-07-24 10:59:36 +1000159 if v1.Type() == protoMessageType {
160 m1, _ := v1.Interface().(Message)
161 m2, _ := v2.Interface().(Message)
162 return Equal(m1, m2)
163 }
David Symonds76551212011-08-22 15:59:24 +1000164 switch v1.Kind() {
165 case reflect.Bool:
166 return v1.Bool() == v2.Bool()
167 case reflect.Float32, reflect.Float64:
168 return v1.Float() == v2.Float()
169 case reflect.Int32, reflect.Int64:
170 return v1.Int() == v2.Int()
David Symonds59b73b32015-08-24 13:22:02 +1000171 case reflect.Interface:
172 // Probably a oneof field; compare the inner values.
173 n1, n2 := v1.IsNil(), v2.IsNil()
174 if n1 || n2 {
175 return n1 == n2
176 }
177 e1, e2 := v1.Elem(), v2.Elem()
178 if e1.Type() != e2.Type() {
179 return false
180 }
David Symonds4df416c2016-01-06 06:53:00 +1100181 return equalAny(e1, e2, nil)
David Symonds3ea3e052014-12-22 16:15:28 +1100182 case reflect.Map:
183 if v1.Len() != v2.Len() {
184 return false
185 }
186 for _, key := range v1.MapKeys() {
187 val2 := v2.MapIndex(key)
188 if !val2.IsValid() {
189 // This key was not found in the second map.
190 return false
191 }
David Symonds4df416c2016-01-06 06:53:00 +1100192 if !equalAny(v1.MapIndex(key), val2, nil) {
David Symonds3ea3e052014-12-22 16:15:28 +1100193 return false
194 }
195 }
196 return true
David Symonds76551212011-08-22 15:59:24 +1000197 case reflect.Ptr:
Ross Light11114612016-05-25 19:11:34 -0400198 // Maps may have nil values in them, so check for nil.
199 if v1.IsNil() && v2.IsNil() {
200 return true
201 }
202 if v1.IsNil() != v2.IsNil() {
203 return false
204 }
David Symonds4df416c2016-01-06 06:53:00 +1100205 return equalAny(v1.Elem(), v2.Elem(), prop)
David Symonds76551212011-08-22 15:59:24 +1000206 case reflect.Slice:
David Symonds35abe742012-02-13 09:55:16 +1100207 if v1.Type().Elem().Kind() == reflect.Uint8 {
208 // short circuit: []byte
David Symonds4df416c2016-01-06 06:53:00 +1100209
210 // Edge case: if this is in a proto3 message, a zero length
211 // bytes field is considered the zero value.
212 if prop != nil && prop.proto3 && v1.Len() == 0 && v2.Len() == 0 {
213 return true
214 }
David Symonds35abe742012-02-13 09:55:16 +1100215 if v1.IsNil() != v2.IsNil() {
216 return false
217 }
218 return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
David Symonds76551212011-08-22 15:59:24 +1000219 }
David Symonds35abe742012-02-13 09:55:16 +1100220
David Symonds76551212011-08-22 15:59:24 +1000221 if v1.Len() != v2.Len() {
222 return false
223 }
David Symonds76551212011-08-22 15:59:24 +1000224 for i := 0; i < v1.Len(); i++ {
David Symonds4df416c2016-01-06 06:53:00 +1100225 if !equalAny(v1.Index(i), v2.Index(i), prop) {
David Symonds76551212011-08-22 15:59:24 +1000226 return false
227 }
228 }
229 return true
230 case reflect.String:
231 return v1.Interface().(string) == v2.Interface().(string)
232 case reflect.Struct:
233 return equalStruct(v1, v2)
234 case reflect.Uint32, reflect.Uint64:
235 return v1.Uint() == v2.Uint()
236 }
237
238 // unknown type, so not a protocol buffer
239 log.Printf("proto: don't know how to compare %v", v1)
240 return false
241}
David Symondsa4b61c02011-09-07 14:06:00 +1000242
243// base is the struct type that the extensions are based on.
matloob@google.come51d0022016-05-23 09:09:04 -0400244// x1 and x2 are InternalExtensions.
245func equalExtensions(base reflect.Type, x1, x2 XXX_InternalExtensions) bool {
246 em1, _ := x1.extensionsRead()
247 em2, _ := x2.extensionsRead()
matloob@google.com9e6977f2016-05-24 15:56:25 -0400248 return equalExtMap(base, em1, em2)
249}
250
251func equalExtMap(base reflect.Type, em1, em2 map[int32]Extension) bool {
David Symondsa4b61c02011-09-07 14:06:00 +1000252 if len(em1) != len(em2) {
253 return false
254 }
255
256 for extNum, e1 := range em1 {
257 e2, ok := em2[extNum]
258 if !ok {
259 return false
260 }
261
262 m1, m2 := e1.value, e2.value
263
264 if m1 != nil && m2 != nil {
265 // Both are unencoded.
David Symonds4df416c2016-01-06 06:53:00 +1100266 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
David Symondsa4b61c02011-09-07 14:06:00 +1000267 return false
268 }
269 continue
270 }
271
272 // At least one is encoded. To do a semantically correct comparison
273 // we need to unmarshal them first.
274 var desc *ExtensionDesc
275 if m := extensionMaps[base]; m != nil {
276 desc = m[extNum]
277 }
278 if desc == nil {
279 log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
280 continue
281 }
Rob Pikea17fdd92011-11-02 12:43:05 -0700282 var err error
David Symondsa4b61c02011-09-07 14:06:00 +1000283 if m1 == nil {
284 m1, err = decodeExtension(e1.enc, desc)
285 }
286 if m2 == nil && err == nil {
287 m2, err = decodeExtension(e2.enc, desc)
288 }
289 if err != nil {
290 // The encoded form is invalid.
291 log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
292 return false
293 }
David Symonds4df416c2016-01-06 06:53:00 +1100294 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
David Symondsa4b61c02011-09-07 14:06:00 +1000295 return false
296 }
297 }
298
299 return true
300}