mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 00:57:27 +00:00
Util to test if all ptr fields are nil
This commit is contained in:
parent
d098456b6d
commit
e4d239cbb1
@ -20,6 +20,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -149,3 +150,28 @@ func ApplyOomScoreAdj(value int) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests whether all pointer fields in a struct are nil. This is useful when,
|
||||||
|
// for example, an API struct is handled by plugins which need to distinguish
|
||||||
|
// "no plugin accepted this spec" from "this spec is empty".
|
||||||
|
//
|
||||||
|
// This function is only valid for structs and pointers to structs. Any other
|
||||||
|
// type will cause a panic. Passing a typed nil pointer will return true.
|
||||||
|
func AllPtrFieldsNil(obj interface{}) bool {
|
||||||
|
v := reflect.ValueOf(obj)
|
||||||
|
if !v.IsValid() {
|
||||||
|
panic(fmt.Sprintf("reflect.ValueOf() produced a non-valid Value for %#v", obj))
|
||||||
|
}
|
||||||
|
if v.Kind() == reflect.Ptr {
|
||||||
|
if v.IsNil() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
v = v.Elem()
|
||||||
|
}
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
if v.Field(i).Kind() == reflect.Ptr && !v.Field(i).IsNil() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
@ -218,3 +218,48 @@ func TestCompileRegex(t *testing.T) {
|
|||||||
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
|
t.Errorf("Wrong regex returned: '%v': %v", uncompiledRegexes[1], regexes[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAllPtrFieldsNil(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
obj interface{}
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{struct{}{}, true},
|
||||||
|
{struct{ Foo int }{12345}, true},
|
||||||
|
{&struct{ Foo int }{12345}, true},
|
||||||
|
{struct{ Foo *int }{nil}, true},
|
||||||
|
{&struct{ Foo *int }{nil}, true},
|
||||||
|
{struct {
|
||||||
|
Foo int
|
||||||
|
Bar *int
|
||||||
|
}{12345, nil}, true},
|
||||||
|
{&struct {
|
||||||
|
Foo int
|
||||||
|
Bar *int
|
||||||
|
}{12345, nil}, true},
|
||||||
|
{struct {
|
||||||
|
Foo *int
|
||||||
|
Bar *int
|
||||||
|
}{nil, nil}, true},
|
||||||
|
{&struct {
|
||||||
|
Foo *int
|
||||||
|
Bar *int
|
||||||
|
}{nil, nil}, true},
|
||||||
|
{struct{ Foo *int }{new(int)}, false},
|
||||||
|
{&struct{ Foo *int }{new(int)}, false},
|
||||||
|
{struct {
|
||||||
|
Foo *int
|
||||||
|
Bar *int
|
||||||
|
}{nil, new(int)}, false},
|
||||||
|
{&struct {
|
||||||
|
Foo *int
|
||||||
|
Bar *int
|
||||||
|
}{nil, new(int)}, false},
|
||||||
|
{(*struct{})(nil), true},
|
||||||
|
}
|
||||||
|
for i, tc := range testCases {
|
||||||
|
if AllPtrFieldsNil(tc.obj) != tc.expected {
|
||||||
|
t.Errorf("case[%d]: expected %t, got %t", i, tc.expected, !tc.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user