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
3ef37771
Commit
3ef37771
authored
Mar 05, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make unexported fields panic (informatively)
...Also fix some incorrect calls to semantic.DeepEqual, and a bug where it returned true incorrectly.
parent
29020284
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
10 deletions
+44
-10
helpers.go
pkg/api/helpers.go
+3
-0
request_test.go
pkg/client/request_test.go
+2
-2
deep_equal.go
pkg/conversion/deep_equal.go
+37
-7
rest_test.go
pkg/registry/controller/rest_test.go
+2
-1
No files found.
pkg/api/helpers.go
View file @
3ef37771
...
@@ -60,6 +60,9 @@ var Semantic = conversion.EqualitiesOrDie(
...
@@ -60,6 +60,9 @@ var Semantic = conversion.EqualitiesOrDie(
}
}
return
a
.
Amount
.
Cmp
(
b
.
Amount
)
==
0
return
a
.
Amount
.
Cmp
(
b
.
Amount
)
==
0
},
},
func
(
a
,
b
util
.
Time
)
bool
{
return
a
.
Unix
()
==
b
.
Unix
()
},
)
)
var
standardResources
=
util
.
NewStringSet
(
var
standardResources
=
util
.
NewStringSet
(
...
...
pkg/client/request_test.go
View file @
3ef37771
...
@@ -27,7 +27,7 @@ import (
...
@@ -27,7 +27,7 @@ import (
"net/http/httptest"
"net/http/httptest"
"net/url"
"net/url"
"os"
"os"
//
"reflect"
"reflect"
"strings"
"strings"
"testing"
"testing"
"time"
"time"
...
@@ -64,7 +64,7 @@ func TestRequestWithErrorWontChange(t *testing.T) {
...
@@ -64,7 +64,7 @@ func TestRequestWithErrorWontChange(t *testing.T) {
if
changed
!=
&
r
{
if
changed
!=
&
r
{
t
.
Errorf
(
"returned request should point to the same object"
)
t
.
Errorf
(
"returned request should point to the same object"
)
}
}
if
!
api
.
Semantic
.
DeepDerivative
(
changed
,
&
original
)
{
if
!
reflect
.
DeepEqual
(
changed
,
&
original
)
{
t
.
Errorf
(
"expected %#v, got %#v"
,
&
original
,
changed
)
t
.
Errorf
(
"expected %#v, got %#v"
,
&
original
,
changed
)
}
}
}
}
...
...
pkg/conversion/deep_equal.go
View file @
3ef37771
...
@@ -19,6 +19,7 @@ package conversion
...
@@ -19,6 +19,7 @@ package conversion
import
(
import
(
"fmt"
"fmt"
"reflect"
"reflect"
"strings"
)
)
// Equalities is a map from type to a function comparing two values of
// Equalities is a map from type to a function comparing two values of
...
@@ -99,10 +100,36 @@ type visit struct {
...
@@ -99,10 +100,36 @@ type visit struct {
typ
reflect
.
Type
typ
reflect
.
Type
}
}
// unexportedTypePanic is thrown when you use this DeepEqual on something that has an
// unexported type. It indicates a programmer error, so should not occur at runtime,
// which is why it's not public and thus impossible to catch.
type
unexportedTypePanic
[]
reflect
.
Type
func
(
u
unexportedTypePanic
)
Error
()
string
{
return
u
.
String
()
}
func
(
u
unexportedTypePanic
)
String
()
string
{
strs
:=
make
([]
string
,
len
(
u
))
for
i
,
t
:=
range
u
{
strs
[
i
]
=
fmt
.
Sprintf
(
"%v"
,
t
)
}
return
"an unexported field was encountered, nested like this: "
+
strings
.
Join
(
strs
,
" -> "
)
}
func
makeUsefulPanic
(
v
reflect
.
Value
)
{
if
x
:=
recover
();
x
!=
nil
{
if
u
,
ok
:=
x
.
(
unexportedTypePanic
);
ok
{
u
=
append
(
unexportedTypePanic
{
v
.
Type
()},
u
...
)
x
=
u
}
panic
(
x
)
}
}
// Tests for deep equality using reflected types. The map argument tracks
// Tests for deep equality using reflected types. The map argument tracks
// comparisons that have already been seen, which allows short circuiting on
// comparisons that have already been seen, which allows short circuiting on
// recursive types.
// recursive types.
func
(
e
Equalities
)
deepValueEqual
(
v1
,
v2
reflect
.
Value
,
visited
map
[
visit
]
bool
,
depth
int
)
bool
{
func
(
e
Equalities
)
deepValueEqual
(
v1
,
v2
reflect
.
Value
,
visited
map
[
visit
]
bool
,
depth
int
)
bool
{
defer
makeUsefulPanic
(
v1
)
if
!
v1
.
IsValid
()
||
!
v2
.
IsValid
()
{
if
!
v1
.
IsValid
()
||
!
v2
.
IsValid
()
{
return
v1
.
IsValid
()
==
v2
.
IsValid
()
return
v1
.
IsValid
()
==
v2
.
IsValid
()
}
}
...
@@ -207,10 +234,10 @@ func (e Equalities) deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool,
...
@@ -207,10 +234,10 @@ func (e Equalities) deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool,
return
false
return
false
default
:
default
:
// Normal equality suffices
// Normal equality suffices
if
v1
.
CanInterface
()
&&
v2
.
CanInterface
()
{
if
!
v1
.
CanInterface
()
||
!
v2
.
CanInterface
()
{
return
v1
.
Interface
()
==
v2
.
Interface
(
)
panic
(
unexportedTypePanic
{}
)
}
}
return
v1
.
CanInterface
()
==
v2
.
Can
Interface
()
return
v1
.
Interface
()
==
v2
.
Interface
()
}
}
}
}
...
@@ -221,7 +248,8 @@ func (e Equalities) deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool,
...
@@ -221,7 +248,8 @@ func (e Equalities) deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool,
//
//
// An empty slice *is* equal to a nil slice for our purposes; same for maps.
// An empty slice *is* equal to a nil slice for our purposes; same for maps.
//
//
// Unexported field members are not compared.
// Unexported field members cannot be compared and will cause an imformative panic; you must add an Equality
// function for these types.
func
(
e
Equalities
)
DeepEqual
(
a1
,
a2
interface
{})
bool
{
func
(
e
Equalities
)
DeepEqual
(
a1
,
a2
interface
{})
bool
{
if
a1
==
nil
||
a2
==
nil
{
if
a1
==
nil
||
a2
==
nil
{
return
a1
==
a2
return
a1
==
a2
...
@@ -235,6 +263,8 @@ func (e Equalities) DeepEqual(a1, a2 interface{}) bool {
...
@@ -235,6 +263,8 @@ func (e Equalities) DeepEqual(a1, a2 interface{}) bool {
}
}
func
(
e
Equalities
)
deepValueDerive
(
v1
,
v2
reflect
.
Value
,
visited
map
[
visit
]
bool
,
depth
int
)
bool
{
func
(
e
Equalities
)
deepValueDerive
(
v1
,
v2
reflect
.
Value
,
visited
map
[
visit
]
bool
,
depth
int
)
bool
{
defer
makeUsefulPanic
(
v1
)
if
!
v1
.
IsValid
()
||
!
v2
.
IsValid
()
{
if
!
v1
.
IsValid
()
||
!
v2
.
IsValid
()
{
return
v1
.
IsValid
()
==
v2
.
IsValid
()
return
v1
.
IsValid
()
==
v2
.
IsValid
()
}
}
...
@@ -347,10 +377,10 @@ func (e Equalities) deepValueDerive(v1, v2 reflect.Value, visited map[visit]bool
...
@@ -347,10 +377,10 @@ func (e Equalities) deepValueDerive(v1, v2 reflect.Value, visited map[visit]bool
return
false
return
false
default
:
default
:
// Normal equality suffices
// Normal equality suffices
if
v1
.
CanInterface
()
&&
v2
.
CanInterface
()
{
if
!
v1
.
CanInterface
()
||
!
v2
.
CanInterface
()
{
return
v1
.
Interface
()
==
v2
.
Interface
(
)
panic
(
unexportedTypePanic
{}
)
}
}
return
v1
.
CanInterface
()
==
v2
.
Can
Interface
()
return
v1
.
Interface
()
==
v2
.
Interface
()
}
}
}
}
...
...
pkg/registry/controller/rest_test.go
View file @
3ef37771
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"reflect"
"strings"
"strings"
"testing"
"testing"
...
@@ -378,7 +379,7 @@ func TestFillCurrentState(t *testing.T) {
...
@@ -378,7 +379,7 @@ func TestFillCurrentState(t *testing.T) {
if
controller
.
Status
.
Replicas
!=
2
{
if
controller
.
Status
.
Replicas
!=
2
{
t
.
Errorf
(
"expected 2, got: %d"
,
controller
.
Status
.
Replicas
)
t
.
Errorf
(
"expected 2, got: %d"
,
controller
.
Status
.
Replicas
)
}
}
if
!
api
.
Semantic
.
DeepEqual
(
fakeLister
.
s
,
labels
.
Set
(
controller
.
Spec
.
Selector
)
.
AsSelector
())
{
if
!
reflect
.
DeepEqual
(
fakeLister
.
s
,
labels
.
Set
(
controller
.
Spec
.
Selector
)
.
AsSelector
())
{
t
.
Errorf
(
"unexpected output: %#v %#v"
,
labels
.
Set
(
controller
.
Spec
.
Selector
)
.
AsSelector
(),
fakeLister
.
s
)
t
.
Errorf
(
"unexpected output: %#v %#v"
,
labels
.
Set
(
controller
.
Spec
.
Selector
)
.
AsSelector
(),
fakeLister
.
s
)
}
}
}
}
...
...
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