-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_clone.go
60 lines (56 loc) · 1.41 KB
/
deep_clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package testit
import (
"errors"
"reflect"
)
var (
ErrNoMatchType = errors.New("no match type")
ErrNoPointer = errors.New("must be interface")
ErrInvalidArgument = errors.New("invalid arguments")
)
func deepCopy(dst, src reflect.Value) {
switch src.Kind() {
case reflect.Interface:
value := src.Elem()
if !value.IsValid() {
return
}
newValue := reflect.New(value.Type()).Elem()
deepCopy(newValue, value)
dst.Set(newValue)
case reflect.Ptr:
value := src.Elem()
if !value.IsValid() {
return
}
dst.Set(reflect.New(value.Type()))
deepCopy(dst.Elem(), value)
case reflect.Map:
dst.Set(reflect.MakeMap(src.Type()))
keys := src.MapKeys()
for _, key := range keys {
value := src.MapIndex(key)
newValue := reflect.New(value.Type()).Elem()
deepCopy(newValue, value)
dst.SetMapIndex(key, newValue)
}
case reflect.Slice:
dst.Set(reflect.MakeSlice(src.Type(), src.Len(), src.Cap()))
for i := 0; i < src.Len(); i++ {
deepCopy(dst.Index(i), src.Index(i))
}
case reflect.Struct:
for i := 0; i < src.NumField(); i++ {
deepCopy(dst.Field(i), src.Field(i))
}
default:
setValue(dst, src)
}
}
// DeepClone is a function to deep clone a value.
// It can clone public and private values, but not private strings, as they are immutable.
func DeepClone[T any](v T) T {
dst := reflect.New(reflect.TypeOf(v)).Elem()
deepCopy(dst, reflect.ValueOf(v))
return dst.Interface().(T)
}