github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/meta/help.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package meta 18 19 import ( 20 "fmt" 21 "reflect" 22 23 "k8s.io/kubernetes/pkg/conversion" 24 "k8s.io/kubernetes/pkg/runtime" 25 ) 26 27 // IsListType returns true if the provided Object has a slice called Items 28 func IsListType(obj runtime.Object) bool { 29 _, err := GetItemsPtr(obj) 30 return err == nil 31 } 32 33 // GetItemsPtr returns a pointer to the list object's Items member. 34 // If 'list' doesn't have an Items member, it's not really a list type 35 // and an error will be returned. 36 // This function will either return a pointer to a slice, or an error, but not both. 37 func GetItemsPtr(list runtime.Object) (interface{}, error) { 38 v, err := conversion.EnforcePtr(list) 39 if err != nil { 40 return nil, err 41 } 42 items := v.FieldByName("Items") 43 if !items.IsValid() { 44 return nil, fmt.Errorf("no Items field in %#v", list) 45 } 46 switch items.Kind() { 47 case reflect.Interface, reflect.Ptr: 48 target := reflect.TypeOf(items.Interface()).Elem() 49 if target.Kind() != reflect.Slice { 50 return nil, fmt.Errorf("items: Expected slice, got %s", target.Kind()) 51 } 52 return items.Interface(), nil 53 case reflect.Slice: 54 return items.Addr().Interface(), nil 55 default: 56 return nil, fmt.Errorf("items: Expected slice, got %s", items.Kind()) 57 } 58 } 59 60 // ExtractList returns obj's Items element as an array of runtime.Objects. 61 // Returns an error if obj is not a List type (does not have an Items member). 62 func ExtractList(obj runtime.Object) ([]runtime.Object, error) { 63 itemsPtr, err := GetItemsPtr(obj) 64 if err != nil { 65 return nil, err 66 } 67 items, err := conversion.EnforcePtr(itemsPtr) 68 if err != nil { 69 return nil, err 70 } 71 list := make([]runtime.Object, items.Len()) 72 for i := range list { 73 raw := items.Index(i) 74 switch item := raw.Interface().(type) { 75 case runtime.RawExtension: 76 switch { 77 case item.Object != nil: 78 list[i] = item.Object 79 case item.Raw != nil: 80 // TODO: Set ContentEncoding and ContentType correctly. 81 list[i] = &runtime.Unknown{Raw: item.Raw} 82 default: 83 list[i] = nil 84 } 85 case runtime.Object: 86 list[i] = item 87 default: 88 var found bool 89 if list[i], found = raw.Addr().Interface().(runtime.Object); !found { 90 return nil, fmt.Errorf("%v: item[%v]: Expected object, got %#v(%s)", obj, i, raw.Interface(), raw.Kind()) 91 } 92 } 93 } 94 return list, nil 95 } 96 97 // objectSliceType is the type of a slice of Objects 98 var objectSliceType = reflect.TypeOf([]runtime.Object{}) 99 100 // SetList sets the given list object's Items member have the elements given in 101 // objects. 102 // Returns an error if list is not a List type (does not have an Items member), 103 // or if any of the objects are not of the right type. 104 func SetList(list runtime.Object, objects []runtime.Object) error { 105 itemsPtr, err := GetItemsPtr(list) 106 if err != nil { 107 return err 108 } 109 items, err := conversion.EnforcePtr(itemsPtr) 110 if err != nil { 111 return err 112 } 113 if items.Type() == objectSliceType { 114 items.Set(reflect.ValueOf(objects)) 115 return nil 116 } 117 slice := reflect.MakeSlice(items.Type(), len(objects), len(objects)) 118 for i := range objects { 119 dest := slice.Index(i) 120 src, err := conversion.EnforcePtr(objects[i]) 121 if err != nil { 122 return err 123 } 124 if src.Type().AssignableTo(dest.Type()) { 125 dest.Set(src) 126 } else if src.Type().ConvertibleTo(dest.Type()) { 127 dest.Set(src.Convert(dest.Type())) 128 } else { 129 return fmt.Errorf("item[%d]: can't assign or convert %v into %v", i, src.Type(), dest.Type()) 130 } 131 } 132 items.Set(slice) 133 return nil 134 }