You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

equal.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. // Protocol buffer comparison.
  32. package proto
  33. import (
  34. "bytes"
  35. "log"
  36. "reflect"
  37. "strings"
  38. )
  39. /*
  40. Equal returns true iff protocol buffers a and b are equal.
  41. The arguments must both be pointers to protocol buffer structs.
  42. Equality is defined in this way:
  43. - Two messages are equal iff they are the same type,
  44. corresponding fields are equal, unknown field sets
  45. are equal, and extensions sets are equal.
  46. - Two set scalar fields are equal iff their values are equal.
  47. If the fields are of a floating-point type, remember that
  48. NaN != x for all x, including NaN. If the message is defined
  49. in a proto3 .proto file, fields are not "set"; specifically,
  50. zero length proto3 "bytes" fields are equal (nil == {}).
  51. - Two repeated fields are equal iff their lengths are the same,
  52. and their corresponding elements are equal. Note a "bytes" field,
  53. although represented by []byte, is not a repeated field and the
  54. rule for the scalar fields described above applies.
  55. - Two unset fields are equal.
  56. - Two unknown field sets are equal if their current
  57. encoded state is equal.
  58. - Two extension sets are equal iff they have corresponding
  59. elements that are pairwise equal.
  60. - Two map fields are equal iff their lengths are the same,
  61. and they contain the same set of elements. Zero-length map
  62. fields are equal.
  63. - Every other combination of things are not equal.
  64. The return value is undefined if a and b are not protocol buffers.
  65. */
  66. func Equal(a, b Message) bool {
  67. if a == nil || b == nil {
  68. return a == b
  69. }
  70. v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
  71. if v1.Type() != v2.Type() {
  72. return false
  73. }
  74. if v1.Kind() == reflect.Ptr {
  75. if v1.IsNil() {
  76. return v2.IsNil()
  77. }
  78. if v2.IsNil() {
  79. return false
  80. }
  81. v1, v2 = v1.Elem(), v2.Elem()
  82. }
  83. if v1.Kind() != reflect.Struct {
  84. return false
  85. }
  86. return equalStruct(v1, v2)
  87. }
  88. // v1 and v2 are known to have the same type.
  89. func equalStruct(v1, v2 reflect.Value) bool {
  90. sprop := GetProperties(v1.Type())
  91. for i := 0; i < v1.NumField(); i++ {
  92. f := v1.Type().Field(i)
  93. if strings.HasPrefix(f.Name, "XXX_") {
  94. continue
  95. }
  96. f1, f2 := v1.Field(i), v2.Field(i)
  97. if f.Type.Kind() == reflect.Ptr {
  98. if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
  99. // both unset
  100. continue
  101. } else if n1 != n2 {
  102. // set/unset mismatch
  103. return false
  104. }
  105. f1, f2 = f1.Elem(), f2.Elem()
  106. }
  107. if !equalAny(f1, f2, sprop.Prop[i]) {
  108. return false
  109. }
  110. }
  111. if em1 := v1.FieldByName("XXX_InternalExtensions"); em1.IsValid() {
  112. em2 := v2.FieldByName("XXX_InternalExtensions")
  113. if !equalExtensions(v1.Type(), em1.Interface().(XXX_InternalExtensions), em2.Interface().(XXX_InternalExtensions)) {
  114. return false
  115. }
  116. }
  117. if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
  118. em2 := v2.FieldByName("XXX_extensions")
  119. if !equalExtMap(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
  120. return false
  121. }
  122. }
  123. uf := v1.FieldByName("XXX_unrecognized")
  124. if !uf.IsValid() {
  125. return true
  126. }
  127. u1 := uf.Bytes()
  128. u2 := v2.FieldByName("XXX_unrecognized").Bytes()
  129. return bytes.Equal(u1, u2)
  130. }
  131. // v1 and v2 are known to have the same type.
  132. // prop may be nil.
  133. func equalAny(v1, v2 reflect.Value, prop *Properties) bool {
  134. if v1.Type() == protoMessageType {
  135. m1, _ := v1.Interface().(Message)
  136. m2, _ := v2.Interface().(Message)
  137. return Equal(m1, m2)
  138. }
  139. switch v1.Kind() {
  140. case reflect.Bool:
  141. return v1.Bool() == v2.Bool()
  142. case reflect.Float32, reflect.Float64:
  143. return v1.Float() == v2.Float()
  144. case reflect.Int32, reflect.Int64:
  145. return v1.Int() == v2.Int()
  146. case reflect.Interface:
  147. // Probably a oneof field; compare the inner values.
  148. n1, n2 := v1.IsNil(), v2.IsNil()
  149. if n1 || n2 {
  150. return n1 == n2
  151. }
  152. e1, e2 := v1.Elem(), v2.Elem()
  153. if e1.Type() != e2.Type() {
  154. return false
  155. }
  156. return equalAny(e1, e2, nil)
  157. case reflect.Map:
  158. if v1.Len() != v2.Len() {
  159. return false
  160. }
  161. for _, key := range v1.MapKeys() {
  162. val2 := v2.MapIndex(key)
  163. if !val2.IsValid() {
  164. // This key was not found in the second map.
  165. return false
  166. }
  167. if !equalAny(v1.MapIndex(key), val2, nil) {
  168. return false
  169. }
  170. }
  171. return true
  172. case reflect.Ptr:
  173. // Maps may have nil values in them, so check for nil.
  174. if v1.IsNil() && v2.IsNil() {
  175. return true
  176. }
  177. if v1.IsNil() != v2.IsNil() {
  178. return false
  179. }
  180. return equalAny(v1.Elem(), v2.Elem(), prop)
  181. case reflect.Slice:
  182. if v1.Type().Elem().Kind() == reflect.Uint8 {
  183. // short circuit: []byte
  184. // Edge case: if this is in a proto3 message, a zero length
  185. // bytes field is considered the zero value.
  186. if prop != nil && prop.proto3 && v1.Len() == 0 && v2.Len() == 0 {
  187. return true
  188. }
  189. if v1.IsNil() != v2.IsNil() {
  190. return false
  191. }
  192. return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
  193. }
  194. if v1.Len() != v2.Len() {
  195. return false
  196. }
  197. for i := 0; i < v1.Len(); i++ {
  198. if !equalAny(v1.Index(i), v2.Index(i), prop) {
  199. return false
  200. }
  201. }
  202. return true
  203. case reflect.String:
  204. return v1.Interface().(string) == v2.Interface().(string)
  205. case reflect.Struct:
  206. return equalStruct(v1, v2)
  207. case reflect.Uint32, reflect.Uint64:
  208. return v1.Uint() == v2.Uint()
  209. }
  210. // unknown type, so not a protocol buffer
  211. log.Printf("proto: don't know how to compare %v", v1)
  212. return false
  213. }
  214. // base is the struct type that the extensions are based on.
  215. // x1 and x2 are InternalExtensions.
  216. func equalExtensions(base reflect.Type, x1, x2 XXX_InternalExtensions) bool {
  217. em1, _ := x1.extensionsRead()
  218. em2, _ := x2.extensionsRead()
  219. return equalExtMap(base, em1, em2)
  220. }
  221. func equalExtMap(base reflect.Type, em1, em2 map[int32]Extension) bool {
  222. if len(em1) != len(em2) {
  223. return false
  224. }
  225. for extNum, e1 := range em1 {
  226. e2, ok := em2[extNum]
  227. if !ok {
  228. return false
  229. }
  230. m1 := extensionAsLegacyType(e1.value)
  231. m2 := extensionAsLegacyType(e2.value)
  232. if m1 == nil && m2 == nil {
  233. // Both have only encoded form.
  234. if bytes.Equal(e1.enc, e2.enc) {
  235. continue
  236. }
  237. // The bytes are different, but the extensions might still be
  238. // equal. We need to decode them to compare.
  239. }
  240. if m1 != nil && m2 != nil {
  241. // Both are unencoded.
  242. if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
  243. return false
  244. }
  245. continue
  246. }
  247. // At least one is encoded. To do a semantically correct comparison
  248. // we need to unmarshal them first.
  249. var desc *ExtensionDesc
  250. if m := extensionMaps[base]; m != nil {
  251. desc = m[extNum]
  252. }
  253. if desc == nil {
  254. // If both have only encoded form and the bytes are the same,
  255. // it is handled above. We get here when the bytes are different.
  256. // We don't know how to decode it, so just compare them as byte
  257. // slices.
  258. log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
  259. return false
  260. }
  261. var err error
  262. if m1 == nil {
  263. m1, err = decodeExtension(e1.enc, desc)
  264. }
  265. if m2 == nil && err == nil {
  266. m2, err = decodeExtension(e2.enc, desc)
  267. }
  268. if err != nil {
  269. // The encoded form is invalid.
  270. log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
  271. return false
  272. }
  273. if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
  274. return false
  275. }
  276. }
  277. return true
  278. }