-
Notifications
You must be signed in to change notification settings - Fork 269
/
deepcopy.go
139 lines (111 loc) · 2.85 KB
/
deepcopy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package deepcopy
import (
"reflect"
"sync"
)
type deepCopyInterface interface {
DeepCopy() interface{}
}
// Copy deepcopies from v.
func Copy(src interface{}) interface{} {
if src == nil {
return nil
}
if fromSyncMap, ok := src.(*sync.Map); ok {
to := copySyncMap(fromSyncMap)
return to
}
return copyNormal(src)
}
// copySyncMap copies with sync.Map but not nested
// Targets are vmssVMCache, vmssFlexVMCache, etc.
func copySyncMap(from *sync.Map) *sync.Map {
to := &sync.Map{}
from.Range(func(k, v interface{}) bool {
vm, ok := v.(*sync.Map)
if ok {
to.Store(k, copySyncMap(vm))
} else {
to.Store(k, copyNormal(v))
}
return true
})
return to
}
func copyNormal(src interface{}) interface{} {
if src == nil {
return nil
}
from := reflect.ValueOf(src)
to := reflect.New(from.Type()).Elem()
copyCustomimpl(from, to)
return to.Interface()
}
func copyCustomimpl(from, to reflect.Value) {
// Check if DeepCopy() is already implemented for the interface
if from.CanInterface() {
if deepcopy, ok := from.Interface().(deepCopyInterface); ok {
to.Set(reflect.ValueOf(deepcopy.DeepCopy()))
return
}
}
switch from.Kind() {
case reflect.Pointer:
fromValue := from.Elem()
if !fromValue.IsValid() {
return
}
to.Set(reflect.New(fromValue.Type()))
copyCustomimpl(fromValue, to.Elem())
case reflect.Interface:
if from.IsNil() {
return
}
fromValue := from.Elem()
toValue := reflect.New(fromValue.Type()).Elem()
copyCustomimpl(fromValue, toValue)
to.Set(toValue)
case reflect.Struct:
for i := 0; i < from.NumField(); i++ {
if from.Type().Field(i).PkgPath != "" {
// It is an unexported field.
continue
}
copyCustomimpl(from.Field(i), to.Field(i))
}
case reflect.Slice:
if from.IsNil() {
return
}
to.Set(reflect.MakeSlice(from.Type(), from.Len(), from.Cap()))
for i := 0; i < from.Len(); i++ {
copyCustomimpl(from.Index(i), to.Index(i))
}
case reflect.Map:
if from.IsNil() {
return
}
to.Set(reflect.MakeMap(from.Type()))
for _, key := range from.MapKeys() {
fromValue := from.MapIndex(key)
toValue := reflect.New(fromValue.Type()).Elem()
copyCustomimpl(fromValue, toValue)
copiedKey := Copy(key.Interface())
to.SetMapIndex(reflect.ValueOf(copiedKey), toValue)
}
default:
to.Set(from)
}
}