deepcopy.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // 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. package deepcopy
  8. import (
  9. "reflect"
  10. "time"
  11. )
  12. // Iface is an alias to Copy; this exists for backwards compatibility reasons.
  13. func Iface(iface interface{}) interface{} {
  14. return Copy(iface)
  15. }
  16. // Copy creates a deep copy of whatever is passed to it and returns the copy
  17. // in an interface{}. The returned value will need to be asserted to the
  18. // correct type.
  19. func Copy(src interface{}) interface{} {
  20. if src == nil {
  21. return nil
  22. }
  23. // Make the interface a reflect.Value
  24. original := reflect.ValueOf(src)
  25. // Make a copy of the same type as the original.
  26. cpy := reflect.New(original.Type()).Elem()
  27. // Recursively copy the original.
  28. copyRecursive(original, cpy)
  29. // Return the copy as an interface.
  30. return cpy.Interface()
  31. }
  32. // copyRecursive does the actual copying of the interface. It currently has
  33. // limited support for what it can handle. Add as needed.
  34. func copyRecursive(original, cpy reflect.Value) {
  35. // handle according to original's Kind
  36. switch original.Kind() {
  37. case reflect.Ptr:
  38. // Get the actual value being pointed to.
  39. originalValue := original.Elem()
  40. // if it isn't valid, return.
  41. if !originalValue.IsValid() {
  42. return
  43. }
  44. cpy.Set(reflect.New(originalValue.Type()))
  45. copyRecursive(originalValue, cpy.Elem())
  46. case reflect.Interface:
  47. // If this is a nil, don't do anything
  48. if original.IsNil() {
  49. return
  50. }
  51. // Get the value for the interface, not the pointer.
  52. originalValue := original.Elem()
  53. // Get the value by calling Elem().
  54. copyValue := reflect.New(originalValue.Type()).Elem()
  55. copyRecursive(originalValue, copyValue)
  56. cpy.Set(copyValue)
  57. case reflect.Struct:
  58. t, ok := original.Interface().(time.Time)
  59. if ok {
  60. cpy.Set(reflect.ValueOf(t))
  61. return
  62. }
  63. // Go through each field of the struct and copy it.
  64. for i := 0; i < original.NumField(); i++ {
  65. // The Type's StructField for a given field is checked to see if StructField.PkgPath
  66. // is set to determine if the field is exported or not because CanSet() returns false
  67. // for settable fields. I'm not sure why. -mohae
  68. if original.Type().Field(i).PkgPath != "" {
  69. continue
  70. }
  71. copyRecursive(original.Field(i), cpy.Field(i))
  72. }
  73. case reflect.Slice:
  74. if original.IsNil() {
  75. return
  76. }
  77. // Make a new slice and copy each element.
  78. cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
  79. for i := 0; i < original.Len(); i++ {
  80. copyRecursive(original.Index(i), cpy.Index(i))
  81. }
  82. case reflect.Map:
  83. if original.IsNil() {
  84. return
  85. }
  86. cpy.Set(reflect.MakeMap(original.Type()))
  87. for _, key := range original.MapKeys() {
  88. originalValue := original.MapIndex(key)
  89. copyValue := reflect.New(originalValue.Type()).Elem()
  90. copyRecursive(originalValue, copyValue)
  91. copyKey := Copy(key.Interface())
  92. cpy.SetMapIndex(reflect.ValueOf(copyKey), copyValue)
  93. }
  94. default:
  95. cpy.Set(original)
  96. }
  97. }