-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.go
64 lines (53 loc) · 1.62 KB
/
merge.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 structs
import (
"reflect"
"time"
"github.com/ikeikeikeike/gocore/util/logger"
"github.com/imdario/mergo"
"gopkg.in/oleiade/reflections.v1"
)
type (
timeTransfomer struct{}
)
// Transformer merges time except src's ZeroTime: https://github.com/imdario/mergo#transformers
//
// If you mind even a bit, you would be better to use
// https://github.com/jinzhu/copier which will be overwritten everything.
//
func (t timeTransfomer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ == reflect.TypeOf(time.Time{}) {
return func(dst, src reflect.Value) error {
if dst.CanSet() {
isZero := dst.MethodByName("IsZero")
result := isZero.Call([]reflect.Value{})
if result[0].Bool() {
dst.Set(src)
}
}
return nil
}
}
return nil
}
// Merge merges dest values
func Merge(dest interface{}, values ...interface{}) error {
data := make(map[string]interface{})
for _, value := range values {
v, _ := reflections.Items(value)
if err := mergo.Map(&data, v, mergo.WithTransformers(timeTransfomer{})); err != nil {
logger.E("merge.go Merge: %s", err)
}
}
return mergo.Map(dest, data, mergo.WithTransformers(timeTransfomer{}))
}
// OverwriteMerge merges dest values
func OverwriteMerge(dest interface{}, values ...interface{}) error {
data := make(map[string]interface{})
for _, value := range values {
v, _ := reflections.Items(value)
if err := mergo.MapWithOverwrite(&data, v, mergo.WithTransformers(timeTransfomer{})); err != nil {
logger.E("merge.go OverwriteMerge: %s", err)
}
}
return mergo.MapWithOverwrite(dest, data, mergo.WithTransformers(timeTransfomer{}))
}