blob: 5cde0a4a7f66d39bc8c77e430351f3bd6a393bc3 [file] [log] [blame]
David Symonds76551212011-08-22 15:59:24 +10001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2011 Google Inc. All rights reserved.
4// http://code.google.com/p/goprotobuf/
5//
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 Symondse9e7aaf2012-03-23 13:12:33 +110033// TODO: MessageSet.
David Symonds76551212011-08-22 15:59:24 +100034
35package proto
36
37import (
38 "bytes"
39 "log"
40 "reflect"
41 "strings"
42)
43
44/*
45Equal returns true iff protocol buffers a and b are equal.
David Symonds9f60f432012-06-14 09:45:25 +100046The arguments must both be pointers to protocol buffer structs.
David Symonds76551212011-08-22 15:59:24 +100047
48Equality is defined in this way:
49 - Two messages are equal iff they are the same type,
50 corresponding fields are equal, unknown field sets
51 are equal, and extensions sets are equal.
52 - Two set scalar fields are equal iff their values are equal.
David Symondsb79d99b2011-08-29 16:38:49 +100053 If the fields are of a floating-point type, remember that
54 NaN != x for all x, including NaN.
David Symonds76551212011-08-22 15:59:24 +100055 - Two repeated fields are equal iff their lengths are the same,
David Symonds35abe742012-02-13 09:55:16 +110056 and their corresponding elements are equal (a "bytes" field,
57 although represented by []byte, is not a repeated field)
David Symonds76551212011-08-22 15:59:24 +100058 - Two unset fields are equal.
59 - Two unknown field sets are equal if their current
60 encoded state is equal. (TODO)
61 - Two extension sets are equal iff they have corresponding
David Symondsa4b61c02011-09-07 14:06:00 +100062 elements that are pairwise equal.
David Symonds76551212011-08-22 15:59:24 +100063 - Every other combination of things are not equal.
64
65The return value is undefined if a and b are not protocol buffers.
66*/
David Symonds9f60f432012-06-14 09:45:25 +100067func Equal(a, b Message) bool {
David Symonds76551212011-08-22 15:59:24 +100068 v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
69 if v1.Type() != v2.Type() {
70 return false
71 }
72 if v1.Kind() == reflect.Ptr {
73 v1, v2 = v1.Elem(), v2.Elem()
74 }
75 if v1.Kind() != reflect.Struct {
76 return false
77 }
78 return equalStruct(v1, v2)
79}
80
81// v1 and v2 are known to have the same type.
82func equalStruct(v1, v2 reflect.Value) bool {
83 for i := 0; i < v1.NumField(); i++ {
84 f := v1.Type().Field(i)
85 if strings.HasPrefix(f.Name, "XXX_") {
86 continue
87 }
88 f1, f2 := v1.Field(i), v2.Field(i)
89 if f.Type.Kind() == reflect.Ptr {
90 if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
91 // both unset
92 continue
93 } else if n1 != n2 {
94 // set/unset mismatch
95 return false
96 }
David Symondse9e7aaf2012-03-23 13:12:33 +110097 b1, ok := f1.Interface().(raw)
98 if ok {
99 b2 := f2.Interface().(raw)
100 // RawMessage
101 if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
102 return false
103 }
104 continue
105 }
David Symonds76551212011-08-22 15:59:24 +1000106 f1, f2 = f1.Elem(), f2.Elem()
107 }
108 if !equalAny(f1, f2) {
109 return false
110 }
111 }
112
David Symondsa4b61c02011-09-07 14:06:00 +1000113 if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
114 em2 := v2.FieldByName("XXX_extensions")
115 if !equalExtensions(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
116 return false
117 }
118 }
119
120 // TODO: Deal with XXX_unrecognized.
David Symonds76551212011-08-22 15:59:24 +1000121
122 return true
123}
124
125// v1 and v2 are known to have the same type.
126func equalAny(v1, v2 reflect.Value) bool {
127 switch v1.Kind() {
128 case reflect.Bool:
129 return v1.Bool() == v2.Bool()
130 case reflect.Float32, reflect.Float64:
131 return v1.Float() == v2.Float()
132 case reflect.Int32, reflect.Int64:
133 return v1.Int() == v2.Int()
134 case reflect.Ptr:
135 return equalAny(v1.Elem(), v2.Elem())
136 case reflect.Slice:
David Symonds35abe742012-02-13 09:55:16 +1100137 if v1.Type().Elem().Kind() == reflect.Uint8 {
138 // short circuit: []byte
139 if v1.IsNil() != v2.IsNil() {
140 return false
141 }
142 return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
David Symonds76551212011-08-22 15:59:24 +1000143 }
David Symonds35abe742012-02-13 09:55:16 +1100144
David Symonds76551212011-08-22 15:59:24 +1000145 if v1.Len() != v2.Len() {
146 return false
147 }
David Symonds76551212011-08-22 15:59:24 +1000148 for i := 0; i < v1.Len(); i++ {
149 if !equalAny(v1.Index(i), v2.Index(i)) {
150 return false
151 }
152 }
153 return true
154 case reflect.String:
155 return v1.Interface().(string) == v2.Interface().(string)
156 case reflect.Struct:
157 return equalStruct(v1, v2)
158 case reflect.Uint32, reflect.Uint64:
159 return v1.Uint() == v2.Uint()
160 }
161
162 // unknown type, so not a protocol buffer
163 log.Printf("proto: don't know how to compare %v", v1)
164 return false
165}
David Symondsa4b61c02011-09-07 14:06:00 +1000166
167// base is the struct type that the extensions are based on.
168// em1 and em2 are extension maps.
169func equalExtensions(base reflect.Type, em1, em2 map[int32]Extension) bool {
170 if len(em1) != len(em2) {
171 return false
172 }
173
174 for extNum, e1 := range em1 {
175 e2, ok := em2[extNum]
176 if !ok {
177 return false
178 }
179
180 m1, m2 := e1.value, e2.value
181
182 if m1 != nil && m2 != nil {
183 // Both are unencoded.
David Symonds9f60f432012-06-14 09:45:25 +1000184 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
David Symondsa4b61c02011-09-07 14:06:00 +1000185 return false
186 }
187 continue
188 }
189
190 // At least one is encoded. To do a semantically correct comparison
191 // we need to unmarshal them first.
192 var desc *ExtensionDesc
193 if m := extensionMaps[base]; m != nil {
194 desc = m[extNum]
195 }
196 if desc == nil {
197 log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
198 continue
199 }
Rob Pikea17fdd92011-11-02 12:43:05 -0700200 var err error
David Symondsa4b61c02011-09-07 14:06:00 +1000201 if m1 == nil {
202 m1, err = decodeExtension(e1.enc, desc)
203 }
204 if m2 == nil && err == nil {
205 m2, err = decodeExtension(e2.enc, desc)
206 }
207 if err != nil {
208 // The encoded form is invalid.
209 log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
210 return false
211 }
David Symonds9f60f432012-06-14 09:45:25 +1000212 if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
David Symondsa4b61c02011-09-07 14:06:00 +1000213 return false
214 }
215 }
216
217 return true
218}