PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/proto/equal.go

https://code.google.com/p/goprotobuf/
Go | 219 lines | 132 code | 17 blank | 70 comment | 56 complexity | a69e9a917ec7fef4b519b1f42eee637f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 Google Inc. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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 protocol buffer structs,
  43. or both be pointers to protocol buffer structs.
  44. Equality is defined in this way:
  45. - Two messages are equal iff they are the same type,
  46. corresponding fields are equal, unknown field sets
  47. are equal, and extensions sets are equal.
  48. - Two set scalar fields are equal iff their values are equal.
  49. If the fields are of a floating-point type, remember that
  50. NaN != x for all x, including NaN.
  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. (TODO)
  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 interface{}) bool {
  63. v1, v2 := reflect.ValueOf(a), reflect.ValueOf(b)
  64. if v1.Type() != v2.Type() {
  65. return false
  66. }
  67. if v1.Kind() == reflect.Ptr {
  68. v1, v2 = v1.Elem(), v2.Elem()
  69. }
  70. if v1.Kind() != reflect.Struct {
  71. return false
  72. }
  73. return equalStruct(v1, v2)
  74. }
  75. // v1 and v2 are known to have the same type.
  76. func equalStruct(v1, v2 reflect.Value) bool {
  77. for i := 0; i < v1.NumField(); i++ {
  78. f := v1.Type().Field(i)
  79. if strings.HasPrefix(f.Name, "XXX_") {
  80. continue
  81. }
  82. f1, f2 := v1.Field(i), v2.Field(i)
  83. if f.Type.Kind() == reflect.Ptr {
  84. if n1, n2 := f1.IsNil(), f2.IsNil(); n1 && n2 {
  85. // both unset
  86. continue
  87. } else if n1 != n2 {
  88. // set/unset mismatch
  89. return false
  90. }
  91. b1, ok := f1.Interface().(raw)
  92. if ok {
  93. b2 := f2.Interface().(raw)
  94. // RawMessage
  95. if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
  96. return false
  97. }
  98. continue
  99. }
  100. f1, f2 = f1.Elem(), f2.Elem()
  101. }
  102. if !equalAny(f1, f2) {
  103. return false
  104. }
  105. }
  106. if em1 := v1.FieldByName("XXX_extensions"); em1.IsValid() {
  107. em2 := v2.FieldByName("XXX_extensions")
  108. if !equalExtensions(v1.Type(), em1.Interface().(map[int32]Extension), em2.Interface().(map[int32]Extension)) {
  109. return false
  110. }
  111. }
  112. // TODO: Deal with XXX_unrecognized.
  113. return true
  114. }
  115. // v1 and v2 are known to have the same type.
  116. func equalAny(v1, v2 reflect.Value) bool {
  117. switch v1.Kind() {
  118. case reflect.Bool:
  119. return v1.Bool() == v2.Bool()
  120. case reflect.Float32, reflect.Float64:
  121. return v1.Float() == v2.Float()
  122. case reflect.Int32, reflect.Int64:
  123. return v1.Int() == v2.Int()
  124. case reflect.Ptr:
  125. return equalAny(v1.Elem(), v2.Elem())
  126. case reflect.Slice:
  127. if v1.Type().Elem().Kind() == reflect.Uint8 {
  128. // short circuit: []byte
  129. if v1.IsNil() != v2.IsNil() {
  130. return false
  131. }
  132. return bytes.Equal(v1.Interface().([]byte), v2.Interface().([]byte))
  133. }
  134. if v1.Len() != v2.Len() {
  135. return false
  136. }
  137. for i := 0; i < v1.Len(); i++ {
  138. if !equalAny(v1.Index(i), v2.Index(i)) {
  139. return false
  140. }
  141. }
  142. return true
  143. case reflect.String:
  144. return v1.Interface().(string) == v2.Interface().(string)
  145. case reflect.Struct:
  146. return equalStruct(v1, v2)
  147. case reflect.Uint32, reflect.Uint64:
  148. return v1.Uint() == v2.Uint()
  149. }
  150. // unknown type, so not a protocol buffer
  151. log.Printf("proto: don't know how to compare %v", v1)
  152. return false
  153. }
  154. // base is the struct type that the extensions are based on.
  155. // em1 and em2 are extension maps.
  156. func equalExtensions(base reflect.Type, em1, em2 map[int32]Extension) bool {
  157. if len(em1) != len(em2) {
  158. return false
  159. }
  160. for extNum, e1 := range em1 {
  161. e2, ok := em2[extNum]
  162. if !ok {
  163. return false
  164. }
  165. m1, m2 := e1.value, e2.value
  166. if m1 != nil && m2 != nil {
  167. // Both are unencoded.
  168. if !Equal(m1, m2) {
  169. return false
  170. }
  171. continue
  172. }
  173. // At least one is encoded. To do a semantically correct comparison
  174. // we need to unmarshal them first.
  175. var desc *ExtensionDesc
  176. if m := extensionMaps[base]; m != nil {
  177. desc = m[extNum]
  178. }
  179. if desc == nil {
  180. log.Printf("proto: don't know how to compare extension %d of %v", extNum, base)
  181. continue
  182. }
  183. var err error
  184. if m1 == nil {
  185. m1, err = decodeExtension(e1.enc, desc)
  186. }
  187. if m2 == nil && err == nil {
  188. m2, err = decodeExtension(e2.enc, desc)
  189. }
  190. if err != nil {
  191. // The encoded form is invalid.
  192. log.Printf("proto: badly encoded extension %d of %v: %v", extNum, base, err)
  193. return false
  194. }
  195. if !Equal(m1, m2) {
  196. return false
  197. }
  198. }
  199. return true
  200. }