Merge pull request #75156 from smarterclayton/diff_invalid

Handle nil interface inputs to diff.ObjectReflectDiff
k3s-v1.15.3
Kubernetes Prow Robot 2019-03-20 00:55:01 -07:00 committed by GitHub
commit 481de197c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View File

@ -448,7 +448,6 @@ staging/src/k8s.io/apimachinery/pkg/test
staging/src/k8s.io/apimachinery/pkg/types
staging/src/k8s.io/apimachinery/pkg/util/cache
staging/src/k8s.io/apimachinery/pkg/util/clock
staging/src/k8s.io/apimachinery/pkg/util/diff
staging/src/k8s.io/apimachinery/pkg/util/errors
staging/src/k8s.io/apimachinery/pkg/util/framer
staging/src/k8s.io/apimachinery/pkg/util/httpstream

View File

@ -78,7 +78,20 @@ func ObjectGoPrintDiff(a, b interface{}) string {
)
}
// ObjectReflectDiff returns a multi-line formatted diff between two objects
// of equal type. If an object with private fields is passed you will
// only see string comparison for those fields. Otherwise this presents the
// most human friendly diff of two structs of equal type in this package.
func ObjectReflectDiff(a, b interface{}) string {
if a == nil && b == nil {
return "<no diffs>"
}
if a == nil {
return fmt.Sprintf("a is nil and b is not-nil")
}
if b == nil {
return fmt.Sprintf("a is not-nil and b is nil")
}
vA, vB := reflect.ValueOf(a), reflect.ValueOf(b)
if vA.Type() != vB.Type() {
return fmt.Sprintf("type A %T and type B %T do not match", a, b)

View File

@ -27,6 +27,20 @@ func TestObjectReflectDiff(t *testing.T) {
a, b interface{}
out string
}{
"both nil": {
a: interface{}(nil),
b: interface{}(nil),
},
"a nil": {
a: interface{}(nil),
b: "test",
out: "a is nil and b is not-nil",
},
"b nil": {
a: "test",
b: interface{}(nil),
out: "a is not-nil and b is nil",
},
"map": {
a: map[string]int{},
b: map[string]int{},