Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
e4d239cb
Commit
e4d239cb
authored
Nov 24, 2014
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Util to test if all ptr fields are nil
parent
d098456b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
0 deletions
+71
-0
util.go
pkg/util/util.go
+26
-0
util_test.go
pkg/util/util_test.go
+45
-0
No files found.
pkg/util/util.go
View file @
e4d239cb
...
@@ -20,6 +20,7 @@ import (
...
@@ -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 {
...
@@ -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
}
pkg/util/util_test.go
View file @
e4d239cb
...
@@ -218,3 +218,48 @@ func TestCompileRegex(t *testing.T) {
...
@@ -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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment