equal.go 8.2 KB

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