mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #94666 from soltysh/sort_nil
Handle nil elements when sorting, instead of panicking
This commit is contained in:
commit
2d8fbd61af
@ -206,6 +206,13 @@ func isLess(i, j reflect.Value) (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
|
|
||||||
case reflect.Interface:
|
case reflect.Interface:
|
||||||
|
if i.IsNil() && j.IsNil() {
|
||||||
|
return false, nil
|
||||||
|
} else if i.IsNil() {
|
||||||
|
return true, nil
|
||||||
|
} else if j.IsNil() {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
switch itype := i.Interface().(type) {
|
switch itype := i.Interface().(type) {
|
||||||
case uint8:
|
case uint8:
|
||||||
if jtype, ok := j.Interface().(uint8); ok {
|
if jtype, ok := j.Interface().(uint8); ok {
|
||||||
|
@ -409,6 +409,32 @@ func TestSortingPrinter(t *testing.T) {
|
|||||||
field: "{.invalid}",
|
field: "{.invalid}",
|
||||||
expectedErr: "couldn't find any field with path \"{.invalid}\" in the list of objects",
|
expectedErr: "couldn't find any field with path \"{.invalid}\" in the list of objects",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "empty fields",
|
||||||
|
obj: &corev1.EventList{
|
||||||
|
Items: []corev1.Event{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
|
||||||
|
LastTimestamp: metav1.Unix(300, 0),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
sort: &corev1.EventList{
|
||||||
|
Items: []corev1.Event{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(200, 0)},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(300, 0)},
|
||||||
|
LastTimestamp: metav1.Unix(300, 0),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
field: "{.lastTimestamp}",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name+" table", func(t *testing.T) {
|
t.Run(tt.name+" table", func(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user