equal.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Protocol buffer comparison.
  5. package proto
  6. import (
  7. "bytes"
  8. "log"
  9. "reflect"
  10. "strings"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. /*
  14. Equal returns true iff protocol buffers a and b are equal.
  15. The arguments must both be pointers to protocol buffer structs.
  16. Equality is defined in this way:
  17. - Two messages are equal iff they are the same type,
  18. corresponding fields are equal, unknown field sets
  19. are equal, and extensions sets are equal.
  20. - Two set scalar fields are equal iff their values are equal.
  21. If the fields are of a floating-point type, remember that
  22. NaN != x for all x, including NaN. If the message is defined
  23. in a proto3 .proto file, fields are not "set"; specifically,
  24. zero length proto3 "bytes" fields are equal (nil == {}).
  25. - Two repeated fields are equal iff their lengths are the same,
  26. and their corresponding elements are equal. Note a "bytes" field,
  27. although represented by []byte, is not a repeated field and the
  28. rule for the scalar fields described above applies.
  29. - Two unset fields are equal.
  30. - Two unknown field sets are equal if their current
  31. encoded state is equal.
  32. - Two extension sets are equal iff they have corresponding
  33. elements that are pairwise equal.
  34. - Two map fields are equal iff their lengths are the same,
  35. and they contain the same set of elements. Zero-length map
  36. fields are equal.
  37. - Every other combination of things are not equal.
  38. The return value is undefined if a and b are not protocol buffers.
  39. */
  40. func Equal(a, b Message) bool {
  41. if a == nil || b == nil {
  42. return a == b
  43. }
  44. v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
  45. if v1.Type() != v2.Type() {
  46. return false
  47. }
  48. if v1.Kind() == reflect.Ptr {
  49. if v1.IsNil() {
  50. return v2.IsNil()
  51. }
  52. if v2.IsNil() {
  53. return false
  54. }
  55. v1, v2 = v1.Elem(), v2.Elem()
  56. }
  57. if v1.Kind() != reflect.Struct {
  58. return false
  59. }
  60. return equalStruct(v1, v2)
  61. }
  62. // v1 and v2 are known to have the same type.
  63. func equalStruct(v1, v2 reflect.Value) bool {
  64. sprop := GetProperties(v1.Type())
  65. for i := 0; i < v1.NumField(); i++ {
  66. f := v1.Type().Field(i)
  67. if strings.HasPrefix(f.Name, "XXX_") || f.PkgPath != "" {
  68. continue
  69. }
  70. f1, f2 := v1.Field(i), v2.Field(i)
  71. if f.Type.Kind() == reflect.Ptr {
  72. if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
  73. // both unset
  74. continue
  75. } else if n1 != n2 {
  76. // set/unset mismatch
  77. return false
  78. }
  79. f1, f2 = f1.Elem(), f2.Elem()
  80. }
  81. if !equalAny(f1, f2, sprop.Prop[i]) {
  82. return false
  83. }
  84. }
  85. if em1 := extensionFieldsValue(v1); em1.IsValid() {
  86. em2 := extensionFieldsValue(v2)
  87. m1 := extensionFieldsOf(em1.Addr().Interface())
  88. m2 := extensionFieldsOf(em2.Addr().Interface())
  89. if !equalExtensions(v1.Type(), m1, m2) {
  90. return false
  91. }
  92. }
  93. if uf := unknownFieldsValue(v1); uf.IsValid() {
  94. u1 := uf.Bytes()
  95. u2 := unknownFieldsValue(v2).Bytes()
  96. if !bytes.Equal(u1, u2) {
  97. return false
  98. }
  99. }
  100. return true
  101. }
  102. // v1 and v2 are known to have the same type.
  103. // prop may be nil.
  104. func equalAny(v1, v2 reflect.Value, prop *Properties) bool {
  105. if v1.Type() == protoMessageType {
  106. m1, _ := v1.Interface().(Message)
  107. m2, _ := v2.Interface().(Message)
  108. return Equal(m1, m2)
  109. }
  110. switch v1.Kind() {
  111. case reflect.Bool:
  112. return v1.Bool() == v2.Bool()
  113. case reflect.Float32, reflect.Float64:
  114. return v1.Float() == v2.Float()
  115. case reflect.Int32, reflect.Int64:
  116. return v1.Int() == v2.Int()
  117. case reflect.Interface:
  118. // Probably a oneof field; compare the inner values.
  119. n1, n2 := v1.IsNil(), v2.IsNil()
  120. if n1 || n2 {
  121. return n1 == n2
  122. }
  123. e1, e2 := v1.Elem(), v2.Elem()
  124. if e1.Type() != e2.Type() {
  125. return false
  126. }
  127. return equalAny(e1, e2, nil)
  128. case reflect.Map:
  129. if v1.Len() != v2.Len() {
  130. return false
  131. }
  132. for _, key := range v1.MapKeys() {
  133. val2 := v2.MapIndex(key)
  134. if !val2.IsValid() {
  135. // This key was not found in the second map.
  136. return false
  137. }
  138. if !equalAny(v1.MapIndex(key), val2, nil) {
  139. return false
  140. }
  141. }
  142. return true
  143. case reflect.Ptr:
  144. // Maps may have nil values in them, so check for nil.
  145. if v1.IsNil() && v2.IsNil() {
  146. return true
  147. }
  148. if v1.IsNil() != v2.IsNil() {
  149. return false
  150. }
  151. return equalAny(v1.Elem(), v2.Elem(), prop)
  152. case reflect.Slice:
  153. if v1.Type().Elem().Kind() == reflect.Uint8 {
  154. // short circuit: []byte
  155. // Edge case: if this is in a proto3 message, a zero length
  156. // bytes field is considered the zero value.
  157. if prop != nil && prop.Proto3 && v1.Len() == 0 && v2.Len() == 0 {
  158. return true
  159. }
  160. if v1.IsNil() != v2.IsNil() {
  161. return false
  162. }
  163. return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
  164. }
  165. if v1.Len() != v2.Len() {
  166. return false
  167. }
  168. for i := 0; i < v1.Len(); i++ {
  169. if !equalAny(v1.Index(i), v2.Index(i), prop) {
  170. return false
  171. }
  172. }
  173. return true
  174. case reflect.String:
  175. return v1.Interface().(string) == v2.Interface().(string)
  176. case reflect.Struct:
  177. return equalStruct(v1, v2)
  178. case reflect.Uint32, reflect.Uint64:
  179. return v1.Uint() == v2.Uint()
  180. }
  181. // unknown type, so not a protocol buffer
  182. log.Printf("proto: don't know how to compare %v", v1)
  183. return false
  184. }
  185. func equalExtensions(base reflect.Type, em1, em2 *extensionMap) bool {
  186. if em1.Len() != em2.Len() {
  187. return false
  188. }
  189. equal := true
  190. em1.Range(func(extNum protoreflect.FieldNumber, e1 Extension) bool {
  191. if !em2.Has(extNum) {
  192. equal = false
  193. return false
  194. }
  195. e2 := em2.Get(extNum)
  196. m1 := extensionAsLegacyType(e1.GetValue())
  197. m2 := extensionAsLegacyType(e2.GetValue())
  198. if m1 == nil && m2 == nil {
  199. return true
  200. }
  201. if m1 != nil && m2 != nil {
  202. // Both are unencoded.
  203. if !equalAny(reflect.ValueOf(m1), reflect.ValueOf(m2), nil) {
  204. equal = false
  205. return false
  206. }
  207. return true
  208. }
  209. equal = false
  210. return false
  211. })
  212. return equal
  213. }