equal.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // TODO: MessageSet.
  33. package proto
  34. import (
  35. "bytes"
  36. "log"
  37. "reflect"
  38. "strings"
  39. )
  40. /*
  41. Equal returns true iff protocol buffers a and b are equal.
  42. The arguments must both be pointers to protocol buffer structs.
  43. Equality is defined in this way:
  44. - Two messages are equal iff they are the same type,
  45. corresponding fields are equal, unknown field sets
  46. are equal, and extensions sets are equal.
  47. - Two set scalar fields are equal iff their values are equal.
  48. If the fields are of a floating-point type, remember that
  49. NaN != x for all x, including NaN.
  50. - Two repeated fields are equal iff their lengths are the same,
  51. and their corresponding elements are equal (a "bytes" field,
  52. although represented by []byte, is not a repeated field)
  53. - Two unset fields are equal.
  54. - Two unknown field sets are equal if their current
  55. encoded state is equal.
  56. - Two extension sets are equal iff they have corresponding
  57. elements that are pairwise equal.
  58. - Every other combination of things are not equal.
  59. The return value is undefined if a and b are not protocol buffers.
  60. */
  61. func Equal(a, b Message) bool {
  62. if a == nil || b == nil {
  63. return a == b
  64. }
  65. v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
  66. if v1.Type() != v2.Type() {
  67. return false
  68. }
  69. if v1.Kind() == reflect.Ptr {
  70. if v1.IsNil() {
  71. return v2.IsNil()
  72. }
  73. if v2.IsNil() {
  74. return false
  75. }
  76. v1, v2 = v1.Elem(), v2.Elem()
  77. }
  78. if v1.Kind() != reflect.Struct {
  79. return false
  80. }
  81. return equalStruct(v1, v2)
  82. }
  83. // v1 and v2 are known to have the same type.
  84. func equalStruct(v1, v2 reflect.Value) bool {
  85. for i := 0; i < v1.NumField(); i++ {
  86. f := v1.Type().Field(i)
  87. if strings.HasPrefix(f.Name, "XXX_") {
  88. continue
  89. }
  90. f1, f2 := v1.Field(i), v2.Field(i)
  91. if f.Type.Kind() == reflect.Ptr {
  92. if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
  93. // both unset
  94. continue
  95. } else if n1 != n2 {
  96. // set/unset mismatch
  97. return false
  98. }
  99. b1, ok := f1.Interface().(raw)
  100. if ok {
  101. b2 := f2.Interface().(raw)
  102. // RawMessage
  103. if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
  104. return false
  105. }
  106. continue
  107. }
  108. f1, f2 = f1.Elem(), f2.Elem()
  109. }
  110. if !equalAny(f1, f2) {
  111. return false
  112. }
  113. }
  114. if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
  115. em2 := v2.FieldByName("XXX_extensions")
  116. if !equalExtensions(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
  117. return false
  118. }
  119. }
  120. uf := v1.FieldByName("XXX_unrecognized")
  121. if !uf.IsValid() {
  122. return true
  123. }
  124. u1 := uf.Bytes()
  125. u2 := v2.FieldByName("XXX_unrecognized").Bytes()
  126. if !bytes.Equal(u1, u2) {
  127. return false
  128. }
  129. return true
  130. }
  131. // v1 and v2 are known to have the same type.
  132. func equalAny(v1, v2 reflect.Value) bool {
  133. if v1.Type() == protoMessageType {
  134. m1, _ := v1.Interface().(Message)
  135. m2, _ := v2.Interface().(Message)
  136. return Equal(m1, m2)
  137. }
  138. switch v1.Kind() {
  139. case reflect.Bool:
  140. return v1.Bool() == v2.Bool()
  141. case reflect.Float32, reflect.Float64:
  142. return v1.Float() == v2.Float()
  143. case reflect.Int32, reflect.Int64:
  144. return v1.Int() == v2.Int()
  145. case reflect.Map:
  146. if v1.Len() != v2.Len() {
  147. return false
  148. }
  149. for _, key := range v1.MapKeys() {
  150. val2 := v2.MapIndex(key)
  151. if !val2.IsValid() {
  152. // This key was not found in the second map.
  153. return false
  154. }
  155. if !equalAny(v1.MapIndex(key), val2) {
  156. return false
  157. }
  158. }
  159. return true
  160. case reflect.Ptr:
  161. return equalAny(v1.Elem(), v2.Elem())
  162. case reflect.Slice:
  163. if v1.Type().Elem().Kind() == reflect.Uint8 {
  164. // short circuit: []byte
  165. if v1.IsNil() != v2.IsNil() {
  166. return false
  167. }
  168. return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
  169. }
  170. if v1.Len() != v2.Len() {
  171. return false
  172. }
  173. for i := 0; i < v1.Len(); i++ {
  174. if !equalAny(v1.Index(i), v2.Index(i)) {
  175. return false
  176. }
  177. }
  178. return true
  179. case reflect.String:
  180. return v1.Interface().(string) == v2.Interface().(string)
  181. case reflect.Struct:
  182. return equalStruct(v1, v2)
  183. case reflect.Uint32, reflect.Uint64:
  184. return v1.Uint() == v2.Uint()
  185. }
  186. // unknown type, so not a protocol buffer
  187. log.Printf("proto: don't know how to compare %v", v1)
  188. return false
  189. }
  190. // base is the struct type that the extensions are based on.
  191. // em1 and em2 are extension maps.
  192. func equalExtensions(base reflect.Type, em1, em2 map[int32]Extension) bool {
  193. if len(em1) != len(em2) {
  194. return false
  195. }
  196. for extNum, e1 := range em1 {
  197. e2, ok := em2[extNum]
  198. if !ok {
  199. return false
  200. }
  201. m1, m2 := e1.value, e2.value
  202. if m1 != nil && m2 != nil {
  203. // Both are unencoded.
  204. if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
  205. return false
  206. }
  207. continue
  208. }
  209. // At least one is encoded. To do a semantically correct comparison
  210. // we need to unmarshal them first.
  211. var desc *ExtensionDesc
  212. if m := extensionMaps[base]; m != nil {
  213. desc = m[extNum]
  214. }
  215. if desc == nil {
  216. log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
  217. continue
  218. }
  219. var err error
  220. if m1 == nil {
  221. m1, err = decodeExtension(e1.enc, desc)
  222. }
  223. if m2 == nil && err == nil {
  224. m2, err = decodeExtension(e2.enc, desc)
  225. }
  226. if err != nil {
  227. // The encoded form is invalid.
  228. log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
  229. return false
  230. }
  231. if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2)) {
  232. return false
  233. }
  234. }
  235. return true
  236. }