equals.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package schema
  14. import "reflect"
  15. // Equals returns true iff the two Schemas are equal.
  16. func (a *Schema) Equals(b *Schema) bool {
  17. if a == nil || b == nil {
  18. return a == nil && b == nil
  19. }
  20. if len(a.Types) != len(b.Types) {
  21. return false
  22. }
  23. for i := range a.Types {
  24. if !a.Types[i].Equals(&b.Types[i]) {
  25. return false
  26. }
  27. }
  28. return true
  29. }
  30. // Equals returns true iff the two TypeRefs are equal.
  31. //
  32. // Note that two typerefs that have an equivalent type but where one is
  33. // inlined and the other is named, are not considered equal.
  34. func (a *TypeRef) Equals(b *TypeRef) bool {
  35. if a == nil || b == nil {
  36. return a == nil && b == nil
  37. }
  38. if (a.NamedType == nil) != (b.NamedType == nil) {
  39. return false
  40. }
  41. if a.NamedType != nil {
  42. if *a.NamedType != *b.NamedType {
  43. return false
  44. }
  45. //return true
  46. }
  47. if a.ElementRelationship != b.ElementRelationship {
  48. return false
  49. }
  50. return a.Inlined.Equals(&b.Inlined)
  51. }
  52. // Equals returns true iff the two TypeDefs are equal.
  53. func (a *TypeDef) Equals(b *TypeDef) bool {
  54. if a == nil || b == nil {
  55. return a == nil && b == nil
  56. }
  57. if a.Name != b.Name {
  58. return false
  59. }
  60. return a.Atom.Equals(&b.Atom)
  61. }
  62. // Equals returns true iff the two Atoms are equal.
  63. func (a *Atom) Equals(b *Atom) bool {
  64. if a == nil || b == nil {
  65. return a == nil && b == nil
  66. }
  67. if (a.Scalar == nil) != (b.Scalar == nil) {
  68. return false
  69. }
  70. if (a.List == nil) != (b.List == nil) {
  71. return false
  72. }
  73. if (a.Map == nil) != (b.Map == nil) {
  74. return false
  75. }
  76. switch {
  77. case a.Scalar != nil:
  78. return *a.Scalar == *b.Scalar
  79. case a.List != nil:
  80. return a.List.Equals(b.List)
  81. case a.Map != nil:
  82. return a.Map.Equals(b.Map)
  83. }
  84. return true
  85. }
  86. // Equals returns true iff the two Maps are equal.
  87. func (a *Map) Equals(b *Map) bool {
  88. if a == nil || b == nil {
  89. return a == nil && b == nil
  90. }
  91. if !a.ElementType.Equals(&b.ElementType) {
  92. return false
  93. }
  94. if a.ElementRelationship != b.ElementRelationship {
  95. return false
  96. }
  97. if len(a.Fields) != len(b.Fields) {
  98. return false
  99. }
  100. for i := range a.Fields {
  101. if !a.Fields[i].Equals(&b.Fields[i]) {
  102. return false
  103. }
  104. }
  105. if len(a.Unions) != len(b.Unions) {
  106. return false
  107. }
  108. for i := range a.Unions {
  109. if !a.Unions[i].Equals(&b.Unions[i]) {
  110. return false
  111. }
  112. }
  113. return true
  114. }
  115. // Equals returns true iff the two Unions are equal.
  116. func (a *Union) Equals(b *Union) bool {
  117. if a == nil || b == nil {
  118. return a == nil && b == nil
  119. }
  120. if (a.Discriminator == nil) != (b.Discriminator == nil) {
  121. return false
  122. }
  123. if a.Discriminator != nil {
  124. if *a.Discriminator != *b.Discriminator {
  125. return false
  126. }
  127. }
  128. if a.DeduceInvalidDiscriminator != b.DeduceInvalidDiscriminator {
  129. return false
  130. }
  131. if len(a.Fields) != len(b.Fields) {
  132. return false
  133. }
  134. for i := range a.Fields {
  135. if !a.Fields[i].Equals(&b.Fields[i]) {
  136. return false
  137. }
  138. }
  139. return true
  140. }
  141. // Equals returns true iff the two UnionFields are equal.
  142. func (a *UnionField) Equals(b *UnionField) bool {
  143. if a == nil || b == nil {
  144. return a == nil && b == nil
  145. }
  146. if a.FieldName != b.FieldName {
  147. return false
  148. }
  149. if a.DiscriminatorValue != b.DiscriminatorValue {
  150. return false
  151. }
  152. return true
  153. }
  154. // Equals returns true iff the two StructFields are equal.
  155. func (a *StructField) Equals(b *StructField) bool {
  156. if a == nil || b == nil {
  157. return a == nil && b == nil
  158. }
  159. if a.Name != b.Name {
  160. return false
  161. }
  162. if !reflect.DeepEqual(a.Default, b.Default) {
  163. return false
  164. }
  165. return a.Type.Equals(&b.Type)
  166. }
  167. // Equals returns true iff the two Lists are equal.
  168. func (a *List) Equals(b *List) bool {
  169. if a == nil || b == nil {
  170. return a == nil && b == nil
  171. }
  172. if !a.ElementType.Equals(&b.ElementType) {
  173. return false
  174. }
  175. if a.ElementRelationship != b.ElementRelationship {
  176. return false
  177. }
  178. if len(a.Keys) != len(b.Keys) {
  179. return false
  180. }
  181. for i := range a.Keys {
  182. if a.Keys[i] != b.Keys[i] {
  183. return false
  184. }
  185. }
  186. return true
  187. }