github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/lib/deepcopy/deepcopy.go (about)

     1  // Package deepcopy makes deep copies of things. A standard copy will copy the
     2  // pointers: deep copy copies the values pointed to.  Unexported field
     3  // values are not copied.
     4  //
     5  // Copyright (c)2014-2016, Joel Scoble (github.com/mohae), all rights reserved.
     6  // License: MIT, for more details check the included LICENSE file.
     7  // https://github.com/mohae/deepcopy/blob/master/deepcopy.go
     8  package deepcopy
     9  
    10  import (
    11  	"reflect"
    12  	"time"
    13  )
    14  
    15  // Interface for delegating copy process to type
    16  type Interface interface {
    17  	DeepCopy() interface{}
    18  }
    19  
    20  // Iface is an alias to Copy; this exists for backwards compatibility reasons.
    21  func Iface(iface interface{}) interface{} {
    22  	return Copy(iface)
    23  }
    24  
    25  // Copy creates a deep copy of whatever is passed to it and returns the copy
    26  // in an interface{}.  The returned value will need to be asserted to the
    27  // correct type.
    28  func Copy(src interface{}) interface{} {
    29  	if src == nil {
    30  		return nil
    31  	}
    32  
    33  	// Make the interface a reflect.Value
    34  	original := reflect.ValueOf(src)
    35  
    36  	// Make a copy of the same type as the original.
    37  	cpy := reflect.New(original.Type()).Elem()
    38  
    39  	// Recursively copy the original.
    40  	copyRecursive(original, cpy)
    41  
    42  	// Return the copy as an interface.
    43  	return cpy.Interface()
    44  }
    45  
    46  // copyRecursive does the actual copying of the interface. It currently has
    47  // limited support for what it can handle. Add as needed.
    48  func copyRecursive(original, cpy reflect.Value) {
    49  	// check for implement deepcopy.Interface
    50  	if original.CanInterface() {
    51  		if copier, ok := original.Interface().(Interface); ok {
    52  			cpy.Set(reflect.ValueOf(copier.DeepCopy()))
    53  			return
    54  		}
    55  	}
    56  
    57  	// handle according to original's Kind
    58  	switch original.Kind() {
    59  	case reflect.Ptr:
    60  		// Get the actual value being pointed to.
    61  		originalValue := original.Elem()
    62  
    63  		// if  it isn't valid, return.
    64  		if !originalValue.IsValid() {
    65  			return
    66  		}
    67  		cpy.Set(reflect.New(originalValue.Type()))
    68  		copyRecursive(originalValue, cpy.Elem())
    69  
    70  	case reflect.Interface:
    71  		// If this is a nil, don't do anything
    72  		if original.IsNil() {
    73  			return
    74  		}
    75  		// Get the value for the interface, not the pointer.
    76  		originalValue := original.Elem()
    77  
    78  		// Get the value by calling Elem().
    79  		copyValue := reflect.New(originalValue.Type()).Elem()
    80  		copyRecursive(originalValue, copyValue)
    81  		cpy.Set(copyValue)
    82  
    83  	case reflect.Struct:
    84  		t, ok := original.Interface().(time.Time)
    85  		if ok {
    86  			cpy.Set(reflect.ValueOf(t))
    87  			return
    88  		}
    89  		// Go through each field of the struct and copy it.
    90  		for i := 0; i < original.NumField(); i++ {
    91  			// The Type's StructField for a given field is checked to see if StructField.PkgPath
    92  			// is set to determine if the field is exported or not because CanSet() returns false
    93  			// for settable fields.  I'm not sure why.  -mohae
    94  			if original.Type().Field(i).PkgPath != "" {
    95  				continue
    96  			}
    97  			copyRecursive(original.Field(i), cpy.Field(i))
    98  		}
    99  
   100  	case reflect.Slice:
   101  		if original.IsNil() {
   102  			return
   103  		}
   104  		// Make a new slice and copy each element.
   105  		cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
   106  		for i := 0; i < original.Len(); i++ {
   107  			copyRecursive(original.Index(i), cpy.Index(i))
   108  		}
   109  
   110  	case reflect.Map:
   111  		if original.IsNil() {
   112  			return
   113  		}
   114  		cpy.Set(reflect.MakeMap(original.Type()))
   115  		for _, key := range original.MapKeys() {
   116  			originalValue := original.MapIndex(key)
   117  			copyValue := reflect.New(originalValue.Type()).Elem()
   118  			copyRecursive(originalValue, copyValue)
   119  			copyKey := Copy(key.Interface())
   120  			cpy.SetMapIndex(reflect.ValueOf(copyKey), copyValue)
   121  		}
   122  
   123  	default:
   124  		cpy.Set(original)
   125  	}
   126  }