equal.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2024 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. package impl
  5. import (
  6. "bytes"
  7. "google.golang.org/protobuf/encoding/protowire"
  8. "google.golang.org/protobuf/reflect/protoreflect"
  9. "google.golang.org/protobuf/runtime/protoiface"
  10. )
  11. func equal(in protoiface.EqualInput) protoiface.EqualOutput {
  12. return protoiface.EqualOutput{Equal: equalMessage(in.MessageA, in.MessageB)}
  13. }
  14. // equalMessage is a fast-path variant of protoreflect.equalMessage.
  15. // It takes advantage of the internal messageState type to avoid
  16. // unnecessary allocations, type assertions.
  17. func equalMessage(mx, my protoreflect.Message) bool {
  18. if mx == nil || my == nil {
  19. return mx == my
  20. }
  21. if mx.Descriptor() != my.Descriptor() {
  22. return false
  23. }
  24. msx, ok := mx.(*messageState)
  25. if !ok {
  26. return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  27. }
  28. msy, ok := my.(*messageState)
  29. if !ok {
  30. return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  31. }
  32. mi := msx.messageInfo()
  33. miy := msy.messageInfo()
  34. if mi != miy {
  35. return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  36. }
  37. mi.init()
  38. // Compares regular fields
  39. // Modified Message.Range code that compares two messages of the same type
  40. // while going over the fields.
  41. for _, ri := range mi.rangeInfos {
  42. var fd protoreflect.FieldDescriptor
  43. var vx, vy protoreflect.Value
  44. switch ri := ri.(type) {
  45. case *fieldInfo:
  46. hx := ri.has(msx.pointer())
  47. hy := ri.has(msy.pointer())
  48. if hx != hy {
  49. return false
  50. }
  51. if !hx {
  52. continue
  53. }
  54. fd = ri.fieldDesc
  55. vx = ri.get(msx.pointer())
  56. vy = ri.get(msy.pointer())
  57. case *oneofInfo:
  58. fnx := ri.which(msx.pointer())
  59. fny := ri.which(msy.pointer())
  60. if fnx != fny {
  61. return false
  62. }
  63. if fnx <= 0 {
  64. continue
  65. }
  66. fi := mi.fields[fnx]
  67. fd = fi.fieldDesc
  68. vx = fi.get(msx.pointer())
  69. vy = fi.get(msy.pointer())
  70. }
  71. if !equalValue(fd, vx, vy) {
  72. return false
  73. }
  74. }
  75. // Compare extensions.
  76. // This is more complicated because mx or my could have empty/nil extension maps,
  77. // however some populated extension map values are equal to nil extension maps.
  78. emx := mi.extensionMap(msx.pointer())
  79. emy := mi.extensionMap(msy.pointer())
  80. if emx != nil {
  81. for k, x := range *emx {
  82. xd := x.Type().TypeDescriptor()
  83. xv := x.Value()
  84. var y ExtensionField
  85. ok := false
  86. if emy != nil {
  87. y, ok = (*emy)[k]
  88. }
  89. // We need to treat empty lists as equal to nil values
  90. if emy == nil || !ok {
  91. if xd.IsList() && xv.List().Len() == 0 {
  92. continue
  93. }
  94. return false
  95. }
  96. if !equalValue(xd, xv, y.Value()) {
  97. return false
  98. }
  99. }
  100. }
  101. if emy != nil {
  102. // emy may have extensions emx does not have, need to check them as well
  103. for k, y := range *emy {
  104. if emx != nil {
  105. // emx has the field, so we already checked it
  106. if _, ok := (*emx)[k]; ok {
  107. continue
  108. }
  109. }
  110. // Empty lists are equal to nil
  111. if y.Type().TypeDescriptor().IsList() && y.Value().List().Len() == 0 {
  112. continue
  113. }
  114. // Cant be equal if the extension is populated
  115. return false
  116. }
  117. }
  118. return equalUnknown(mx.GetUnknown(), my.GetUnknown())
  119. }
  120. func equalValue(fd protoreflect.FieldDescriptor, vx, vy protoreflect.Value) bool {
  121. // slow path
  122. if fd.Kind() != protoreflect.MessageKind {
  123. return vx.Equal(vy)
  124. }
  125. // fast path special cases
  126. if fd.IsMap() {
  127. if fd.MapValue().Kind() == protoreflect.MessageKind {
  128. return equalMessageMap(vx.Map(), vy.Map())
  129. }
  130. return vx.Equal(vy)
  131. }
  132. if fd.IsList() {
  133. return equalMessageList(vx.List(), vy.List())
  134. }
  135. return equalMessage(vx.Message(), vy.Message())
  136. }
  137. // Mostly copied from protoreflect.equalMap.
  138. // This variant only works for messages as map types.
  139. // All other map types should be handled via Value.Equal.
  140. func equalMessageMap(mx, my protoreflect.Map) bool {
  141. if mx.Len() != my.Len() {
  142. return false
  143. }
  144. equal := true
  145. mx.Range(func(k protoreflect.MapKey, vx protoreflect.Value) bool {
  146. if !my.Has(k) {
  147. equal = false
  148. return false
  149. }
  150. vy := my.Get(k)
  151. equal = equalMessage(vx.Message(), vy.Message())
  152. return equal
  153. })
  154. return equal
  155. }
  156. // Mostly copied from protoreflect.equalList.
  157. // The only change is the usage of equalImpl instead of protoreflect.equalValue.
  158. func equalMessageList(lx, ly protoreflect.List) bool {
  159. if lx.Len() != ly.Len() {
  160. return false
  161. }
  162. for i := 0; i < lx.Len(); i++ {
  163. // We only operate on messages here since equalImpl will not call us in any other case.
  164. if !equalMessage(lx.Get(i).Message(), ly.Get(i).Message()) {
  165. return false
  166. }
  167. }
  168. return true
  169. }
  170. // equalUnknown compares unknown fields by direct comparison on the raw bytes
  171. // of each individual field number.
  172. // Copied from protoreflect.equalUnknown.
  173. func equalUnknown(x, y protoreflect.RawFields) bool {
  174. if len(x) != len(y) {
  175. return false
  176. }
  177. if bytes.Equal([]byte(x), []byte(y)) {
  178. return true
  179. }
  180. mx := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
  181. my := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
  182. for len(x) > 0 {
  183. fnum, _, n := protowire.ConsumeField(x)
  184. mx[fnum] = append(mx[fnum], x[:n]...)
  185. x = x[n:]
  186. }
  187. for len(y) > 0 {
  188. fnum, _, n := protowire.ConsumeField(y)
  189. my[fnum] = append(my[fnum], y[:n]...)
  190. y = y[n:]
  191. }
  192. if len(mx) != len(my) {
  193. return false
  194. }
  195. for k, v1 := range mx {
  196. if v2, ok := my[k]; !ok || !bytes.Equal([]byte(v1), []byte(v2)) {
  197. return false
  198. }
  199. }
  200. return true
  201. }