deepcopy.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "errors"
  10. "reflect"
  11. "time"
  12. )
  13. // copyContext 用于追踪已复制的对象,避免循环引用
  14. type copyContext struct {
  15. seen map[uintptr]reflect.Value
  16. deep int
  17. }
  18. // Interface for delegating copy process to type
  19. type DeepCopyer interface {
  20. DeepCopy() interface{}
  21. }
  22. // Copy creates a deep copy of whatever is passed to it and returns the copy
  23. // in an interface{}. The returned value will need to be asserted to the
  24. // correct type.
  25. // 支持以下类型的复制:
  26. // - 简单类型(int, string 等)
  27. // - 时间类型 (time.Time)
  28. // - 复合类型(map, slice, array)
  29. // - 指针类型
  30. // - 结构体(包含匿名字段)
  31. //
  32. // 特性:
  33. // - 支持循环引用检测
  34. // - 未导出字段不会被复制
  35. // - 实现了 DeepCopyer 接口的类型可以自定义复制行为
  36. // - 不支持的类型(Channel、Function等)会被跳过,不影响其他数据的复制
  37. //
  38. // 限制:
  39. // - 递归深度限制为20层
  40. // - 不支持的类型(Channel、Function等)会被跳过不处理
  41. func Copy(src interface{}) (interface{}, error) {
  42. if src == nil {
  43. return nil, nil
  44. }
  45. // Make the interface a reflect.Value
  46. original := reflect.ValueOf(src)
  47. // Make a copy of the same type as the original.
  48. cpy := reflect.New(original.Type()).Elem()
  49. // Create a new context for tracking copied objects
  50. ctx := &copyContext{
  51. seen: make(map[uintptr]reflect.Value),
  52. deep: 0,
  53. }
  54. // Recursively copy the original.
  55. if err := copyRecursive(ctx, original, cpy); err != nil {
  56. return nil, err
  57. }
  58. // Return the copy as an interface.
  59. return cpy.Interface(), nil
  60. }
  61. // isUnsupportedType checks if the given type is unsupported for deep copy
  62. func isUnsupportedType(k reflect.Kind) bool {
  63. switch k {
  64. case reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Invalid:
  65. return true
  66. }
  67. return false
  68. }
  69. // copyRecursive does the actual copying of the interface.
  70. // 支持的类型:
  71. // - 简单类型
  72. // - 时间类型 (time.Time)
  73. // - map, slice, array
  74. // - 指针
  75. // - 结构体及其匿名字段
  76. //
  77. // 特性:
  78. // - 使用 copyContext 处理循环引用
  79. // - 未导出字段会被跳过
  80. // - 支持 noCopy 标记的结构体
  81. // - 不支持的类型会被跳过不处理
  82. //
  83. // 参数:
  84. // - ctx: 复制上下文,用于追踪已复制的对象
  85. // - original: 原始值
  86. // - cpy: 目标值(指针的解引用)
  87. func copyRecursive(ctx *copyContext, original, cpy reflect.Value) error {
  88. ctx.deep++
  89. if ctx.deep > 20 {
  90. return errors.New("max recursion depth exceeded")
  91. }
  92. defer func() { ctx.deep-- }()
  93. // Handle nil pointer
  94. if !original.IsValid() {
  95. return nil
  96. }
  97. // 对不支持的类型直接跳过
  98. if isUnsupportedType(original.Kind()) {
  99. return nil
  100. }
  101. // Check for circular references
  102. if original.Kind() == reflect.Ptr || original.Kind() == reflect.Interface {
  103. if original.Kind() == reflect.Ptr {
  104. ptr := original.Pointer()
  105. if copied, ok := ctx.seen[ptr]; ok {
  106. cpy.Set(copied)
  107. return nil
  108. }
  109. if !original.IsNil() {
  110. ctx.seen[ptr] = cpy
  111. }
  112. }
  113. }
  114. // check for implement deepcopy.Interface
  115. if original.CanInterface() {
  116. if copier, ok := original.Interface().(DeepCopyer); ok {
  117. cpy.Set(reflect.ValueOf(copier.DeepCopy()))
  118. return nil
  119. }
  120. }
  121. var err error
  122. switch original.Kind() {
  123. case reflect.Ptr:
  124. originalValue := original.Elem()
  125. if !originalValue.IsValid() {
  126. return nil
  127. }
  128. cpy.Set(reflect.New(originalValue.Type()))
  129. err = copyRecursive(ctx, originalValue, cpy.Elem())
  130. case reflect.Interface:
  131. if original.IsNil() {
  132. return nil
  133. }
  134. originalValue := original.Elem()
  135. copyValue := reflect.New(originalValue.Type()).Elem()
  136. err = copyRecursive(ctx, originalValue, copyValue)
  137. if err == nil {
  138. cpy.Set(copyValue)
  139. }
  140. case reflect.Struct:
  141. t, ok := original.Interface().(time.Time)
  142. if ok {
  143. cpy.Set(reflect.ValueOf(t))
  144. return nil
  145. }
  146. oriType := original.Type()
  147. if _, ok := oriType.FieldByName("noCopy"); ok {
  148. return nil
  149. }
  150. for i := 0; i < original.NumField(); i++ {
  151. if !oriType.Field(i).IsExported() {
  152. continue
  153. }
  154. err = copyRecursive(ctx, original.Field(i), cpy.Field(i))
  155. if err != nil {
  156. // 忽略不支持类型的错误
  157. continue
  158. }
  159. }
  160. case reflect.Slice:
  161. if original.IsNil() {
  162. return nil
  163. }
  164. cpy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap()))
  165. for i := 0; i < original.Len(); i++ {
  166. err = copyRecursive(ctx, original.Index(i), cpy.Index(i))
  167. if err != nil {
  168. // 忽略不支持类型的错误
  169. continue
  170. }
  171. }
  172. case reflect.Map:
  173. if original.IsNil() {
  174. return nil
  175. }
  176. cpy.Set(reflect.MakeMap(original.Type()))
  177. for _, key := range original.MapKeys() {
  178. originalValue := original.MapIndex(key)
  179. copyValue := reflect.New(originalValue.Type()).Elem()
  180. err = copyRecursive(ctx, originalValue, copyValue)
  181. if err != nil {
  182. // 忽略不支持类型的错误
  183. continue
  184. }
  185. copyKey := reflect.New(key.Type()).Elem()
  186. err = copyRecursive(ctx, key, copyKey)
  187. if err != nil {
  188. // 忽略不支持类型的错误
  189. continue
  190. }
  191. cpy.SetMapIndex(copyKey, copyValue)
  192. }
  193. case reflect.Array:
  194. for i := 0; i < original.Len(); i++ {
  195. err = copyRecursive(ctx, original.Index(i), cpy.Index(i))
  196. if err != nil {
  197. // 忽略不支持类型的错误
  198. continue
  199. }
  200. }
  201. default:
  202. cpy.Set(original)
  203. }
  204. // 只返回非不支持类型的错误
  205. if err != nil && err.Error() != "max recursion depth exceeded" {
  206. return nil
  207. }
  208. return err
  209. }