Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ExtractList to support extraction from generic api.List{} #3753

Merged
merged 1 commit into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions pkg/runtime/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ func GetItemsPtr(list Object) (interface{}, error) {
if !items.IsValid() {
return nil, fmt.Errorf("no Items field in %#v", list)
}
if items.Kind() != reflect.Slice {
return nil, fmt.Errorf("Items field is not a slice")
switch items.Kind() {
case reflect.Interface, reflect.Ptr:
return items.Interface(), nil
case reflect.Slice:
return items.Addr().Interface(), nil
default:
return nil, fmt.Errorf("items: Expected slice, got %s", items.Kind())
}
return items.Addr().Interface(), nil
}

// ExtractList returns obj's Items element as an array of runtime.Objects.
Expand All @@ -61,11 +65,16 @@ func ExtractList(obj Object) ([]Object, error) {
list := make([]Object, items.Len())
for i := range list {
raw := items.Index(i)
item, ok := raw.Addr().Interface().(Object)
if !ok {
return nil, fmt.Errorf("item[%v]: Expected object, got %#v", i, raw.Interface())
var found bool
switch raw.Kind() {
case reflect.Interface, reflect.Ptr:
list[i], found = raw.Interface().(Object)
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())
}
list[i] = item
}
return list, nil
}
Expand Down
68 changes: 68 additions & 0 deletions pkg/runtime/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,74 @@ func TestExtractList(t *testing.T) {
}
}

func TestExtractListGeneric(t *testing.T) {
pl := &api.List{
Items: []runtime.Object{
&api.Pod{ObjectMeta: api.ObjectMeta{Name: "1"}},
&api.Service{ObjectMeta: api.ObjectMeta{Name: "2"}},
},
}
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].(*api.Pod); !ok {
t.Fatalf("Expected list[0] to be *api.Pod, it is %#v", obj)
}
if obj, ok := list[1].(*api.Service); !ok {
t.Fatalf("Expected list[1] to be *api.Service, it is %#v", obj)
}
}

type fakePtrInterfaceList struct {
Items []*runtime.Object
}

func (f fakePtrInterfaceList) IsAnAPIObject() {}

func TestExtractListOfInterfacePtrs(t *testing.T) {
pl := &fakePtrInterfaceList{
Items: []*runtime.Object{},
}
list, err := runtime.ExtractList(pl)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if len(list) > 0 {
t.Fatalf("Expected empty list, got %#v", list)
}
}

type fakePtrValueList struct {
Items []*api.Pod
}

func (f fakePtrValueList) IsAnAPIObject() {}

func TestExtractListOfValuePtrs(t *testing.T) {
pl := &fakePtrValueList{
Items: []*api.Pod{
{ObjectMeta: api.ObjectMeta{Name: "1"}},
{ObjectMeta: api.ObjectMeta{Name: "2"}},
},
}
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)
}
for i := range list {
if obj, ok := list[i].(*api.Pod); !ok {
t.Fatalf("Expected list[%d] to be *api.Pod, it is %#v", i, obj)
}
}
}

func TestSetList(t *testing.T) {
pl := &api.PodList{}
list := []runtime.Object{
Expand Down