/
attributecopier.go
75 lines (70 loc) · 2.17 KB
/
attributecopier.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
package apikit
import (
"reflect"
"errors"
)
// Copies attributes that should be immutable from the source RESTObject to the dest RESTObject
type ImmutableAttributeCopier interface {
RESTObject
CopyImmutableAttributesTo(dest interface{}) error
}
func CopyImmutableAttributes(source, dest interface{}) error {
if dest == nil {
return errors.New("Given a nil destination object")
}
if source == nil {
return errors.New("Given a nil source object")
}
if copier, ok := source.(ImmutableAttributeCopier); ok {
// use the custom implementation if exists
return copier.CopyImmutableAttributesTo(dest)
} else {
// copy immutable attributes based on struct tags
var vOld reflect.Value = reflect.ValueOf(source)
if vOld.Type().Kind() == reflect.Ptr {
if vOld.Elem().Type().Kind() == reflect.Struct {
vOld = vOld.Elem()
} else {
return errors.New("Source is not a pointer to a struct")
}
} else {
return errors.New("Source is not a pointer to a struct")
}
var vNew reflect.Value = reflect.ValueOf(dest)
if vNew.Type().Kind() == reflect.Ptr {
if vNew.Elem().Type().Kind() == reflect.Struct {
vNew = vNew.Elem()
} else {
return errors.New("Destination is not a pointer to a struct")
}
} else {
return errors.New("Destination is not a pointer to a struct")
}
const tagKeyName = "apikit"
const immutableKeyValue = "immutable"
for i := 0; i < vOld.NumField(); i++ {
oldFieldType := vOld.Type().Field(i)
oldFieldVal := vOld.Field(i)
fieldName := oldFieldType.Name
if oldFieldType.Tag.Get(tagKeyName) == immutableKeyValue {
// this field was marked as immutable
if newField := vNew.FieldByName(fieldName); newField.IsValid() && newField.CanSet() {
newField.Set(vOld.FieldByName(fieldName))
}
} else {
// check to see if it is a struct
if oldFieldType.Anonymous {
newFieldType := vNew.Type().Field(i)
if newFieldVal := vNew.FieldByName(fieldName); newFieldVal.IsValid() && newFieldType.Anonymous {
o := oldFieldVal.Addr().Interface()
n := newFieldVal.Addr().Interface()
if err := CopyImmutableAttributes(o, n); err != nil {
return err
}
}
}
}
}
return nil
}
}