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

This commit is contained in:
Michal Fojtik
2015-01-23 09:49:02 +01:00
parent e0acd75629
commit e7df8aa098
2 changed files with 84 additions and 7 deletions

View File

@@ -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.
@@ -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
}