Browse code

Upgrade imdario/mergo to v0.3.5

Mainly to get inline with `docker/cli` version of that dependency

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2018/06/11 22:30:46
Showing 6 changed files
... ...
@@ -436,7 +436,7 @@ func (s *DockerDaemonSuite) TestDaemonEvents(c *check.C) {
436 436
 		"(allow-nondistributable-artifacts=[",
437 437
 		" cluster-advertise=, ",
438 438
 		" cluster-store=, ",
439
-		" cluster-store-opts={",
439
+		" cluster-store-opts=",
440 440
 		" debug=true, ",
441 441
 		" default-ipc-mode=",
442 442
 		" default-runtime=",
... ...
@@ -23,7 +23,7 @@ github.com/gotestyourself/gotestyourself cf3a5ab914a2efa8bc838d09f5918c1d44d029
23 23
 github.com/google/go-cmp v0.2.0
24 24
 
25 25
 github.com/RackSec/srslog 456df3a81436d29ba874f3590eeeee25d666f8a5
26
-github.com/imdario/mergo 0.2.1
26
+github.com/imdario/mergo v0.3.5
27 27
 golang.org/x/sync fd80eb99c8f653c847d294a001bdf2a3a6f768f5
28 28
 
29 29
 # buildkit
... ...
@@ -2,31 +2,72 @@
2 2
 
3 3
 A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
4 4
 
5
-Also a lovely [comune](http://en.wikipedia.org/wiki/Mergo) (municipality) in the Province of Ancona in the Italian region Marche.
6
-
7
-![Mergo dall'alto](http://www.comune.mergo.an.it/Siti/Mergo/Immagini/Foto/mergo_dall_alto.jpg)
5
+Also a lovely [comune](http://en.wikipedia.org/wiki/Mergo) (municipality) in the Province of Ancona in the Italian region of Marche.
8 6
 
9 7
 ## Status
10 8
 
11
-It is ready for production use. It works fine after extensive use in the wild.
9
+It is ready for production use. [It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, etc](https://github.com/imdario/mergo#mergo-in-the-wild).
12 10
 
11
+[![GoDoc][3]][4]
12
+[![GoCard][5]][6]
13 13
 [![Build Status][1]][2]
14
-[![GoDoc](https://godoc.org/github.com/imdario/mergo?status.svg)](https://godoc.org/github.com/imdario/mergo)
14
+[![Coverage Status][7]][8]
15
+[![Sourcegraph][9]][10]
15 16
 
16 17
 [1]: https://travis-ci.org/imdario/mergo.png
17 18
 [2]: https://travis-ci.org/imdario/mergo
19
+[3]: https://godoc.org/github.com/imdario/mergo?status.svg
20
+[4]: https://godoc.org/github.com/imdario/mergo
21
+[5]: https://goreportcard.com/badge/imdario/mergo
22
+[6]: https://goreportcard.com/report/github.com/imdario/mergo
23
+[7]: https://coveralls.io/repos/github/imdario/mergo/badge.svg?branch=master
24
+[8]: https://coveralls.io/github/imdario/mergo?branch=master
25
+[9]: https://sourcegraph.com/github.com/imdario/mergo/-/badge.svg
26
+[10]: https://sourcegraph.com/github.com/imdario/mergo?badge
27
+
28
+### Latest release
29
+
30
+[Release v0.3.4](https://github.com/imdario/mergo/releases/tag/v0.3.4).
18 31
 
19 32
 ### Important note
20 33
 
21
-Mergo is intended to assign **only** zero value fields on destination with source value. Since April 6th it works like this. Before it didn't work properly, causing some random overwrites. After some issues and PRs I found it didn't merge as I designed it. Thanks to [imdario/mergo#8](https://github.com/imdario/mergo/pull/8) overwriting functions were added and the wrong behavior was clearly detected.
34
+Please keep in mind that in [0.3.2](//github.com/imdario/mergo/releases/tag/0.3.2) Mergo changed `Merge()`and `Map()` signatures to support [transformers](#transformers). An optional/variadic argument has been added, so it won't break existing code.
22 35
 
23 36
 If you were using Mergo **before** April 6th 2015, please check your project works as intended after updating your local copy with ```go get -u github.com/imdario/mergo```. I apologize for any issue caused by its previous behavior and any future bug that Mergo could cause (I hope it won't!) in existing projects after the change (release 0.2.0).
24 37
 
38
+### Donations
39
+
40
+If Mergo is useful to you, consider buying me a coffee, a beer or making a monthly donation so I can keep building great free software. :heart_eyes:
41
+
42
+<a href='https://ko-fi.com/B0B58839' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
43
+[![Beerpay](https://beerpay.io/imdario/mergo/badge.svg)](https://beerpay.io/imdario/mergo)
44
+[![Beerpay](https://beerpay.io/imdario/mergo/make-wish.svg)](https://beerpay.io/imdario/mergo)
45
+<a href="https://liberapay.com/dario/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a>
46
+
25 47
 ### Mergo in the wild
26 48
 
49
+- [moby/moby](https://github.com/moby/moby)
50
+- [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes)
51
+- [vmware/dispatch](https://github.com/vmware/dispatch)
52
+- [Shopify/themekit](https://github.com/Shopify/themekit)
27 53
 - [imdario/zas](https://github.com/imdario/zas)
28
-- [GoogleCloudPlatform/kubernetes](https://github.com/GoogleCloudPlatform/kubernetes)
54
+- [matcornic/hermes](https://github.com/matcornic/hermes)
55
+- [OpenBazaar/openbazaar-go](https://github.com/OpenBazaar/openbazaar-go)
56
+- [kataras/iris](https://github.com/kataras/iris)
57
+- [michaelsauter/crane](https://github.com/michaelsauter/crane)
58
+- [go-task/task](https://github.com/go-task/task)
59
+- [sensu/uchiwa](https://github.com/sensu/uchiwa)
60
+- [ory/hydra](https://github.com/ory/hydra)
61
+- [sisatech/vcli](https://github.com/sisatech/vcli)
62
+- [dairycart/dairycart](https://github.com/dairycart/dairycart)
63
+- [projectcalico/felix](https://github.com/projectcalico/felix)
64
+- [resin-os/balena](https://github.com/resin-os/balena)
65
+- [go-kivik/kivik](https://github.com/go-kivik/kivik)
66
+- [Telefonica/govice](https://github.com/Telefonica/govice)
67
+- [supergiant/supergiant](supergiant/supergiant)
68
+- [SergeyTsalkov/brooce](https://github.com/SergeyTsalkov/brooce)
29 69
 - [soniah/dnsmadeeasy](https://github.com/soniah/dnsmadeeasy)
70
+- [ohsu-comp-bio/funnel](https://github.com/ohsu-comp-bio/funnel)
30 71
 - [EagerIO/Stout](https://github.com/EagerIO/Stout)
31 72
 - [lynndylanhurley/defsynth-api](https://github.com/lynndylanhurley/defsynth-api)
32 73
 - [russross/canvasassignments](https://github.com/russross/canvasassignments)
... ...
@@ -44,6 +85,7 @@ If you were using Mergo **before** April 6th 2015, please check your project wor
44 44
 - [thoas/picfit](https://github.com/thoas/picfit)
45 45
 - [mantasmatelis/whooplist-server](https://github.com/mantasmatelis/whooplist-server)
46 46
 - [jnuthong/item_search](https://github.com/jnuthong/item_search)
47
+- [bukalapak/snowboard](https://github.com/bukalapak/snowboard)
47 48
 
48 49
 ## Installation
49 50
 
... ...
@@ -56,19 +98,31 @@ If you were using Mergo **before** April 6th 2015, please check your project wor
56 56
 
57 57
 ## Usage
58 58
 
59
-You can only merge same-type structs with exported fields initialized as zero value of their type and same-types maps. Mergo won't merge unexported (private) fields but will do recursively any exported one. Also maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).
59
+You can only merge same-type structs with exported fields initialized as zero value of their type and same-types maps. Mergo won't merge unexported (private) fields but will do recursively any exported one. It won't merge empty structs value as [they are not considered zero values](https://golang.org/ref/spec#The_zero_value) either. Also maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).
60 60
 
61
-    if err := mergo.Merge(&dst, src); err != nil {
62
-        // ...
63
-    }
61
+```go
62
+if err := mergo.Merge(&dst, src); err != nil {
63
+    // ...
64
+}
65
+```
64 66
 
65
-Additionally, you can map a map[string]interface{} to a struct (and otherwise, from struct to map), following the same restrictions as in Merge(). Keys are capitalized to find each corresponding exported field.
67
+Also, you can merge overwriting values using the transformer `WithOverride`.
66 68
 
67
-    if err := mergo.Map(&dst, srcMap); err != nil {
68
-        // ...
69
-    }
69
+```go
70
+if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
71
+    // ...
72
+}
73
+```
70 74
 
71
-Warning: if you map a struct to map, it won't do it recursively. Don't expect Mergo to map struct members of your struct as map[string]interface{}. They will be just assigned as values.
75
+Additionally, you can map a `map[string]interface{}` to a struct (and otherwise, from struct to map), following the same restrictions as in `Merge()`. Keys are capitalized to find each corresponding exported field.
76
+
77
+```go
78
+if err := mergo.Map(&dst, srcMap); err != nil {
79
+    // ...
80
+}
81
+```
82
+
83
+Warning: if you map a struct to map, it won't do it recursively. Don't expect Mergo to map struct members of your struct as `map[string]interface{}`. They will be just assigned as values.
72 84
 
73 85
 More information and examples in [godoc documentation](http://godoc.org/github.com/imdario/mergo).
74 86
 
... ...
@@ -90,15 +144,12 @@ type Foo struct {
90 90
 func main() {
91 91
 	src := Foo{
92 92
 		A: "one",
93
+		B: 2,
93 94
 	}
94
-
95 95
 	dest := Foo{
96 96
 		A: "two",
97
-		B: 2,
98 97
 	}
99
-
100 98
 	mergo.Merge(&dest, src)
101
-
102 99
 	fmt.Println(dest)
103 100
 	// Will print
104 101
 	// {two 2}
... ...
@@ -107,7 +158,56 @@ func main() {
107 107
 
108 108
 Note: if test are failing due missing package, please execute:
109 109
 
110
-    go get gopkg.in/yaml.v1
110
+    go get gopkg.in/yaml.v2
111
+
112
+### Transformers
113
+
114
+Transformers allow to merge specific types differently than in the default behavior. In other words, now you can customize how some types are merged. For example, `time.Time` is a struct; it doesn't have zero value but IsZero can return true because it has fields with zero value. How can we merge a non-zero `time.Time`?
115
+
116
+```go
117
+package main
118
+
119
+import (
120
+	"fmt"
121
+	"github.com/imdario/mergo"
122
+        "reflect"
123
+        "time"
124
+)
125
+
126
+type timeTransfomer struct {
127
+}
128
+
129
+func (t timeTransfomer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
130
+	if typ == reflect.TypeOf(time.Time{}) {
131
+		return func(dst, src reflect.Value) error {
132
+			if dst.CanSet() {
133
+				isZero := dst.MethodByName("IsZero")
134
+				result := isZero.Call([]reflect.Value{})
135
+				if result[0].Bool() {
136
+					dst.Set(src)
137
+				}
138
+			}
139
+			return nil
140
+		}
141
+	}
142
+	return nil
143
+}
144
+
145
+type Snapshot struct {
146
+	Time time.Time
147
+	// ...
148
+}
149
+
150
+func main() {
151
+	src := Snapshot{time.Now()}
152
+	dest := Snapshot{}
153
+	mergo.Merge(&dest, src, mergo.WithTransformers(timeTransfomer{}))
154
+	fmt.Println(dest)
155
+	// Will print
156
+	// { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
157
+}
158
+```
159
+
111 160
 
112 161
 ## Contact me
113 162
 
... ...
@@ -31,7 +31,8 @@ func isExported(field reflect.StructField) bool {
31 31
 // Traverses recursively both values, assigning src's fields values to dst.
32 32
 // The map argument tracks comparisons that have already been seen, which allows
33 33
 // short circuiting on recursive types.
34
-func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
34
+func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
35
+	overwrite := config.Overwrite
35 36
 	if dst.CanAddr() {
36 37
 		addr := dst.UnsafeAddr()
37 38
 		h := 17 * addr
... ...
@@ -61,6 +62,13 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, over
61 61
 				dstMap[fieldName] = src.Field(i).Interface()
62 62
 			}
63 63
 		}
64
+	case reflect.Ptr:
65
+		if dst.IsNil() {
66
+			v := reflect.New(dst.Type().Elem())
67
+			dst.Set(v)
68
+		}
69
+		dst = dst.Elem()
70
+		fallthrough
64 71
 	case reflect.Struct:
65 72
 		srcMap := src.Interface().(map[string]interface{})
66 73
 		for key := range srcMap {
... ...
@@ -85,21 +93,24 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, over
85 85
 					srcKind = reflect.Ptr
86 86
 				}
87 87
 			}
88
+
88 89
 			if !srcElement.IsValid() {
89 90
 				continue
90 91
 			}
91 92
 			if srcKind == dstKind {
92
-				if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
93
+				if err = deepMerge(dstElement, srcElement, visited, depth+1, config); err != nil {
93 94
 					return
94 95
 				}
95
-			} else {
96
-				if srcKind == reflect.Map {
97
-					if err = deepMap(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
98
-						return
99
-					}
100
-				} else {
101
-					return fmt.Errorf("type mismatch on %s field: found %v, expected %v", fieldName, srcKind, dstKind)
96
+			} else if dstKind == reflect.Interface && dstElement.Kind() == reflect.Interface {
97
+				if err = deepMerge(dstElement, srcElement, visited, depth+1, config); err != nil {
98
+					return
99
+				}
100
+			} else if srcKind == reflect.Map {
101
+				if err = deepMap(dstElement, srcElement, visited, depth+1, config); err != nil {
102
+					return
102 103
 				}
104
+			} else {
105
+				return fmt.Errorf("type mismatch on %s field: found %v, expected %v", fieldName, srcKind, dstKind)
103 106
 			}
104 107
 		}
105 108
 	}
... ...
@@ -117,26 +128,35 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, over
117 117
 // doesn't apply if dst is a map.
118 118
 // This is separated method from Merge because it is cleaner and it keeps sane
119 119
 // semantics: merging equal types, mapping different (restricted) types.
120
-func Map(dst, src interface{}) error {
121
-	return _map(dst, src, false)
120
+func Map(dst, src interface{}, opts ...func(*Config)) error {
121
+	return _map(dst, src, opts...)
122 122
 }
123 123
 
124
-func MapWithOverwrite(dst, src interface{}) error {
125
-	return _map(dst, src, true)
124
+// MapWithOverwrite will do the same as Map except that non-empty dst attributes will be overridden by
125
+// non-empty src attribute values.
126
+// Deprecated: Use Map(…) with WithOverride
127
+func MapWithOverwrite(dst, src interface{}, opts ...func(*Config)) error {
128
+	return _map(dst, src, append(opts, WithOverride)...)
126 129
 }
127 130
 
128
-func _map(dst, src interface{}, overwrite bool) error {
131
+func _map(dst, src interface{}, opts ...func(*Config)) error {
129 132
 	var (
130 133
 		vDst, vSrc reflect.Value
131 134
 		err        error
132 135
 	)
136
+	config := &Config{}
137
+
138
+	for _, opt := range opts {
139
+		opt(config)
140
+	}
141
+
133 142
 	if vDst, vSrc, err = resolveValues(dst, src); err != nil {
134 143
 		return err
135 144
 	}
136 145
 	// To be friction-less, we redirect equal-type arguments
137 146
 	// to deepMerge. Only because arguments can be anything.
138 147
 	if vSrc.Kind() == vDst.Kind() {
139
-		return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
148
+		return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, config)
140 149
 	}
141 150
 	switch vSrc.Kind() {
142 151
 	case reflect.Struct:
... ...
@@ -150,5 +170,5 @@ func _map(dst, src interface{}, overwrite bool) error {
150 150
 	default:
151 151
 		return ErrNotSupported
152 152
 	}
153
-	return deepMap(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
153
+	return deepMap(vDst, vSrc, make(map[uintptr]*visit), 0, config)
154 154
 }
... ...
@@ -12,10 +12,34 @@ import (
12 12
 	"reflect"
13 13
 )
14 14
 
15
+func hasExportedField(dst reflect.Value) (exported bool) {
16
+	for i, n := 0, dst.NumField(); i < n; i++ {
17
+		field := dst.Type().Field(i)
18
+		if field.Anonymous && dst.Field(i).Kind() == reflect.Struct {
19
+			exported = exported || hasExportedField(dst.Field(i))
20
+		} else {
21
+			exported = exported || len(field.PkgPath) == 0
22
+		}
23
+	}
24
+	return
25
+}
26
+
27
+type Config struct {
28
+	Overwrite    bool
29
+	AppendSlice  bool
30
+	Transformers Transformers
31
+}
32
+
33
+type Transformers interface {
34
+	Transformer(reflect.Type) func(dst, src reflect.Value) error
35
+}
36
+
15 37
 // Traverses recursively both values, assigning src's fields values to dst.
16 38
 // The map argument tracks comparisons that have already been seen, which allows
17 39
 // short circuiting on recursive types.
18
-func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
40
+func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error) {
41
+	overwrite := config.Overwrite
42
+
19 43
 	if !src.IsValid() {
20 44
 		return
21 45
 	}
... ...
@@ -32,14 +56,31 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, ov
32 32
 		// Remember, remember...
33 33
 		visited[h] = &visit{addr, typ, seen}
34 34
 	}
35
+
36
+	if config.Transformers != nil && !isEmptyValue(dst) {
37
+		if fn := config.Transformers.Transformer(dst.Type()); fn != nil {
38
+			err = fn(dst, src)
39
+			return
40
+		}
41
+	}
42
+
35 43
 	switch dst.Kind() {
36 44
 	case reflect.Struct:
37
-		for i, n := 0, dst.NumField(); i < n; i++ {
38
-			if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, overwrite); err != nil {
39
-				return
45
+		if hasExportedField(dst) {
46
+			for i, n := 0, dst.NumField(); i < n; i++ {
47
+				if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, config); err != nil {
48
+					return
49
+				}
50
+			}
51
+		} else {
52
+			if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
53
+				dst.Set(src)
40 54
 			}
41 55
 		}
42 56
 	case reflect.Map:
57
+		if dst.IsNil() && !src.IsNil() {
58
+			dst.Set(reflect.MakeMap(dst.Type()))
59
+		}
43 60
 		for _, key := range src.MapKeys() {
44 61
 			srcElement := src.MapIndex(key)
45 62
 			if !srcElement.IsValid() {
... ...
@@ -47,40 +88,99 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, ov
47 47
 			}
48 48
 			dstElement := dst.MapIndex(key)
49 49
 			switch srcElement.Kind() {
50
-			case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
50
+			case reflect.Chan, reflect.Func, reflect.Map, reflect.Interface, reflect.Slice:
51 51
 				if srcElement.IsNil() {
52 52
 					continue
53 53
 				}
54 54
 				fallthrough
55 55
 			default:
56
+				if !srcElement.CanInterface() {
57
+					continue
58
+				}
56 59
 				switch reflect.TypeOf(srcElement.Interface()).Kind() {
57 60
 				case reflect.Struct:
58 61
 					fallthrough
59 62
 				case reflect.Ptr:
60 63
 					fallthrough
61 64
 				case reflect.Map:
62
-					if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
65
+					srcMapElm := srcElement
66
+					dstMapElm := dstElement
67
+					if srcMapElm.CanInterface() {
68
+						srcMapElm = reflect.ValueOf(srcMapElm.Interface())
69
+						if dstMapElm.IsValid() {
70
+							dstMapElm = reflect.ValueOf(dstMapElm.Interface())
71
+						}
72
+					}
73
+					if err = deepMerge(dstMapElm, srcMapElm, visited, depth+1, config); err != nil {
63 74
 						return
64 75
 					}
76
+				case reflect.Slice:
77
+					srcSlice := reflect.ValueOf(srcElement.Interface())
78
+
79
+					var dstSlice reflect.Value
80
+					if !dstElement.IsValid() || dstElement.IsNil() {
81
+						dstSlice = reflect.MakeSlice(srcSlice.Type(), 0, srcSlice.Len())
82
+					} else {
83
+						dstSlice = reflect.ValueOf(dstElement.Interface())
84
+					}
85
+
86
+					if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
87
+						dstSlice = srcSlice
88
+					} else if config.AppendSlice {
89
+						dstSlice = reflect.AppendSlice(dstSlice, srcSlice)
90
+					}
91
+					dst.SetMapIndex(key, dstSlice)
65 92
 				}
66 93
 			}
67
-			if !isEmptyValue(srcElement) && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
94
+			if dstElement.IsValid() && reflect.TypeOf(srcElement.Interface()).Kind() == reflect.Map {
95
+				continue
96
+			}
97
+
98
+			if srcElement.IsValid() && (overwrite || (!dstElement.IsValid() || isEmptyValue(dstElement))) {
68 99
 				if dst.IsNil() {
69 100
 					dst.Set(reflect.MakeMap(dst.Type()))
70 101
 				}
71 102
 				dst.SetMapIndex(key, srcElement)
72 103
 			}
73 104
 		}
105
+	case reflect.Slice:
106
+		if !dst.CanSet() {
107
+			break
108
+		}
109
+		if !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) && !config.AppendSlice {
110
+			dst.Set(src)
111
+		} else if config.AppendSlice {
112
+			dst.Set(reflect.AppendSlice(dst, src))
113
+		}
74 114
 	case reflect.Ptr:
75 115
 		fallthrough
76 116
 	case reflect.Interface:
77 117
 		if src.IsNil() {
78 118
 			break
79
-		} else if dst.IsNil() {
119
+		}
120
+		if src.Kind() != reflect.Interface {
121
+			if dst.IsNil() || overwrite {
122
+				if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
123
+					dst.Set(src)
124
+				}
125
+			} else if src.Kind() == reflect.Ptr {
126
+				if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
127
+					return
128
+				}
129
+			} else if dst.Elem().Type() == src.Type() {
130
+				if err = deepMerge(dst.Elem(), src, visited, depth+1, config); err != nil {
131
+					return
132
+				}
133
+			} else {
134
+				return ErrDifferentArgumentsTypes
135
+			}
136
+			break
137
+		}
138
+		if dst.IsNil() || overwrite {
80 139
 			if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
81 140
 				dst.Set(src)
82 141
 			}
83
-		} else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, overwrite); err != nil {
142
+		} else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, config); err != nil {
84 143
 			return
85 144
 		}
86 145
 	default:
... ...
@@ -91,30 +191,55 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, ov
91 91
 	return
92 92
 }
93 93
 
94
-// Merge sets fields' values in dst from src if they have a zero
95
-// value of their type.
96
-// dst and src must be valid same-type structs and dst must be
97
-// a pointer to struct.
98
-// It won't merge unexported (private) fields and will do recursively
99
-// any exported field.
100
-func Merge(dst, src interface{}) error {
101
-	return merge(dst, src, false)
94
+// Merge will fill any empty for value type attributes on the dst struct using corresponding
95
+// src attributes if they themselves are not empty. dst and src must be valid same-type structs
96
+// and dst must be a pointer to struct.
97
+// It won't merge unexported (private) fields and will do recursively any exported field.
98
+func Merge(dst, src interface{}, opts ...func(*Config)) error {
99
+	return merge(dst, src, opts...)
100
+}
101
+
102
+// MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overriden by
103
+// non-empty src attribute values.
104
+// Deprecated: use Merge(…) with WithOverride
105
+func MergeWithOverwrite(dst, src interface{}, opts ...func(*Config)) error {
106
+	return merge(dst, src, append(opts, WithOverride)...)
107
+}
108
+
109
+// WithTransformers adds transformers to merge, allowing to customize the merging of some types.
110
+func WithTransformers(transformers Transformers) func(*Config) {
111
+	return func(config *Config) {
112
+		config.Transformers = transformers
113
+	}
114
+}
115
+
116
+// WithOverride will make merge override non-empty dst attributes with non-empty src attributes values.
117
+func WithOverride(config *Config) {
118
+	config.Overwrite = true
102 119
 }
103 120
 
104
-func MergeWithOverwrite(dst, src interface{}) error {
105
-	return merge(dst, src, true)
121
+// WithAppendSlice will make merge append slices instead of overwriting it
122
+func WithAppendSlice(config *Config) {
123
+	config.AppendSlice = true
106 124
 }
107 125
 
108
-func merge(dst, src interface{}, overwrite bool) error {
126
+func merge(dst, src interface{}, opts ...func(*Config)) error {
109 127
 	var (
110 128
 		vDst, vSrc reflect.Value
111 129
 		err        error
112 130
 	)
131
+
132
+	config := &Config{}
133
+
134
+	for _, opt := range opts {
135
+		opt(config)
136
+	}
137
+
113 138
 	if vDst, vSrc, err = resolveValues(dst, src); err != nil {
114 139
 		return err
115 140
 	}
116 141
 	if vDst.Type() != vSrc.Type() {
117 142
 		return ErrDifferentArgumentsTypes
118 143
 	}
119
-	return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
144
+	return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, config)
120 145
 }
... ...
@@ -32,7 +32,7 @@ type visit struct {
32 32
 	next *visit
33 33
 }
34 34
 
35
-// From src/pkg/encoding/json.
35
+// From src/pkg/encoding/json/encode.go.
36 36
 func isEmptyValue(v reflect.Value) bool {
37 37
 	switch v.Kind() {
38 38
 	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
... ...
@@ -46,7 +46,14 @@ func isEmptyValue(v reflect.Value) bool {
46 46
 	case reflect.Float32, reflect.Float64:
47 47
 		return v.Float() == 0
48 48
 	case reflect.Interface, reflect.Ptr:
49
+		if v.IsNil() {
50
+			return true
51
+		}
52
+		return isEmptyValue(v.Elem())
53
+	case reflect.Func:
49 54
 		return v.IsNil()
55
+	case reflect.Invalid:
56
+		return true
50 57
 	}
51 58
 	return false
52 59
 }