-
Notifications
You must be signed in to change notification settings - Fork 2
/
deepcopy.go
177 lines (149 loc) · 4.58 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package evendeep
import (
"reflect"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/flags"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/ref"
)
// New gets a new instance of DeepCopier (the underlying
// is *cpController) different with DefaultCopyController.
//
// Use New:
//
// src, tgt := 123, 0
// err = evendeep.New().CopyTo(src, &tgt)
//
// Use package functions (With-opts might cumulate):
//
// evendeep.Copy(src, &tgt) // or synonym: evendeep.DeepCopy(src, &tgt)
// tgt = evendeep.MakeClone(src)
//
// Use DefaultCopyController (With-opts might cumulate):
//
// evendeep.DefaultCopyController.CopyTo(src, &tgt)
//
// The most conventional way is:
//
// err := evendeep.New().CopyTo(src, &tgt)
func New(opts ...Opt) DeepCopier {
c := newDeepCopier()
for _, opt := range opts {
opt(c)
}
return c
}
// Copy is a synonym of DeepCopy.
//
// DeepCopy makes a deep clone of a source object or merges it into the target.
func Copy(fromObj, toObj interface{}, opts ...Opt) (result interface{}) { //nolint:revive
return DeepCopy(fromObj, toObj, opts...)
}
// DeepCopy makes a deep clone of a source object or merges it into the target.
func DeepCopy(fromObj, toObj interface{}, opts ...Opt) (result interface{}) { //nolint:revive
if fromObj == nil {
return toObj
}
defer DefaultCopyController.SaveFlagsAndRestore()() //nolint:revive
if err := DefaultCopyController.CopyTo(fromObj, toObj, opts...); err == nil {
result = toObj
}
return
}
// MakeClone makes a deep clone of a source object.
func MakeClone(fromObj interface{}) (result interface{}) { //nolint:revive
if fromObj == nil {
return nil
}
var (
from = reflect.ValueOf(fromObj)
fromTyp = from.Type()
findTyp = ref.Rdecodetypesimple(fromTyp)
toPtr = reflect.New(findTyp)
toPtrObj = toPtr.Interface()
)
dbglog.Log("toPtrObj: %v", toPtrObj)
defer defaultCloneController.SaveFlagsAndRestore()() //nolint:revive
if err := defaultCloneController.CopyTo(fromObj, toPtrObj); err == nil {
result = toPtr.Elem().Interface()
}
return
}
// Cloneable interface represents a cloneable object that supports Clone() method.
//
// The native Clone algorithm of a Cloneable object can be adapted into DeepCopier.
type Cloneable interface {
// Clone return a pointer to copy of source object.
// But you can return the copy itself with your will.
Clone() interface{} //nolint:revive
}
// DeepCopyable interface represents a cloneable object that supports DeepCopy() method.
//
// The native DeepCopy algorithm of a DeepCopyable object can be adapted into DeepCopier.
type DeepCopyable interface {
DeepCopy() interface{} //nolint:revive
}
// DeepCopier interface.
type DeepCopier interface {
// CopyTo function.
CopyTo(fromObj, toObj interface{}, opts ...Opt) (err error) //nolint:revive
}
//nolint:gochecknoglobals //i know that
var (
// DefaultCopyController provides standard deepcopy feature.
// copy and merge slice or map to an existed target.
DefaultCopyController *cpController // by newDeepCopier()
// defaultCloneController provides standard clone feature.
// simply clone itself to a new fresh object to make a deep clone object.
defaultCloneController *cpController // by newCloner()
)
// NewFlatDeepCopier gets a new instance of DeepCopier (the underlying
// is *cpController) like NewDeepCopier but no merge strategies
// (SliceMerge and MapMerge).
func NewFlatDeepCopier(opts ...Opt) DeepCopier {
// lazyInitRoutines()
c := newCopier()
c.flags = flags.New()
for _, opt := range opts {
opt(c)
}
return c
}
func newDeepCopier() *cpController {
// lazyInitRoutines()
return &cpController{
valueConverters: defaultValueConverters(),
valueCopiers: defaultValueCopiers(),
copyUnexportedFields: true,
copyFunctionResultToTarget: true,
passSourceAsFunctionInArgs: true,
autoExpandStruct: true,
autoNewStruct: true,
flags: flags.New(cms.SliceMerge, cms.MapMerge),
rethrow: true,
makeNewClone: false,
}
}
func newCopier() *cpController {
return &cpController{
valueConverters: defaultValueConverters(),
valueCopiers: defaultValueCopiers(),
copyFunctionResultToTarget: true,
passSourceAsFunctionInArgs: true,
rethrow: true,
makeNewClone: false,
}
}
func newCloner() *cpController {
return &cpController{
valueConverters: defaultValueConverters(),
valueCopiers: defaultValueCopiers(),
copyFunctionResultToTarget: true,
passSourceAsFunctionInArgs: true,
autoExpandStruct: true,
autoNewStruct: true,
flags: flags.New(cms.Default),
rethrow: true,
makeNewClone: true,
}
}