github.com/galaxyobe/gen@v0.0.0-20220910125335-392fa8f0990f/cmd/deepcopy-gen/output_tests/reflect_deepcopy.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     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 output_tests
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  )
    23  
    24  // ReflectDeepCopy deep copies the object using reflection.
    25  func ReflectDeepCopy(in interface{}) interface{} {
    26  	return reflectDeepCopy(reflect.ValueOf(in)).Interface()
    27  }
    28  
    29  func reflectDeepCopy(src reflect.Value) reflect.Value {
    30  	switch src.Kind() {
    31  	case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
    32  		if src.IsNil() {
    33  			return src
    34  		}
    35  	}
    36  
    37  	switch src.Kind() {
    38  	case reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Uintptr:
    39  		panic(fmt.Sprintf("cannot deep copy kind: %s", src.Kind()))
    40  	case reflect.Array:
    41  		dst := reflect.New(src.Type())
    42  		for i := 0; i < src.Len(); i++ {
    43  			dst.Elem().Index(i).Set(reflectDeepCopy(src.Index(i)))
    44  		}
    45  		return dst.Elem()
    46  	case reflect.Interface:
    47  		return reflectDeepCopy(src.Elem())
    48  	case reflect.Map:
    49  		dst := reflect.MakeMap(src.Type())
    50  		for _, k := range src.MapKeys() {
    51  			dst.SetMapIndex(k, reflectDeepCopy(src.MapIndex(k)))
    52  		}
    53  		return dst
    54  	case reflect.Ptr:
    55  		dst := reflect.New(src.Type().Elem())
    56  		dst.Elem().Set(reflectDeepCopy(src.Elem()))
    57  		return dst
    58  	case reflect.Slice:
    59  		dst := reflect.MakeSlice(src.Type(), 0, src.Len())
    60  		for i := 0; i < src.Len(); i++ {
    61  			dst = reflect.Append(dst, reflectDeepCopy(src.Index(i)))
    62  		}
    63  		return dst
    64  	case reflect.Struct:
    65  		dst := reflect.New(src.Type())
    66  		for i := 0; i < src.NumField(); i++ {
    67  			if !dst.Elem().Field(i).CanSet() {
    68  				// Can't set private fields. At this point, the
    69  				// best we can do is a shallow copy. For
    70  				// example, time.Time is a value type with
    71  				// private members that can be shallow copied.
    72  				return src
    73  			}
    74  			dst.Elem().Field(i).Set(reflectDeepCopy(src.Field(i)))
    75  		}
    76  		return dst.Elem()
    77  	default:
    78  		// Value types like numbers, booleans, and strings.
    79  		return src
    80  	}
    81  }