...

Source file src/sigs.k8s.io/structured-merge-diff/v4/schema/equals.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/schema

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

View as plain text