mirror of https://github.com/k3s-io/k3s
commit
0e5478efae
|
@ -71,15 +71,18 @@ func ExtractList(obj Object) ([]Object, error) {
|
|||
list := make([]Object, items.Len())
|
||||
for i := range list {
|
||||
raw := items.Index(i)
|
||||
var found bool
|
||||
switch raw.Kind() {
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
list[i], found = raw.Interface().(Object)
|
||||
switch item := raw.Interface().(type) {
|
||||
case Object:
|
||||
list[i] = item
|
||||
case RawExtension:
|
||||
list[i] = &Unknown{
|
||||
RawJSON: item.RawJSON,
|
||||
}
|
||||
default:
|
||||
list[i], found = raw.Addr().Interface().(Object)
|
||||
}
|
||||
if !found {
|
||||
return nil, fmt.Errorf("item[%v]: Expected object, got %#v(%s)", i, raw.Interface(), raw.Kind())
|
||||
var found bool
|
||||
if list[i], found = raw.Addr().Interface().(Object); !found {
|
||||
return nil, fmt.Errorf("%v: item[%v]: Expected object, got %#v(%s)", obj, i, raw.Interface(), raw.Kind())
|
||||
}
|
||||
}
|
||||
}
|
||||
return list, nil
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/api/v1"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
|
||||
|
@ -87,6 +88,28 @@ func TestExtractListGeneric(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestExtractListGenericV1(t *testing.T) {
|
||||
pl := &v1.List{
|
||||
Items: []runtime.RawExtension{
|
||||
{RawJSON: []byte("foo")},
|
||||
{RawJSON: []byte("bar")},
|
||||
},
|
||||
}
|
||||
list, err := runtime.ExtractList(pl)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
if e, a := len(list), len(pl.Items); e != a {
|
||||
t.Fatalf("Expected %v, got %v", e, a)
|
||||
}
|
||||
if obj, ok := list[0].(*runtime.Unknown); !ok {
|
||||
t.Fatalf("Expected list[0] to be *runtime.Unknown, it is %#v", obj)
|
||||
}
|
||||
if obj, ok := list[1].(*runtime.Unknown); !ok {
|
||||
t.Fatalf("Expected list[1] to be *runtime.Unknown, it is %#v", obj)
|
||||
}
|
||||
}
|
||||
|
||||
type fakePtrInterfaceList struct {
|
||||
Items *[]runtime.Object
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue