forked from ulule/deepcopier
/
deepcopier.go
352 lines (312 loc) · 8.61 KB
/
deepcopier.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package deepcopier
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/lib/pq"
"github.com/oleiade/reflections"
)
const (
// TagName is struct field tag name.
TagName = "deepcopier"
// FieldOptionName is the from field option name for struct tag.
FieldOptionName = "field"
// ContextOptionName is the context option name for struct tag.
ContextOptionName = "context"
// SkipOptionName is the skip option name for struct tag.
SkipOptionName = "skip"
)
// DeepCopier deep copies a struct to/from a struct.
type DeepCopier struct {
Source interface{}
Destination interface{}
Tagged interface{}
Context map[string]interface{}
Reversed bool
}
// FieldOptions contains options passed to SetField method.
type FieldOptions struct {
SourceField string
DestinationField string
WithContext bool
Skip bool
}
// Copy sets the source.
func Copy(source interface{}) *DeepCopier {
return &DeepCopier{
Source: source,
Reversed: false,
}
}
// To sets the given tagged struct as destination struct.
// Source -> Destination
func (dc *DeepCopier) To(tagged interface{}) error {
dc.Destination = tagged
dc.Tagged = tagged
return dc.ProcessCopy()
}
// From sets the given tagged struct as source and the current source as destination.
// Source <- Destination
func (dc *DeepCopier) From(tagged interface{}) error {
dc.Destination = dc.Source
dc.Source = tagged
dc.Tagged = tagged
dc.Reversed = true
return dc.ProcessCopy()
}
// ProcessCopy processes copy.
func (dc *DeepCopier) ProcessCopy() error {
fields, _ := reflections.Fields(dc.Tagged)
for _, field := range fields {
fieldOptions := &FieldOptions{
SourceField: field,
DestinationField: field,
WithContext: false,
Skip: false,
}
tagOptions, _ := reflections.GetFieldTag(dc.Tagged, field, TagName)
if tagOptions != "" {
opts := dc.GetTagOptions(tagOptions)
if _, ok := opts[FieldOptionName]; ok {
fieldName := opts[FieldOptionName]
if !dc.Reversed {
fieldOptions.SourceField = fieldName
} else {
fieldOptions.DestinationField = fieldName
}
}
if _, ok := opts[ContextOptionName]; ok {
fieldOptions.WithContext = true
}
if _, ok := opts[SkipOptionName]; ok {
fieldOptions.Skip = true
}
}
if err := dc.SetField(fieldOptions); err != nil {
return err
}
}
return nil
}
// -----------------------------------------------------------------------------
// Options
// -----------------------------------------------------------------------------
// WithContext injects the given context into the builder instance.
func (dc *DeepCopier) WithContext(context map[string]interface{}) *DeepCopier {
dc.Context = context
return dc
}
// -----------------------------------------------------------------------------
// Struct tags
// -----------------------------------------------------------------------------
// GetTagOptions parses deepcopier tag field and returns options.
func (dc *DeepCopier) GetTagOptions(value string) map[string]string {
options := map[string]string{}
for _, opt := range strings.Split(value, ";") {
o := strings.Split(opt, ":")
// deepcopier:"keyword; without; value;"
if len(o) == 1 {
k := o[0]
options[k] = ""
}
// deepcopier:"key:value; anotherkey:anothervalue"
if len(o) == 2 {
k, v := o[0], o[1]
k = strings.TrimSpace(k)
v = strings.TrimSpace(v)
options[k] = v
}
}
return options
}
// -----------------------------------------------------------------------------
// Field Setters
// -----------------------------------------------------------------------------
// SetField sets the value of the given field.
func (dc *DeepCopier) SetField(options *FieldOptions) error {
if options.Skip {
return nil
}
if dc.Reversed {
has, _ := reflections.HasField(dc.Destination, options.SourceField)
if !has {
return nil
}
}
has, _ := reflections.HasField(dc.Source, options.SourceField)
if !has {
err := dc.HandleMethod(options)
if err != nil {
has, _ = reflections.HasField(dc.Destination, options.DestinationField)
if has {
return nil
}
}
return nil
}
kind, _ := reflections.GetFieldKind(dc.Source, options.SourceField)
if kind == reflect.Struct {
if err := dc.HandleStructField(options); err != nil {
return err
}
return nil
}
if err := dc.HandleField(options); err != nil {
return err
}
return nil
}
// SetFieldValue Sets the given value to the given field.
func (dc *DeepCopier) SetFieldValue(entity interface{}, name string, value reflect.Value) error {
kind := value.Kind()
// Maps
if kind == reflect.Map {
switch v := value.Interface().(type) {
case map[string]interface{}, map[string]string, map[string]map[string]string, map[string]map[string]map[string]string:
if err := reflections.SetField(entity, name, v); err != nil {
return err
}
return nil
}
}
// Structs
if kind == reflect.Struct {
switch v := value.Interface().(type) {
case time.Time, pq.NullTime:
if err := reflections.SetField(entity, name, v); err != nil {
return err
}
return nil
}
}
// Reflect
switch kind {
case reflect.Int8:
if err := reflections.SetField(entity, name, int8(value.Int())); err != nil {
return err
}
return nil
case reflect.Int16:
if err := reflections.SetField(entity, name, int16(value.Int())); err != nil {
return err
}
return nil
case reflect.Int32:
if err := reflections.SetField(entity, name, int32(value.Int())); err != nil {
return err
}
return nil
case reflect.Int64:
if err := reflections.SetField(entity, name, value.Int()); err != nil {
return err
}
return nil
case reflect.Int:
if err := reflections.SetField(entity, name, int(value.Int())); err != nil {
return err
}
return nil
case reflect.Uint8:
if err := reflections.SetField(entity, name, uint8(value.Uint())); err != nil {
return err
}
return nil
case reflect.Uint16:
if err := reflections.SetField(entity, name, uint16(value.Uint())); err != nil {
return err
}
return nil
case reflect.Uint32:
if err := reflections.SetField(entity, name, uint32(value.Uint())); err != nil {
return err
}
return nil
case reflect.Uint64:
if err := reflections.SetField(entity, name, value.Uint()); err != nil {
return err
}
return nil
case reflect.Uint:
if err := reflections.SetField(entity, name, uint(value.Uint())); err != nil {
return err
}
return nil
case reflect.Float32:
if err := reflections.SetField(entity, name, float32(value.Float())); err != nil {
return err
}
return nil
case reflect.Float64:
if err := reflections.SetField(entity, name, value.Float()); err != nil {
return err
}
return nil
case reflect.String:
if err := reflections.SetField(entity, name, value.String()); err != nil {
return err
}
return nil
case reflect.Bool:
if err := reflections.SetField(entity, name, value.Bool()); err != nil {
return err
}
return nil
}
return nil
}
// -----------------------------------------------------------------------------
// Field Type Handlers
// -----------------------------------------------------------------------------
// HandleStructField sets the value for the given supported struct field.
func (dc *DeepCopier) HandleStructField(options *FieldOptions) error {
f, err := reflections.GetField(dc.Source, options.SourceField)
if err != nil {
return err
}
switch v := f.(type) {
case pq.NullTime:
if v.Valid {
if err := reflections.SetField(dc.Destination, options.DestinationField, &v.Time); err != nil {
return err
}
}
case time.Time:
if err := reflections.SetField(dc.Destination, options.DestinationField, v); err != nil {
return err
}
}
return nil
}
// HandleField sets value for the given field.
func (dc *DeepCopier) HandleField(options *FieldOptions) error {
v, err := reflections.GetField(dc.Source, options.SourceField)
if err != nil {
return err
}
value := reflect.ValueOf(v)
if err := dc.SetFieldValue(dc.Destination, options.DestinationField, value); err != nil {
return err
}
return nil
}
// HandleMethod tries to call method on model and sets result in resource field.
func (dc *DeepCopier) HandleMethod(options *FieldOptions) error {
if dc.Reversed {
return nil
}
method := reflect.ValueOf(dc.Source).MethodByName(options.SourceField)
if !method.IsValid() {
return fmt.Errorf("Method %s does not exist", options.SourceField)
}
var results []reflect.Value
if options.WithContext {
results = method.Call([]reflect.Value{reflect.ValueOf(dc.Context)})
} else {
results = method.Call([]reflect.Value{})
}
if err := dc.SetFieldValue(dc.Destination, options.DestinationField, results[0]); err != nil {
return err
}
return nil
}