Codebase list golang-github-imdario-mergo / 72d5195
cleanup Dmitry Smirnov 8 years ago
12 changed file(s) with 0 addition(s) and 867 deletion(s). Raw diff Collapse all Expand all
+0
-1
debian/files less more
0 golang-github-imdario-mergo-dev_0.1.2+git20150606.5.61a5285-1_all.deb devel extra
+0
-115
debian/golang-github-imdario-mergo-dev/DEBIAN/control less more
0 Package: golang-github-imdario-mergo-dev
1 Source: golang-github-imdario-mergo
2 Version: 0.1.2+git20150606.5.61a5285-1
3 Architecture: all
4 Maintainer: Debian Go Packaging Team <pkg-go-maintainers@lists.alioth.debian.org>
5 Installed-Size: 32
6 Depends: golang-go
7 Section: devel
8 Priority: extra
9 Homepage: https://github.com/imdario/mergo
10 Description: Mergo: merging Go structs and maps since 2013
11 Mergo A helper to merge structs and maps in Golang. Useful for
12 configuration default values, avoiding messy if-statements.
13 .
14 Also a lovely comune (http://en.wikipedia.org/wiki/Mergo) (municipality)
15 in the Province of Ancona in the Italian region Marche.
16 .
17 Mergo dall'alto Status It is ready for production use. It works fine
18 after extensive use in the wild.
19 .
20 Build Status (https://travis-ci.org/imdario/mergo) GoDoc
21 (https://godoc.org/github.com/imdario/mergo) Important note Mergo is
22 intended to assign only zero value fields on destination with source
23 value. Since April 6th it works like this. Before it didn't work
24 properly, causing some random overwrites. After some issues and PRs
25 I found it didn't merge as I designed it. Thanks to imdario/mergo#8
26 (https://github.com/imdario/mergo/pull/8) overwriting functions were
27 added and the wrong behavior was clearly detected.
28 .
29 If you were using Mergo before April 6th 2015, please check
30 your project works as intended after updating your local copy
31 with go get -u github.com/imdario/mergo. I apologize for any
32 issue caused by its previous behavior and any future bug that
33 Mergo could cause (I hope it won't!) in existing projects after
34 the change (release 0.2.0). Mergo in the wild• imdario/zas
35 (https://github.com/imdario/zas)• GoogleCloudPlatform/kubernetes
36 (https://github.com/GoogleCloudPlatform/kubernetes)• soniah/dnsmadeeasy
37 (https://github.com/soniah/dnsmadeeasy)• EagerIO/Stout
38 (https://github.com/EagerIO/Stout)• lynndylanhurley/defsynth-api
39 (https://github.com/lynndylanhurley/defsynth-api)•
40 russross/canvasassignments
41 (https://github.com/russross/canvasassignments)• rdegges/cryptly-api
42 (https://github.com/rdegges/cryptly-api)• casualjim/exeggutor
43 (https://github.com/casualjim/exeggutor)• divshot/gitling
44 (https://github.com/divshot/gitling)• RWJMurphy/gorl
45 (https://github.com/RWJMurphy/gorl)• andrerocker/deploy42
46 (https://github.com/andrerocker/deploy42)• elwinar/rambler
47 (https://github.com/elwinar/rambler)• tmaiaroto/gopartman
48 (https://github.com/tmaiaroto/gopartman)• jfbus/impressionist
49 (https://github.com/jfbus/impressionist)• Jmeyering/zealot
50 (https://github.com/Jmeyering/zealot)• godep-migrator/rigger-host
51 (https://github.com/godep-migrator/rigger-host)•
52 Dronevery/MultiwaySwitch-Go
53 (https://github.com/Dronevery/MultiwaySwitch-Go)• thoas/picfit
54 (https://github.com/thoas/picfit)• mantasmatelis/whooplist-server
55 (https://github.com/mantasmatelis/whooplist-server)•
56 jnuthong/item_search
57 (https://github.com/jnuthong/item_search)Installationgo get
58 github.com/imdario/mergo
59 .
60 // use in your .go code import (
61 "github.com/imdario/mergo"
62 ) Usage You can only merge same-type structs with exported fields
63 initialized as zero value of their type and same-types maps. Mergo won't
64 merge unexported (private) fields but will do recursively any exported
65 one. Also maps will be merged recursively except for structs inside
66 maps (because they are not addressable using Go reflection). if err :=
67 mergo.Merge(&dst, src); err != nil {
68 // ...
69 }
70 .
71 Additionally, you can map a map[string]interface{} to a struct (and
72 otherwise, from struct to map), following the same restrictions as in
73 Merge(). Keys are capitalized to find each corresponding exported field.
74 if err := mergo.Map(&dst, srcMap); err != nil {
75 // ...
76 }
77 .
78 Warning: if you map a struct to map, it won't do it recursively. Don't
79 expect Mergo to map struct members of your struct as
80 map[string]interface{}. They will be just assigned as values.
81 .
82 More information and examples in godoc documentation
83 (http://godoc.org/github.com/imdario/mergo). Nice example ```go
84 package main
85 .
86 import (
87 "fmt" "github.com/imdario/mergo"
88 )
89 .
90 type Foo struct {
91 A string B int64
92 }
93 .
94 func main() {
95 src := Foo{
96 A: "one",
97 }
98 dest := Foo{
99 A: "two", B: 2,
100 }
101 .
102 mergo.Merge(&dest, src)
103 .
104 fmt.Println(dest) // Will print // {two 2}
105 .
106 } ```
107 .
108 Note: if test are failing due missing package, please execute: go get
109 gopkg.in/yaml.v1 Contact me If I can help you, you have an idea or you
110 are using Mergo in your projects, don't hesitate to drop me a line
111 (or a pull request): @im_dario (https://twitter.com/im_dario) About
112 Written by Dario Castañé (http://dario.im). License BSD 3-Clause
113 (http://opensource.org/licenses/BSD-3-Clause) license, as Go language
114 (http://golang.org/LICENSE).
+0
-7
debian/golang-github-imdario-mergo-dev/DEBIAN/md5sums less more
0 cea04a95e95a86e12eeaeabffe961407 usr/share/doc/golang-github-imdario-mergo-dev/changelog.Debian.gz
1 23c76504c9f8fcdbc9cf9dace180e609 usr/share/doc/golang-github-imdario-mergo-dev/copyright
2 72d36ad766a79c6213213fe895ea689b usr/share/gocode/src/github.com/imdario/mergo/doc.go
3 d93b415363bfd121b6ae4d6017d2bd20 usr/share/gocode/src/github.com/imdario/mergo/map.go
4 cb00ac7e30385c86b544f5f61ff25305 usr/share/gocode/src/github.com/imdario/mergo/merge.go
5 53083cfcfcc7928978e6241303c1e0ce usr/share/gocode/src/github.com/imdario/mergo/mergo.go
6 db680b3570c05f2dd24fe1d035e7c9d3 usr/share/gocode/src/github.com/imdario/mergo/mergo_test.go
debian/golang-github-imdario-mergo-dev/usr/share/doc/golang-github-imdario-mergo-dev/changelog.Debian.gz less more
Binary diff not shown
+0
-15
debian/golang-github-imdario-mergo-dev/usr/share/doc/golang-github-imdario-mergo-dev/copyright less more
0 Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
1 Upstream-Name: mergo
2 Source: https://github.com/imdario/mergo
3
4 Files: *
5 Copyright: 2013 Dario Castañé
6 License: BSD-3-clause
7
8 Files: debian/*
9 Copyright: 2015 Tim Potter <tpot@hpe.com>
10 License: BSD-3-clause
11 Comment: Debian packaging is licensed under the same terms as upstream
12
13 License: BSD-3-clause
14 TODO
+0
-44
debian/golang-github-imdario-mergo-dev/usr/share/gocode/src/github.com/imdario/mergo/doc.go less more
0 // Copyright 2013 Dario Castañé. All rights reserved.
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 /*
6 Package mergo merges same-type structs and maps by setting default values in zero-value fields.
7
8 Mergo won't merge unexported (private) fields but will do recursively any exported one. It also won't merge structs inside maps (because they are not addressable using Go reflection).
9
10 Usage
11
12 From my own work-in-progress project:
13
14 type networkConfig struct {
15 Protocol string
16 Address string
17 ServerType string `json: "server_type"`
18 Port uint16
19 }
20
21 type FssnConfig struct {
22 Network networkConfig
23 }
24
25 var fssnDefault = FssnConfig {
26 networkConfig {
27 "tcp",
28 "127.0.0.1",
29 "http",
30 31560,
31 },
32 }
33
34 // Inside a function [...]
35
36 if err := mergo.Merge(&config, fssnDefault); err != nil {
37 log.Fatal(err)
38 }
39
40 // More code [...]
41
42 */
43 package mergo
+0
-154
debian/golang-github-imdario-mergo-dev/usr/share/gocode/src/github.com/imdario/mergo/map.go less more
0 // Copyright 2014 Dario Castañé. All rights reserved.
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Based on src/pkg/reflect/deepequal.go from official
6 // golang's stdlib.
7
8 package mergo
9
10 import (
11 "fmt"
12 "reflect"
13 "unicode"
14 "unicode/utf8"
15 )
16
17 func changeInitialCase(s string, mapper func(rune) rune) string {
18 if s == "" {
19 return s
20 }
21 r, n := utf8.DecodeRuneInString(s)
22 return string(mapper(r)) + s[n:]
23 }
24
25 func isExported(field reflect.StructField) bool {
26 r, _ := utf8.DecodeRuneInString(field.Name)
27 return r >= 'A' && r <= 'Z'
28 }
29
30 // Traverses recursively both values, assigning src's fields values to dst.
31 // The map argument tracks comparisons that have already been seen, which allows
32 // short circuiting on recursive types.
33 func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
34 if dst.CanAddr() {
35 addr := dst.UnsafeAddr()
36 h := 17 * addr
37 seen := visited[h]
38 typ := dst.Type()
39 for p := seen; p != nil; p = p.next {
40 if p.ptr == addr && p.typ == typ {
41 return nil
42 }
43 }
44 // Remember, remember...
45 visited[h] = &visit{addr, typ, seen}
46 }
47 zeroValue := reflect.Value{}
48 switch dst.Kind() {
49 case reflect.Map:
50 dstMap := dst.Interface().(map[string]interface{})
51 for i, n := 0, src.NumField(); i < n; i++ {
52 srcType := src.Type()
53 field := srcType.Field(i)
54 if !isExported(field) {
55 continue
56 }
57 fieldName := field.Name
58 fieldName = changeInitialCase(fieldName, unicode.ToLower)
59 if v, ok := dstMap[fieldName]; !ok || (isEmptyValue(reflect.ValueOf(v)) || overwrite) {
60 dstMap[fieldName] = src.Field(i).Interface()
61 }
62 }
63 case reflect.Struct:
64 srcMap := src.Interface().(map[string]interface{})
65 for key := range srcMap {
66 srcValue := srcMap[key]
67 fieldName := changeInitialCase(key, unicode.ToUpper)
68 dstElement := dst.FieldByName(fieldName)
69 if dstElement == zeroValue {
70 // We discard it because the field doesn't exist.
71 continue
72 }
73 srcElement := reflect.ValueOf(srcValue)
74 dstKind := dstElement.Kind()
75 srcKind := srcElement.Kind()
76 if srcKind == reflect.Ptr && dstKind != reflect.Ptr {
77 srcElement = srcElement.Elem()
78 srcKind = reflect.TypeOf(srcElement.Interface()).Kind()
79 } else if dstKind == reflect.Ptr {
80 // Can this work? I guess it can't.
81 if srcKind != reflect.Ptr && srcElement.CanAddr() {
82 srcPtr := srcElement.Addr()
83 srcElement = reflect.ValueOf(srcPtr)
84 srcKind = reflect.Ptr
85 }
86 }
87 if !srcElement.IsValid() {
88 continue
89 }
90 if srcKind == dstKind {
91 if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
92 return
93 }
94 } else {
95 if srcKind == reflect.Map {
96 if err = deepMap(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
97 return
98 }
99 } else {
100 return fmt.Errorf("type mismatch on %s field: found %v, expected %v", fieldName, srcKind, dstKind)
101 }
102 }
103 }
104 }
105 return
106 }
107
108 // Map sets fields' values in dst from src.
109 // src can be a map with string keys or a struct. dst must be the opposite:
110 // if src is a map, dst must be a valid pointer to struct. If src is a struct,
111 // dst must be map[string]interface{}.
112 // It won't merge unexported (private) fields and will do recursively
113 // any exported field.
114 // If dst is a map, keys will be src fields' names in lower camel case.
115 // Missing key in src that doesn't match a field in dst will be skipped. This
116 // doesn't apply if dst is a map.
117 // This is separated method from Merge because it is cleaner and it keeps sane
118 // semantics: merging equal types, mapping different (restricted) types.
119 func Map(dst, src interface{}) error {
120 return _map(dst, src, false)
121 }
122
123 func MapWithOverwrite(dst, src interface{}) error {
124 return _map(dst, src, true)
125 }
126
127 func _map(dst, src interface{}, overwrite bool) error {
128 var (
129 vDst, vSrc reflect.Value
130 err error
131 )
132 if vDst, vSrc, err = resolveValues(dst, src); err != nil {
133 return err
134 }
135 // To be friction-less, we redirect equal-type arguments
136 // to deepMerge. Only because arguments can be anything.
137 if vSrc.Kind() == vDst.Kind() {
138 return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
139 }
140 switch vSrc.Kind() {
141 case reflect.Struct:
142 if vDst.Kind() != reflect.Map {
143 return ErrExpectedMapAsDestination
144 }
145 case reflect.Map:
146 if vDst.Kind() != reflect.Struct {
147 return ErrExpectedStructAsDestination
148 }
149 default:
150 return ErrNotSupported
151 }
152 return deepMap(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
153 }
+0
-110
debian/golang-github-imdario-mergo-dev/usr/share/gocode/src/github.com/imdario/mergo/merge.go less more
0 // Copyright 2013 Dario Castañé. All rights reserved.
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Based on src/pkg/reflect/deepequal.go from official
6 // golang's stdlib.
7
8 package mergo
9
10 import (
11 "reflect"
12 )
13
14 // Traverses recursively both values, assigning src's fields values to dst.
15 // The map argument tracks comparisons that have already been seen, which allows
16 // short circuiting on recursive types.
17 func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, overwrite bool) (err error) {
18 if !src.IsValid() {
19 return
20 }
21 if dst.CanAddr() {
22 addr := dst.UnsafeAddr()
23 h := 17 * addr
24 seen := visited[h]
25 typ := dst.Type()
26 for p := seen; p != nil; p = p.next {
27 if p.ptr == addr && p.typ == typ {
28 return nil
29 }
30 }
31 // Remember, remember...
32 visited[h] = &visit{addr, typ, seen}
33 }
34 switch dst.Kind() {
35 case reflect.Struct:
36 for i, n := 0, dst.NumField(); i < n; i++ {
37 if err = deepMerge(dst.Field(i), src.Field(i), visited, depth+1, overwrite); err != nil {
38 return
39 }
40 }
41 case reflect.Map:
42 for _, key := range src.MapKeys() {
43 srcElement := src.MapIndex(key)
44 if !srcElement.IsValid() {
45 continue
46 }
47 dstElement := dst.MapIndex(key)
48 switch reflect.TypeOf(srcElement.Interface()).Kind() {
49 case reflect.Struct:
50 fallthrough
51 case reflect.Map:
52 if err = deepMerge(dstElement, srcElement, visited, depth+1, overwrite); err != nil {
53 return
54 }
55 }
56 if !isEmptyValue(srcElement) && (overwrite || (!dstElement.IsValid() || isEmptyValue(dst))) {
57 if dst.IsNil() {
58 dst.Set(reflect.MakeMap(dst.Type()))
59 }
60 dst.SetMapIndex(key, srcElement)
61 }
62 }
63 case reflect.Ptr:
64 fallthrough
65 case reflect.Interface:
66 if src.IsNil() {
67 break
68 } else if dst.IsNil() {
69 if dst.CanSet() && (overwrite || isEmptyValue(dst)) {
70 dst.Set(src)
71 }
72 } else if err = deepMerge(dst.Elem(), src.Elem(), visited, depth+1, overwrite); err != nil {
73 return
74 }
75 default:
76 if dst.CanSet() && !isEmptyValue(src) && (overwrite || isEmptyValue(dst)) {
77 dst.Set(src)
78 }
79 }
80 return
81 }
82
83 // Merge sets fields' values in dst from src if they have a zero
84 // value of their type.
85 // dst and src must be valid same-type structs and dst must be
86 // a pointer to struct.
87 // It won't merge unexported (private) fields and will do recursively
88 // any exported field.
89 func Merge(dst, src interface{}) error {
90 return merge(dst, src, false)
91 }
92
93 func MergeWithOverwrite(dst, src interface{}) error {
94 return merge(dst, src, true)
95 }
96
97 func merge(dst, src interface{}, overwrite bool) error {
98 var (
99 vDst, vSrc reflect.Value
100 err error
101 )
102 if vDst, vSrc, err = resolveValues(dst, src); err != nil {
103 return err
104 }
105 if vDst.Type() != vSrc.Type() {
106 return ErrDifferentArgumentsTypes
107 }
108 return deepMerge(vDst, vSrc, make(map[uintptr]*visit), 0, overwrite)
109 }
+0
-90
debian/golang-github-imdario-mergo-dev/usr/share/gocode/src/github.com/imdario/mergo/mergo.go less more
0 // Copyright 2013 Dario Castañé. All rights reserved.
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Based on src/pkg/reflect/deepequal.go from official
6 // golang's stdlib.
7
8 package mergo
9
10 import (
11 "errors"
12 "reflect"
13 )
14
15 // Errors reported by Mergo when it finds invalid arguments.
16 var (
17 ErrNilArguments = errors.New("src and dst must not be nil")
18 ErrDifferentArgumentsTypes = errors.New("src and dst must be of same type")
19 ErrNotSupported = errors.New("only structs and maps are supported")
20 ErrExpectedMapAsDestination = errors.New("dst was expected to be a map")
21 ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
22 )
23
24 // During deepMerge, must keep track of checks that are
25 // in progress. The comparison algorithm assumes that all
26 // checks in progress are true when it reencounters them.
27 // Visited are stored in a map indexed by 17 * a1 + a2;
28 type visit struct {
29 ptr uintptr
30 typ reflect.Type
31 next *visit
32 }
33
34 // From src/pkg/encoding/json.
35 func isEmptyValue(v reflect.Value) bool {
36 switch v.Kind() {
37 case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
38 return v.Len() == 0
39 case reflect.Bool:
40 return !v.Bool()
41 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
42 return v.Int() == 0
43 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
44 return v.Uint() == 0
45 case reflect.Float32, reflect.Float64:
46 return v.Float() == 0
47 case reflect.Interface, reflect.Ptr:
48 return v.IsNil()
49 }
50 return false
51 }
52
53 func resolveValues(dst, src interface{}) (vDst, vSrc reflect.Value, err error) {
54 if dst == nil || src == nil {
55 err = ErrNilArguments
56 return
57 }
58 vDst = reflect.ValueOf(dst).Elem()
59 if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map {
60 err = ErrNotSupported
61 return
62 }
63 vSrc = reflect.ValueOf(src)
64 // We check if vSrc is a pointer to dereference it.
65 if vSrc.Kind() == reflect.Ptr {
66 vSrc = vSrc.Elem()
67 }
68 return
69 }
70
71 // Traverses recursively both values, assigning src's fields values to dst.
72 // The map argument tracks comparisons that have already been seen, which allows
73 // short circuiting on recursive types.
74 func deeper(dst, src reflect.Value, visited map[uintptr]*visit, depth int) (err error) {
75 if dst.CanAddr() {
76 addr := dst.UnsafeAddr()
77 h := 17 * addr
78 seen := visited[h]
79 typ := dst.Type()
80 for p := seen; p != nil; p = p.next {
81 if p.ptr == addr && p.typ == typ {
82 return nil
83 }
84 }
85 // Remember, remember...
86 visited[h] = &visit{addr, typ, seen}
87 }
88 return // TODO refactor
89 }
+0
-310
debian/golang-github-imdario-mergo-dev/usr/share/gocode/src/github.com/imdario/mergo/mergo_test.go less more
0 // Copyright 2013 Dario Castañé. All rights reserved.
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package mergo
6
7 import (
8 "reflect"
9 "testing"
10 )
11
12 type simpleTest struct {
13 Value int
14 }
15
16 type complexTest struct {
17 St simpleTest
18 sz int
19 Id string
20 }
21
22 type moreComplextText struct {
23 Ct complexTest
24 St simpleTest
25 Nt simpleTest
26 }
27
28 type pointerTest struct {
29 C *simpleTest
30 }
31
32 type sliceTest struct {
33 S []int
34 }
35
36 func TestKb(t *testing.T) {
37 type testStruct struct {
38 Name string
39 KeyValue map[string]interface{}
40 }
41
42 akv := make(map[string]interface{})
43 akv["Key1"] = "not value 1"
44 akv["Key2"] = "value2"
45 a := testStruct{}
46 a.Name = "A"
47 a.KeyValue = akv
48
49 bkv := make(map[string]interface{})
50 bkv["Key1"] = "value1"
51 bkv["Key3"] = "value3"
52 b := testStruct{}
53 b.Name = "B"
54 b.KeyValue = bkv
55
56 ekv := make(map[string]interface{})
57 ekv["Key1"] = "value1"
58 ekv["Key2"] = "value2"
59 ekv["Key3"] = "value3"
60 expected := testStruct{}
61 expected.Name = "B"
62 expected.KeyValue = ekv
63
64 Merge(&b, a)
65
66 if !reflect.DeepEqual(b, expected) {
67 t.Errorf("Actual: %#v did not match \nExpected: %#v", b, expected)
68 }
69 }
70
71 func TestNil(t *testing.T) {
72 if err := Merge(nil, nil); err != ErrNilArguments {
73 t.Fail()
74 }
75 }
76
77 func TestDifferentTypes(t *testing.T) {
78 a := simpleTest{42}
79 b := 42
80 if err := Merge(&a, b); err != ErrDifferentArgumentsTypes {
81 t.Fail()
82 }
83 }
84
85 func TestSimpleStruct(t *testing.T) {
86 a := simpleTest{}
87 b := simpleTest{42}
88 if err := Merge(&a, b); err != nil {
89 t.FailNow()
90 }
91 if a.Value != 42 {
92 t.Fatalf("b not merged in properly: a.Value(%d) != b.Value(%d)", a.Value, b.Value)
93 }
94 if !reflect.DeepEqual(a, b) {
95 t.FailNow()
96 }
97 }
98
99 func TestComplexStruct(t *testing.T) {
100 a := complexTest{}
101 a.Id = "athing"
102 b := complexTest{simpleTest{42}, 1, "bthing"}
103 if err := Merge(&a, b); err != nil {
104 t.FailNow()
105 }
106 if a.St.Value != 42 {
107 t.Fatalf("b not merged in properly: a.St.Value(%d) != b.St.Value(%d)", a.St.Value, b.St.Value)
108 }
109 if a.sz == 1 {
110 t.Fatalf("a's private field sz not preserved from merge: a.sz(%d) == b.sz(%d)", a.sz, b.sz)
111 }
112 if a.Id == b.Id {
113 t.Fatalf("a's field Id merged unexpectedly: a.Id(%s) == b.Id(%s)", a.Id, b.Id)
114 }
115 }
116
117 func TestComplexStructWithOverwrite(t *testing.T) {
118 a := complexTest{simpleTest{1}, 1, "do-not-overwrite-with-empty-value"}
119 b := complexTest{simpleTest{42}, 2, ""}
120
121 expect := complexTest{simpleTest{42}, 1, "do-not-overwrite-with-empty-value"}
122 if err := MergeWithOverwrite(&a, b); err != nil {
123 t.FailNow()
124 }
125
126 if !reflect.DeepEqual(a, expect) {
127 t.Fatalf("Test failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", a, expect)
128 }
129 }
130
131 func TestPointerStruct(t *testing.T) {
132 s1 := simpleTest{}
133 s2 := simpleTest{19}
134 a := pointerTest{&s1}
135 b := pointerTest{&s2}
136 if err := Merge(&a, b); err != nil {
137 t.FailNow()
138 }
139 if a.C.Value != b.C.Value {
140 t.Fatalf("b not merged in properly: a.C.Value(%d) != b.C.Value(%d)", a.C.Value, b.C.Value)
141 }
142 }
143
144 type embeddingStruct struct {
145 embeddedStruct
146 }
147
148 type embeddedStruct struct {
149 A string
150 }
151
152 func TestEmbeddedStruct(t *testing.T) {
153 tests := []struct {
154 src embeddingStruct
155 dst embeddingStruct
156 expected embeddingStruct
157 }{
158 {
159 src: embeddingStruct{
160 embeddedStruct{"foo"},
161 },
162 dst: embeddingStruct{
163 embeddedStruct{""},
164 },
165 expected: embeddingStruct{
166 embeddedStruct{"foo"},
167 },
168 },
169 {
170 src: embeddingStruct{
171 embeddedStruct{""},
172 },
173 dst: embeddingStruct{
174 embeddedStruct{"bar"},
175 },
176 expected: embeddingStruct{
177 embeddedStruct{"bar"},
178 },
179 },
180 {
181 src: embeddingStruct{
182 embeddedStruct{"foo"},
183 },
184 dst: embeddingStruct{
185 embeddedStruct{"bar"},
186 },
187 expected: embeddingStruct{
188 embeddedStruct{"bar"},
189 },
190 },
191 }
192
193 for _, test := range tests {
194 err := Merge(&test.dst, test.src)
195 if err != nil {
196 t.Errorf("unexpected error: %v", err)
197 continue
198 }
199 if !reflect.DeepEqual(test.dst, test.expected) {
200 t.Errorf("unexpected output\nexpected:\n%+v\nsaw:\n%+v\n", test.expected, test.dst)
201 }
202 }
203 }
204
205 func TestPointerStructNil(t *testing.T) {
206 a := pointerTest{nil}
207 b := pointerTest{&simpleTest{19}}
208 if err := Merge(&a, b); err != nil {
209 t.FailNow()
210 }
211 if a.C.Value != b.C.Value {
212 t.Fatalf("b not merged in a properly: a.C.Value(%d) != b.C.Value(%d)", a.C.Value, b.C.Value)
213 }
214 }
215
216 func TestSliceStruct(t *testing.T) {
217 a := sliceTest{}
218 b := sliceTest{[]int{1, 2, 3}}
219 if err := Merge(&a, b); err != nil {
220 t.FailNow()
221 }
222 if len(b.S) != 3 {
223 t.FailNow()
224 }
225 if len(a.S) != len(b.S) {
226 t.Fatalf("b not merged in a proper way %d != %d", len(a.S), len(b.S))
227 }
228
229 a = sliceTest{[]int{1}}
230 b = sliceTest{[]int{1, 2, 3}}
231 if err := Merge(&a, b); err != nil {
232 t.FailNow()
233 }
234 if len(a.S) != 1 {
235 t.FailNow()
236 }
237 if len(a.S) == len(b.S) {
238 t.Fatalf("b merged unexpectedly %d != %d", len(a.S), len(b.S))
239 }
240 }
241
242 func TestMapsWithOverwrite(t *testing.T) {
243 m := map[string]simpleTest{
244 "a": simpleTest{}, // overwritten by 16
245 "b": simpleTest{42}, // not overwritten by empty value
246 "c": simpleTest{13}, // overwritten by 12
247 "d": simpleTest{61},
248 }
249 n := map[string]simpleTest{
250 "a": simpleTest{16},
251 "b": simpleTest{},
252 "c": simpleTest{12},
253 "e": simpleTest{14},
254 }
255 expect := map[string]simpleTest{
256 "a": simpleTest{16},
257 "b": simpleTest{},
258 "c": simpleTest{12},
259 "d": simpleTest{61},
260 "e": simpleTest{14},
261 }
262
263 if err := MergeWithOverwrite(&m, n); err != nil {
264 t.Fatalf(err.Error())
265 }
266
267 if !reflect.DeepEqual(m, expect) {
268 t.Fatalf("Test failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", m, expect)
269 }
270 }
271
272 func TestMaps(t *testing.T) {
273 m := map[string]simpleTest{
274 "a": simpleTest{},
275 "b": simpleTest{42},
276 "c": simpleTest{13},
277 "d": simpleTest{61},
278 }
279 n := map[string]simpleTest{
280 "a": simpleTest{16},
281 "b": simpleTest{},
282 "c": simpleTest{12},
283 "e": simpleTest{14},
284 }
285 expect := map[string]simpleTest{
286 "a": simpleTest{0},
287 "b": simpleTest{42},
288 "c": simpleTest{13},
289 "d": simpleTest{61},
290 "e": simpleTest{14},
291 }
292
293 if err := Merge(&m, n); err != nil {
294 t.Fatalf(err.Error())
295 }
296
297 if !reflect.DeepEqual(m, expect) {
298 t.Fatalf("Test failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", m, expect)
299 }
300 if m["a"].Value != 0 {
301 t.Fatalf(`n merged in m because I solved non-addressable map values TODO: m["a"].Value(%d) != n["a"].Value(%d)`, m["a"].Value, n["a"].Value)
302 }
303 if m["b"].Value != 42 {
304 t.Fatalf(`n wrongly merged in m: m["b"].Value(%d) != n["b"].Value(%d)`, m["b"].Value, n["b"].Value)
305 }
306 if m["c"].Value != 13 {
307 t.Fatalf(`n overwritten in m: m["c"].Value(%d) != n["c"].Value(%d)`, m["c"].Value, n["c"].Value)
308 }
309 }
+0
-18
debian/golang-github-imdario-mergo-dev.debhelper.log less more
0 dh_auto_configure
1 dh_auto_build
2 dh_auto_test
3 dh_prep
4 dh_auto_install
5 dh_installdocs
6 dh_installchangelogs
7 dh_perl
8 dh_link
9 dh_strip_nondeterminism
10 dh_compress
11 dh_fixperms
12 dh_installdeb
13 dh_golang
14 dh_gencontrol
15 dh_md5sums
16 dh_builddeb
17 dh_builddeb
+0
-3
debian/golang-github-imdario-mergo-dev.substvars less more
0 misc:Built-Using=golang (= 2:1.4.3-3),
1 misc:Depends=
2 misc:Pre-Depends=