equals.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Equals returns true iff the two Schemas are equal.
  15. func (a *Schema) Equals(b *Schema) bool {
  16. if a == nil || b == nil {
  17. return a == nil && b == nil
  18. }
  19. if len(a.Types) != len(b.Types) {
  20. return false
  21. }
  22. for i := range a.Types {
  23. if !a.Types[i].Equals(&b.Types[i]) {
  24. return false
  25. }
  26. }
  27. return true
  28. }
  29. // Equals returns true iff the two TypeRefs are equal.
  30. //
  31. // Note that two typerefs that have an equivalent type but where one is
  32. // inlined and the other is named, are not considered equal.
  33. func (a *TypeRef) Equals(b *TypeRef) bool {
  34. if a == nil || b == nil {
  35. return a == nil && b == nil
  36. }
  37. if (a.NamedType == nil) != (b.NamedType == nil) {
  38. return false
  39. }
  40. if a.NamedType != nil {
  41. if *a.NamedType != *b.NamedType {
  42. return false
  43. }
  44. //return true
  45. }
  46. return a.Inlined.Equals(&b.Inlined)
  47. }
  48. // Equals returns true iff the two TypeDefs are equal.
  49. func (a *TypeDef) Equals(b *TypeDef) bool {
  50. if a == nil || b == nil {
  51. return a == nil && b == nil
  52. }
  53. if a.Name != b.Name {
  54. return false
  55. }
  56. return a.Atom.Equals(&b.Atom)
  57. }
  58. // Equals returns true iff the two Atoms are equal.
  59. func (a *Atom) Equals(b *Atom) bool {
  60. if a == nil || b == nil {
  61. return a == nil && b == nil
  62. }
  63. if (a.Scalar == nil) != (b.Scalar == nil) {
  64. return false
  65. }
  66. if (a.List == nil) != (b.List == nil) {
  67. return false
  68. }
  69. if (a.Map == nil) != (b.Map == nil) {
  70. return false
  71. }
  72. switch {
  73. case a.Scalar != nil:
  74. return *a.Scalar == *b.Scalar
  75. case a.List != nil:
  76. return a.List.Equals(b.List)
  77. case a.Map != nil:
  78. return a.Map.Equals(b.Map)
  79. }
  80. return true
  81. }
  82. // Equals returns true iff the two Maps are equal.
  83. func (a *Map) Equals(b *Map) bool {
  84. if a == nil || b == nil {
  85. return a == nil && b == nil
  86. }
  87. if !a.ElementType.Equals(&b.ElementType) {
  88. return false
  89. }
  90. if a.ElementRelationship != b.ElementRelationship {
  91. return false
  92. }
  93. if len(a.Fields) != len(b.Fields) {
  94. return false
  95. }
  96. for i := range a.Fields {
  97. if !a.Fields[i].Equals(&b.Fields[i]) {
  98. return false
  99. }
  100. }
  101. if len(a.Unions) != len(b.Unions) {
  102. return false
  103. }
  104. for i := range a.Unions {
  105. if !a.Unions[i].Equals(&b.Unions[i]) {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. // Equals returns true iff the two Unions are equal.
  112. func (a *Union) Equals(b *Union) bool {
  113. if a == nil || b == nil {
  114. return a == nil && b == nil
  115. }
  116. if (a.Discriminator == nil) != (b.Discriminator == nil) {
  117. return false
  118. }
  119. if a.Discriminator != nil {
  120. if *a.Discriminator != *b.Discriminator {
  121. return false
  122. }
  123. }
  124. if a.DeduceInvalidDiscriminator != b.DeduceInvalidDiscriminator {
  125. return false
  126. }
  127. if len(a.Fields) != len(b.Fields) {
  128. return false
  129. }
  130. for i := range a.Fields {
  131. if !a.Fields[i].Equals(&b.Fields[i]) {
  132. return false
  133. }
  134. }
  135. return true
  136. }
  137. // Equals returns true iff the two UnionFields are equal.
  138. func (a *UnionField) Equals(b *UnionField) bool {
  139. if a == nil || b == nil {
  140. return a == nil && b == nil
  141. }
  142. if a.FieldName != b.FieldName {
  143. return false
  144. }
  145. if a.DiscriminatorValue != b.DiscriminatorValue {
  146. return false
  147. }
  148. return true
  149. }
  150. // Equals returns true iff the two StructFields are equal.
  151. func (a *StructField) Equals(b *StructField) bool {
  152. if a == nil || b == nil {
  153. return a == nil && b == nil
  154. }
  155. if a.Name != b.Name {
  156. return false
  157. }
  158. return a.Type.Equals(&b.Type)
  159. }
  160. // Equals returns true iff the two Lists are equal.
  161. func (a *List) Equals(b *List) bool {
  162. if a == nil || b == nil {
  163. return a == nil && b == nil
  164. }
  165. if !a.ElementType.Equals(&b.ElementType) {
  166. return false
  167. }
  168. if a.ElementRelationship != b.ElementRelationship {
  169. return false
  170. }
  171. if len(a.Keys) != len(b.Keys) {
  172. return false
  173. }
  174. for i := range a.Keys {
  175. if a.Keys[i] != b.Keys[i] {
  176. return false
  177. }
  178. }
  179. return true
  180. }