equals.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. return a.Inlined.Equals(&b.Inlined)
  48. }
  49. // Equals returns true iff the two TypeDefs are equal.
  50. func (a *TypeDef) Equals(b *TypeDef) bool {
  51. if a == nil || b == nil {
  52. return a == nil && b == nil
  53. }
  54. if a.Name != b.Name {
  55. return false
  56. }
  57. return a.Atom.Equals(&b.Atom)
  58. }
  59. // Equals returns true iff the two Atoms are equal.
  60. func (a *Atom) Equals(b *Atom) bool {
  61. if a == nil || b == nil {
  62. return a == nil && b == nil
  63. }
  64. if (a.Scalar == nil) != (b.Scalar == nil) {
  65. return false
  66. }
  67. if (a.List == nil) != (b.List == nil) {
  68. return false
  69. }
  70. if (a.Map == nil) != (b.Map == nil) {
  71. return false
  72. }
  73. switch {
  74. case a.Scalar != nil:
  75. return *a.Scalar == *b.Scalar
  76. case a.List != nil:
  77. return a.List.Equals(b.List)
  78. case a.Map != nil:
  79. return a.Map.Equals(b.Map)
  80. }
  81. return true
  82. }
  83. // Equals returns true iff the two Maps are equal.
  84. func (a *Map) Equals(b *Map) bool {
  85. if a == nil || b == nil {
  86. return a == nil && b == nil
  87. }
  88. if !a.ElementType.Equals(&b.ElementType) {
  89. return false
  90. }
  91. if a.ElementRelationship != b.ElementRelationship {
  92. return false
  93. }
  94. if len(a.Fields) != len(b.Fields) {
  95. return false
  96. }
  97. for i := range a.Fields {
  98. if !a.Fields[i].Equals(&b.Fields[i]) {
  99. return false
  100. }
  101. }
  102. if len(a.Unions) != len(b.Unions) {
  103. return false
  104. }
  105. for i := range a.Unions {
  106. if !a.Unions[i].Equals(&b.Unions[i]) {
  107. return false
  108. }
  109. }
  110. return true
  111. }
  112. // Equals returns true iff the two Unions are equal.
  113. func (a *Union) Equals(b *Union) bool {
  114. if a == nil || b == nil {
  115. return a == nil && b == nil
  116. }
  117. if (a.Discriminator == nil) != (b.Discriminator == nil) {
  118. return false
  119. }
  120. if a.Discriminator != nil {
  121. if *a.Discriminator != *b.Discriminator {
  122. return false
  123. }
  124. }
  125. if a.DeduceInvalidDiscriminator != b.DeduceInvalidDiscriminator {
  126. return false
  127. }
  128. if len(a.Fields) != len(b.Fields) {
  129. return false
  130. }
  131. for i := range a.Fields {
  132. if !a.Fields[i].Equals(&b.Fields[i]) {
  133. return false
  134. }
  135. }
  136. return true
  137. }
  138. // Equals returns true iff the two UnionFields are equal.
  139. func (a *UnionField) Equals(b *UnionField) bool {
  140. if a == nil || b == nil {
  141. return a == nil && b == nil
  142. }
  143. if a.FieldName != b.FieldName {
  144. return false
  145. }
  146. if a.DiscriminatorValue != b.DiscriminatorValue {
  147. return false
  148. }
  149. return true
  150. }
  151. // Equals returns true iff the two StructFields are equal.
  152. func (a *StructField) Equals(b *StructField) bool {
  153. if a == nil || b == nil {
  154. return a == nil && b == nil
  155. }
  156. if a.Name != b.Name {
  157. return false
  158. }
  159. if !reflect.DeepEqual(a.Default, b.Default) {
  160. return false
  161. }
  162. return a.Type.Equals(&b.Type)
  163. }
  164. // Equals returns true iff the two Lists are equal.
  165. func (a *List) Equals(b *List) bool {
  166. if a == nil || b == nil {
  167. return a == nil && b == nil
  168. }
  169. if !a.ElementType.Equals(&b.ElementType) {
  170. return false
  171. }
  172. if a.ElementRelationship != b.ElementRelationship {
  173. return false
  174. }
  175. if len(a.Keys) != len(b.Keys) {
  176. return false
  177. }
  178. for i := range a.Keys {
  179. if a.Keys[i] != b.Keys[i] {
  180. return false
  181. }
  182. }
  183. return true
  184. }