mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 19:23:40 +00:00
Extract List reflect optimization
This commit is contained in:
parent
b8a3bd673d
commit
efc601302a
@ -214,8 +214,9 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
|
||||
list := make([]runtime.Object, items.Len())
|
||||
for i := range list {
|
||||
raw := items.Index(i)
|
||||
switch item := raw.Interface().(type) {
|
||||
case runtime.RawExtension:
|
||||
switch {
|
||||
case raw.Type() == rawExtensionObjectType:
|
||||
item := raw.Interface().(runtime.RawExtension)
|
||||
switch {
|
||||
case item.Object != nil:
|
||||
list[i] = item.Object
|
||||
@ -225,8 +226,8 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
|
||||
default:
|
||||
list[i] = nil
|
||||
}
|
||||
case runtime.Object:
|
||||
list[i] = item
|
||||
case raw.Type().Implements(objectType):
|
||||
list[i] = raw.Interface().(runtime.Object)
|
||||
default:
|
||||
var found bool
|
||||
if list[i], found = raw.Addr().Interface().(runtime.Object); !found {
|
||||
@ -237,8 +238,12 @@ func extractList(obj runtime.Object, allocNew bool) ([]runtime.Object, error) {
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// objectSliceType is the type of a slice of Objects
|
||||
var objectSliceType = reflect.TypeOf([]runtime.Object{})
|
||||
var (
|
||||
// objectSliceType is the type of a slice of Objects
|
||||
objectSliceType = reflect.TypeOf([]runtime.Object{})
|
||||
objectType = reflect.TypeOf((*runtime.Object)(nil)).Elem()
|
||||
rawExtensionObjectType = reflect.TypeOf(runtime.RawExtension{})
|
||||
)
|
||||
|
||||
// LenList returns the length of this list or 0 if it is not a list.
|
||||
func LenList(list runtime.Object) int {
|
||||
@ -273,7 +278,7 @@ func SetList(list runtime.Object, objects []runtime.Object) error {
|
||||
slice := reflect.MakeSlice(items.Type(), len(objects), len(objects))
|
||||
for i := range objects {
|
||||
dest := slice.Index(i)
|
||||
if dest.Type() == reflect.TypeOf(runtime.RawExtension{}) {
|
||||
if dest.Type() == rawExtensionObjectType {
|
||||
dest = dest.FieldByName("Object")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user