-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldmask.go
64 lines (52 loc) · 1.4 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
62
63
64
package fieldmask
import (
"errors"
"reflect"
"strings"
fieldmask "google.golang.org/genproto/protobuf/field_mask"
)
var (
ErrorNilSource = errors.New("source object is nil")
ErrorNilDest = errors.New("destination object is nil")
ErrorTypeNotMatch = errors.New("types of source and destination objects do not match")
)
// Merge will take the fields of `source` that are included as
// paths in `mask` and write them to the corresponding fields of `dest`
func Merge(source, dest interface{}, mask *fieldmask.FieldMask) error {
if source == nil {
return ErrorNilSource
}
if dest == nil {
return ErrorNilDest
}
if reflect.TypeOf(source) != reflect.TypeOf(dest) {
return ErrorTypeNotMatch
}
pathsloop:
for _, fullpath := range mask.GetPaths() {
srcVal := reflect.ValueOf(source).Elem()
dstVal := reflect.ValueOf(dest).Elem()
subpaths := strings.Split(fullpath, ".")
for _, path := range subpaths {
// return all fields when path equals to *
if path == "*" {
break
}
for dstVal.Kind() == reflect.Ptr {
if dstVal.IsNil() {
dstVal.Set(reflect.New(dstVal.Type().Elem()))
}
dstVal = dstVal.Elem()
srcVal = srcVal.Elem()
}
// avoid panic when dstVal.Kind() is not struct
if dstVal.Kind() != reflect.Struct {
continue pathsloop
}
srcVal = srcVal.FieldByName(path)
dstVal = dstVal.FieldByName(path)
}
dstVal.Set(srcVal)
}
return nil
}