forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldmask.go
61 lines (58 loc) · 1.62 KB
/
fieldmask.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
package gorm
import (
"errors"
"fmt"
"reflect"
"strings"
fieldmask "google.golang.org/genproto/protobuf/field_mask"
)
// MergeWithMask will take the fields of `source` that are included as
// paths in `mask` and write them to the corresponding fields of `dest`
func MergeWithMask(source, dest interface{}, mask *fieldmask.FieldMask) error {
if mask == nil || len(mask.Paths) == 0 {
return nil
}
if source == nil {
return errors.New("Source object is nil")
}
if dest == nil {
return errors.New("Destination object is nil")
}
if reflect.TypeOf(source) != reflect.TypeOf(dest) {
return errors.New("Types of source and destination objects do not match")
}
pathsloop:
for _, fullpath := range mask.GetPaths() {
subpaths := strings.Split(fullpath, ".")
srcVal := reflect.ValueOf(source).Elem()
dstVal := reflect.ValueOf(dest).Elem()
for _, path := range subpaths {
for dstVal.Kind() == reflect.Ptr {
if dstVal.IsNil() {
dstVal.Set(reflect.New(dstVal.Type().Elem()))
}
dstVal = dstVal.Elem()
srcVal = srcVal.Elem()
}
// For safety, skip paths that will cause a panic to call FieldByName on
if dstVal.Kind() != reflect.Struct {
continue pathsloop
}
srcVal = srcVal.FieldByName(path)
dstVal = dstVal.FieldByName(path)
if !srcVal.IsValid() || !dstVal.IsValid() {
return fmt.Errorf("Field path %q doesn't exist in type %s",
fullpath, reflect.TypeOf(source))
}
}
for dstVal.Kind() == reflect.Ptr {
if dstVal.IsNil() {
dstVal.Set(reflect.New(dstVal.Type().Elem()))
}
dstVal = dstVal.Elem()
srcVal = srcVal.Elem()
}
dstVal.Set(srcVal)
}
return nil
}