-
Notifications
You must be signed in to change notification settings - Fork 63
reflect.DeepEqual, not even once #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
// Equals returns true iff the two Schemas are equal. | ||
func (a Schema) Equals(b Schema) bool { | ||
if len(a.Types) != len(b.Types) { | ||
return false | ||
} | ||
for i := range a.Types { | ||
if !a.Types[i].Equals(b.Types[i]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are they guaranteed to be sorted in the same order? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, but reflect.DeepEqual behaved like this too... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! |
||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns true iff the two TypeRefs are equal. | ||
// | ||
// Note that two typerefs that have an equivalent type but where one is | ||
// inlined and the other is named, are not considered equal. | ||
func (a TypeRef) Equals(b TypeRef) bool { | ||
if (a.NamedType == nil) != (b.NamedType == nil) { | ||
return false | ||
} | ||
if a.NamedType != nil { | ||
if *a.NamedType != *b.NamedType { | ||
return false | ||
} | ||
//return true | ||
} | ||
return a.Inlined.Equals(b.Inlined) | ||
} | ||
|
||
// Equals returns true iff the two TypeDefs are equal. | ||
func (a TypeDef) Equals(b TypeDef) bool { | ||
if a.Name != b.Name { | ||
return false | ||
} | ||
return a.Atom.Equals(b.Atom) | ||
} | ||
|
||
// Equals returns true iff the two Atoms are equal. | ||
func (a Atom) Equals(b Atom) bool { | ||
if (a.Scalar == nil) != (b.Scalar == nil) { | ||
return false | ||
} | ||
if (a.List == nil) != (b.List == nil) { | ||
return false | ||
} | ||
if (a.Map == nil) != (b.Map == nil) { | ||
return false | ||
} | ||
switch { | ||
case a.Scalar != nil: | ||
return *a.Scalar == *b.Scalar | ||
case a.List != nil: | ||
return a.List.Equals(*b.List) | ||
case a.Map != nil: | ||
return a.Map.Equals(*b.Map) | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns true iff the two Maps are equal. | ||
func (a Map) Equals(b Map) bool { | ||
if !a.ElementType.Equals(b.ElementType) { | ||
return false | ||
} | ||
if a.ElementRelationship != b.ElementRelationship { | ||
return false | ||
} | ||
if len(a.Fields) != len(b.Fields) { | ||
return false | ||
} | ||
for i := range a.Fields { | ||
if !a.Fields[i].Equals(b.Fields[i]) { | ||
return false | ||
} | ||
} | ||
if len(a.Unions) != len(b.Unions) { | ||
return false | ||
} | ||
for i := range a.Unions { | ||
if !a.Unions[i].Equals(b.Unions[i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns true iff the two Unions are equal. | ||
func (a Union) Equals(b Union) bool { | ||
if (a.Discriminator == nil) != (b.Discriminator == nil) { | ||
return false | ||
} | ||
if a.Discriminator != nil { | ||
if *a.Discriminator != *b.Discriminator { | ||
return false | ||
} | ||
} | ||
if a.DeduceInvalidDiscriminator != b.DeduceInvalidDiscriminator { | ||
return false | ||
} | ||
if len(a.Fields) != len(b.Fields) { | ||
return false | ||
} | ||
for i := range a.Fields { | ||
if !a.Fields[i].Equals(b.Fields[i]) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns true iff the two UnionFields are equal. | ||
func (a UnionField) Equals(b UnionField) bool { | ||
if a.FieldName != b.FieldName { | ||
return false | ||
} | ||
if a.DiscriminatorValue != b.DiscriminatorValue { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// Equals returns true iff the two StructFields are equal. | ||
func (a StructField) Equals(b StructField) bool { | ||
if a.Name != b.Name { | ||
return false | ||
} | ||
return a.Type.Equals(b.Type) | ||
} | ||
|
||
// Equals returns true iff the two Lists are equal. | ||
func (a List) Equals(b List) bool { | ||
if !a.ElementType.Equals(b.ElementType) { | ||
return false | ||
} | ||
if a.ElementRelationship != b.ElementRelationship { | ||
return false | ||
} | ||
if len(a.Keys) != len(b.Keys) { | ||
return false | ||
} | ||
for i := range a.Keys { | ||
if a.Keys[i] != b.Keys[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
func TestEquals(t *testing.T) { | ||
// In general this test will make sure people update things when they | ||
// add a field. | ||
// | ||
// The "copy known fields" section of these function is to break if folks | ||
// add new fields without fixing the Equals function and this test. | ||
funcs := []interface{}{ | ||
func(x Schema) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y Schema | ||
y.Types = x.Types | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x TypeDef) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y TypeDef | ||
y.Name = x.Name | ||
y.Atom = x.Atom | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x TypeRef) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y TypeRef | ||
y.NamedType = x.NamedType | ||
y.Inlined = x.Inlined | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x Atom) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y Atom | ||
y.Scalar = x.Scalar | ||
y.List = x.List | ||
y.Map = x.Map | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x Map) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y Map | ||
y.ElementType = x.ElementType | ||
y.ElementRelationship = x.ElementRelationship | ||
y.Fields = x.Fields | ||
y.Unions = x.Unions | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x Union) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y Union | ||
y.Discriminator = x.Discriminator | ||
y.DeduceInvalidDiscriminator = x.DeduceInvalidDiscriminator | ||
y.Fields = x.Fields | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x UnionField) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y UnionField | ||
y.DiscriminatorValue = x.DiscriminatorValue | ||
y.FieldName = x.FieldName | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x StructField) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y StructField | ||
y.Name = x.Name | ||
y.Type = x.Type | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
func(x List) bool { | ||
if !x.Equals(x) { | ||
return false | ||
} | ||
var y List | ||
y.ElementType = x.ElementType | ||
y.ElementRelationship = x.ElementRelationship | ||
y.Keys = x.Keys | ||
return x.Equals(y) == reflect.DeepEqual(x, y) | ||
}, | ||
} | ||
for i, f := range funcs { | ||
if err := quick.Check(f, nil); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
t.Errorf("%v: %v", i, err) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-)