Codebase list golang-github-imdario-mergo / fresh-snapshots/upstream
Import upstream version 0.3.13+git20220917.1.c42713b Debian Janitor a month ago
21 changed file(s) with 274 addition(s) and 114 deletion(s). Raw diff Collapse all Expand all
+0
-5
.github/FUNDING.yml less more
0 # These are supported funding model platforms
1
2 github: imdario
3 ko_fi: dariocc
4 custom: https://beerpay.io/imdario/mergo
+0
-33
.gitignore less more
0 #### joe made this: http://goel.io/joe
1
2 #### go ####
3 # Binaries for programs and plugins
4 *.exe
5 *.dll
6 *.so
7 *.dylib
8
9 # Test binary, build with `go test -c`
10 *.test
11
12 # Output of the go coverage tool, specifically when used with LiteIDE
13 *.out
14
15 # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
16 .glide/
17
18 #### vim ####
19 # Swap
20 [._]*.s[a-v][a-z]
21 [._]*.sw[a-p]
22 [._]s[a-v][a-z]
23 [._]sw[a-p]
24
25 # Session
26 Session.vim
27
28 # Temporary
29 .netrwhist
30 *~
31 # Auto-generated tag files
32 tags
0 <!-- omit in toc -->
1 # Contributing to mergo
2
3 First off, thanks for taking the time to contribute! ❤️
4
5 All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions. 🎉
6
7 > And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about:
8 > - Star the project
9 > - Tweet about it
10 > - Refer this project in your project's readme
11 > - Mention the project at local meetups and tell your friends/colleagues
12
13 <!-- omit in toc -->
14 ## Table of Contents
15
16 - [Code of Conduct](#code-of-conduct)
17 - [I Have a Question](#i-have-a-question)
18 - [I Want To Contribute](#i-want-to-contribute)
19 - [Reporting Bugs](#reporting-bugs)
20 - [Suggesting Enhancements](#suggesting-enhancements)
21
22 ## Code of Conduct
23
24 This project and everyone participating in it is governed by the
25 [mergo Code of Conduct](https://github.com/imdario/mergoblob/master/CODE_OF_CONDUCT.md).
26 By participating, you are expected to uphold this code. Please report unacceptable behavior
27 to <>.
28
29
30 ## I Have a Question
31
32 > If you want to ask a question, we assume that you have read the available [Documentation](https://pkg.go.dev/github.com/imdario/mergo).
33
34 Before you ask a question, it is best to search for existing [Issues](https://github.com/imdario/mergo/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
35
36 If you then still feel the need to ask a question and need clarification, we recommend the following:
37
38 - Open an [Issue](https://github.com/imdario/mergo/issues/new).
39 - Provide as much context as you can about what you're running into.
40 - Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant.
41
42 We will then take care of the issue as soon as possible.
43
44 ## I Want To Contribute
45
46 > ### Legal Notice <!-- omit in toc -->
47 > When contributing to this project, you must agree that you have authored 100% of the content, that you have the necessary rights to the content and that the content you contribute may be provided under the project license.
48
49 ### Reporting Bugs
50
51 <!-- omit in toc -->
52 #### Before Submitting a Bug Report
53
54 A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible.
55
56 - Make sure that you are using the latest version.
57 - Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](). If you are looking for support, you might want to check [this section](#i-have-a-question)).
58 - To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/imdario/mergoissues?q=label%3Abug).
59 - Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue.
60 - Collect information about the bug:
61 - Stack trace (Traceback)
62 - OS, Platform and Version (Windows, Linux, macOS, x86, ARM)
63 - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant.
64 - Possibly your input and the output
65 - Can you reliably reproduce the issue? And can you also reproduce it with older versions?
66
67 <!-- omit in toc -->
68 #### How Do I Submit a Good Bug Report?
69
70 > You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to .
71 <!-- You may add a PGP key to allow the messages to be sent encrypted as well. -->
72
73 We use GitHub issues to track bugs and errors. If you run into an issue with the project:
74
75 - Open an [Issue](https://github.com/imdario/mergo/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
76 - Explain the behavior you would expect and the actual behavior.
77 - Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
78 - Provide the information you collected in the previous section.
79
80 Once it's filed:
81
82 - The project team will label the issue accordingly.
83 - A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced.
84 - If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be implemented by someone.
85
86 ### Suggesting Enhancements
87
88 This section guides you through submitting an enhancement suggestion for mergo, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
89
90 <!-- omit in toc -->
91 #### Before Submitting an Enhancement
92
93 - Make sure that you are using the latest version.
94 - Read the [documentation]() carefully and find out if the functionality is already covered, maybe by an individual configuration.
95 - Perform a [search](https://github.com/imdario/mergo/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
96 - Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.
97
98 <!-- omit in toc -->
99 #### How Do I Submit a Good Enhancement Suggestion?
100
101 Enhancement suggestions are tracked as [GitHub issues](https://github.com/imdario/mergo/issues).
102
103 - Use a **clear and descriptive title** for the issue to identify the suggestion.
104 - Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
105 - **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you.
106 - You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. <!-- this should only be included if the project has a GUI -->
107 - **Explain why this enhancement would be useful** to most mergo users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
108
109 <!-- omit in toc -->
110 ## Attribution
111 This guide is based on the **contributing-gen**. [Make your own](https://github.com/bttger/contributing-gen)!
77 [![Coverage Status][9]][10]
88 [![Sourcegraph][11]][12]
99 [![FOSSA Status][13]][14]
10
11 [![GoCenter Kudos][15]][16]
10 [![Become my sponsor][15]][16]
1211
1312 [1]: https://travis-ci.org/imdario/mergo.png
1413 [2]: https://travis-ci.org/imdario/mergo
2423 [12]: https://sourcegraph.com/github.com/imdario/mergo?badge
2524 [13]: https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=shield
2625 [14]: https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_shield
27 [15]: https://search.gocenter.io/api/ui/badge/github.com%2Fimdario%2Fmergo
28 [16]: https://search.gocenter.io/github.com/imdario/mergo
26 [15]: https://img.shields.io/github/sponsors/imdario
27 [16]: https://github.com/sponsors/imdario
2928
3029 A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
3130
3534
3635 ## Status
3736
38 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).
37 It is ready for production use. [It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, Microsoft, etc](https://github.com/imdario/mergo#mergo-in-the-wild).
3938
4039 ### Important note
4140
42 Please keep in mind that a problematic PR broke [0.3.9](//github.com/imdario/mergo/releases/tag/0.3.9). I reverted it in [0.3.10](//github.com/imdario/mergo/releases/tag/0.3.10), and I consider it stable but not bug-free. Also, this version adds suppot for go modules.
41 Please keep in mind that a problematic PR broke [0.3.9](//github.com/imdario/mergo/releases/tag/0.3.9). I reverted it in [0.3.10](//github.com/imdario/mergo/releases/tag/0.3.10), and I consider it stable but not bug-free. Also, this version adds support for go modules.
4342
4443 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). I added an optional/variadic argument so that it won't break the existing code.
4544
5049 If Mergo is useful to you, consider buying me a coffee, a beer, or making a monthly donation to allow me to keep building great free software. :heart_eyes:
5150
5251 <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>
53 [![Beerpay](https://beerpay.io/imdario/mergo/badge.svg)](https://beerpay.io/imdario/mergo)
54 [![Beerpay](https://beerpay.io/imdario/mergo/make-wish.svg)](https://beerpay.io/imdario/mergo)
5552 <a href="https://liberapay.com/dario/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a>
53 <a href='https://github.com/sponsors/imdario' target='_blank'><img alt="Become my sponsor" src="https://img.shields.io/github/sponsors/imdario?style=for-the-badge" /></a>
5654
5755 ### Mergo in the wild
5856
57 - [cli/cli](https://github.com/cli/cli)
5958 - [moby/moby](https://github.com/moby/moby)
6059 - [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes)
6160 - [vmware/dispatch](https://github.com/vmware/dispatch)
9796 - [jnuthong/item_search](https://github.com/jnuthong/item_search)
9897 - [bukalapak/snowboard](https://github.com/bukalapak/snowboard)
9998 - [containerssh/containerssh](https://github.com/containerssh/containerssh)
99 - [goreleaser/goreleaser](https://github.com/goreleaser/goreleaser)
100 - [tjpnz/structbot](https://github.com/tjpnz/structbot)
100101
101102 ## Install
102103
167168
168169 Note: if test are failing due missing package, please execute:
169170
170 go get gopkg.in/yaml.v2
171 go get gopkg.in/yaml.v3
171172
172173 ### Transformers
173174
217218 }
218219 ```
219220
220
221221 ## Contact me
222222
223223 If I can help you, you have an idea or you are using Mergo in your projects, don't hesitate to drop me a line (or a pull request): [@im_dario](https://twitter.com/im_dario)
226226
227227 Written by [Dario Castañé](http://dario.im).
228228
229 ## Top Contributors
230
231 [![0](https://sourcerer.io/fame/imdario/imdario/mergo/images/0)](https://sourcerer.io/fame/imdario/imdario/mergo/links/0)
232 [![1](https://sourcerer.io/fame/imdario/imdario/mergo/images/1)](https://sourcerer.io/fame/imdario/imdario/mergo/links/1)
233 [![2](https://sourcerer.io/fame/imdario/imdario/mergo/images/2)](https://sourcerer.io/fame/imdario/imdario/mergo/links/2)
234 [![3](https://sourcerer.io/fame/imdario/imdario/mergo/images/3)](https://sourcerer.io/fame/imdario/imdario/mergo/links/3)
235 [![4](https://sourcerer.io/fame/imdario/imdario/mergo/images/4)](https://sourcerer.io/fame/imdario/imdario/mergo/links/4)
236 [![5](https://sourcerer.io/fame/imdario/imdario/mergo/images/5)](https://sourcerer.io/fame/imdario/imdario/mergo/links/5)
237 [![6](https://sourcerer.io/fame/imdario/imdario/mergo/images/6)](https://sourcerer.io/fame/imdario/imdario/mergo/links/6)
238 [![7](https://sourcerer.io/fame/imdario/imdario/mergo/images/7)](https://sourcerer.io/fame/imdario/imdario/mergo/links/7)
239
240
241229 ## License
242230
243231 [BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) license, as [Go language](http://golang.org/LICENSE).
0 # Security Policy
1
2 ## Supported Versions
3
4 | Version | Supported |
5 | ------- | ------------------ |
6 | 0.3.x | :white_check_mark: |
7 | < 0.3 | :x: |
8
9 ## Reporting a Vulnerability
10
11 Report any vulnerability to d@rio.hn.
11
22 go 1.13
33
4 require gopkg.in/yaml.v2 v2.3.0
4 require gopkg.in/yaml.v3 v3.0.1
00 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
11 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2 gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
3 gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2222 t.Fatal(err)
2323 }
2424 testCases := []struct {
25 expected interface{}
2526 key string
26 expected interface{}
2727 }{
2828 {
29 nil,
2930 "col1",
30 nil,
3131 },
3232 {
33 4,
3334 "col2",
34 4,
3535 },
3636 {
37 nil,
3738 "col3",
38 nil,
3939 },
4040 }
4141 for _, tC := range testCases {
1111 }
1212
1313 type embeddedTestB struct {
14 Address string
1415 embeddedTestA
15 Address string
1616 }
1717
1818 func TestMergeEmbedded(t *testing.T) {
88
99 func TestIssue143(t *testing.T) {
1010 testCases := []struct {
11 expected func(map[string]interface{}) error
1112 options []func(*mergo.Config)
12 expected func(map[string]interface{}) error
1313 }{
1414 {
1515 options: []func(*mergo.Config){mergo.WithOverride},
0 package mergo_test
1
2 import (
3 "testing"
4
5 "github.com/imdario/mergo"
6 )
7
8 func TestIssue209(t *testing.T) {
9 dst := []string{"a", "b"}
10 src := []string{"c", "d"}
11
12 if err := mergo.Merge(&dst, src, mergo.WithAppendSlice); err != nil {
13 t.Error(err)
14 }
15
16 expected := []string{"a", "b", "c", "d"}
17 if len(dst) != len(expected) {
18 t.Errorf("arrays not equal length")
19 }
20 for i := range expected {
21 if dst[i] != expected[i] {
22 t.Errorf("array elements at %d are not equal", i)
23 }
24 }
25 }
3232 t.Error(err)
3333 }
3434 testCases := []struct {
35 expected interface{}
3536 key string
36 expected interface{}
3737 }{
3838 {
39 3,
3940 "A",
40 3,
4141 },
4242 {
43 "",
4344 "B",
44 "",
4545 },
4646 {
47 false,
4748 "C",
48 false,
4949 },
5050 }
5151 for _, tC := range testCases {
99 Data map[string]string
1010 }
1111
12
1312 func TestIssue90(t *testing.T) {
14 dst := map[string]structWithStringMap{
15 "struct": {
16 Data: nil,
13 dst := map[string]structWithStringMap{
14 "struct": {
15 Data: nil,
1716 },
1817 }
1918 src := map[string]structWithStringMap{
4343 }
4444 }
4545 // Remember, remember...
46 visited[h] = &visit{addr, typ, seen}
46 visited[h] = &visit{typ, seen, addr}
4747 }
4848 zeroValue := reflect.Value{}
4949 switch dst.Kind() {
3737 }
3838
3939 type Config struct {
40 Transformers Transformers
4041 Overwrite bool
4142 AppendSlice bool
4243 TypeCheck bool
43 Transformers Transformers
4444 overwriteWithEmptyValue bool
4545 overwriteSliceWithEmptyValue bool
4646 sliceDeepCopy bool
7575 }
7676 }
7777 // Remember, remember...
78 visited[h] = &visit{addr, typ, seen}
79 }
80
81 if config.Transformers != nil && !isEmptyValue(dst) {
78 visited[h] = &visit{typ, seen, addr}
79 }
80
81 if config.Transformers != nil && !isReflectNil(dst) && dst.IsValid() {
8282 if fn := config.Transformers.Transformer(dst.Type()); fn != nil {
8383 err = fn(dst, src)
8484 return
1818 }
1919
2020 type foo struct {
21 Bar *bar
2122 s string
22 Bar *bar
2323 }
2424
2525 type bar struct {
26 s map[string]string
2627 i int
27 s map[string]string
2828 }
2929
3030 func TestMergeWithTransformerNilStruct(t *testing.T) {
1616 var (
1717 ErrNilArguments = errors.New("src and dst must not be nil")
1818 ErrDifferentArgumentsTypes = errors.New("src and dst must be of same type")
19 ErrNotSupported = errors.New("only structs and maps are supported")
19 ErrNotSupported = errors.New("only structs, maps, and slices are supported")
2020 ErrExpectedMapAsDestination = errors.New("dst was expected to be a map")
2121 ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct")
2222 ErrNonPointerAgument = errors.New("dst must be a pointer")
2727 // checks in progress are true when it reencounters them.
2828 // Visited are stored in a map indexed by 17 * a1 + a2;
2929 type visit struct {
30 ptr uintptr
3130 typ reflect.Type
3231 next *visit
32 ptr uintptr
3333 }
3434
3535 // From src/pkg/encoding/json/encode.go.
6464 return
6565 }
6666 vDst = reflect.ValueOf(dst).Elem()
67 if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map {
67 if vDst.Kind() != reflect.Struct && vDst.Kind() != reflect.Map && vDst.Kind() != reflect.Slice {
6868 err = ErrNotSupported
6969 return
7070 }
1212 "time"
1313
1414 "github.com/imdario/mergo"
15 "gopkg.in/yaml.v2"
15 "gopkg.in/yaml.v3"
1616 )
1717
1818 type simpleTest struct {
2020 }
2121
2222 type complexTest struct {
23 ID string
2324 St simpleTest
2425 sz int
25 ID string
2626 }
2727
2828 type mapTest struct {
4949
5050 func TestKb(t *testing.T) {
5151 type testStruct struct {
52 KeyValue map[string]interface{}
5253 Name string
53 KeyValue map[string]interface{}
5454 }
5555
5656 akv := make(map[string]interface{})
115115 func TestComplexStruct(t *testing.T) {
116116 a := complexTest{}
117117 a.ID = "athing"
118 b := complexTest{simpleTest{42}, 1, "bthing"}
118 b := complexTest{"bthing", simpleTest{42}, 1}
119119 if err := mergo.Merge(&a, b); err != nil {
120120 t.FailNow()
121121 }
131131 }
132132
133133 func TestComplexStructWithOverwrite(t *testing.T) {
134 a := complexTest{simpleTest{1}, 1, "do-not-overwrite-with-empty-value"}
135 b := complexTest{simpleTest{42}, 2, ""}
136
137 expect := complexTest{simpleTest{42}, 1, "do-not-overwrite-with-empty-value"}
134 a := complexTest{"do-not-overwrite-with-empty-value", simpleTest{1}, 1}
135 b := complexTest{"", simpleTest{42}, 2}
136
137 expect := complexTest{"do-not-overwrite-with-empty-value", simpleTest{42}, 1}
138138 if err := mergo.MergeWithOverwrite(&a, b); err != nil {
139139 t.FailNow()
140140 }
416416 Msg2 string
417417 }
418418 type params struct {
419 Name string
420419 Multi *multiPtr
421420 Final *final
421 Name string
422422 }
423423 type config struct {
424 Params *params
424425 Foo string
425426 Bar string
426 Params *params
427427 }
428428
429429 cases := []struct {
430 name string
431 overwrite bool
432430 changes *config
433431 target *config
434432 output *config
433 name string
434 overwrite bool
435435 }{
436436 {
437437 name: "Should overwrite values in target for non-nil values in source",
602602 func TestYAMLMaps(t *testing.T) {
603603 thing := loadYAML("testdata/thing.yml")
604604 license := loadYAML("testdata/license.yml")
605 ft := thing["fields"].(map[interface{}]interface{})
606 fl := license["fields"].(map[interface{}]interface{})
605 ft := thing["fields"].(map[string]interface{})
606 fl := license["fields"].(map[string]interface{})
607607 // license has one extra field (site) and another already existing in thing (author) that Mergo won't override.
608608 expectedLength := len(ft) + len(fl) - 1
609609 if err := mergo.Merge(&license, thing); err != nil {
610610 t.Error(err.Error())
611611 }
612 currentLength := len(license["fields"].(map[interface{}]interface{}))
612 currentLength := len(license["fields"].(map[string]interface{}))
613613 if currentLength != expectedLength {
614614 t.Errorf(`thing not merged in license properly, license must have %d elements instead of %d`, expectedLength, currentLength)
615615 }
616 fields := license["fields"].(map[interface{}]interface{})
616 fields := license["fields"].(map[string]interface{})
617617 if _, ok := fields["id"]; !ok {
618618 t.Errorf(`thing not merged in license properly, license must have a new id field from thing`)
619619 }
720720 }
721721
722722 type pointerMapTest struct {
723 B *simpleTest
723724 A int
724725 hidden int
725 B *simpleTest
726726 }
727727
728728 func TestBackAndForth(t *testing.T) {
729 pt := pointerMapTest{42, 1, &simpleTest{66}}
729 pt := pointerMapTest{&simpleTest{66}, 42, 1}
730730 m := make(map[string]interface{})
731731 if err := mergo.Map(&m, pt); err != nil {
732732 t.FailNow()
762762
763763 func TestEmbeddedPointerUnpacking(t *testing.T) {
764764 tests := []struct{ input pointerMapTest }{
765 {pointerMapTest{42, 1, nil}},
766 {pointerMapTest{42, 1, &simpleTest{66}}},
765 {pointerMapTest{nil, 42, 1}},
766 {pointerMapTest{&simpleTest{66}, 42, 1}},
767767 }
768768 newValue := 77
769769 m := map[string]interface{}{
899899 func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) {
900900 testCases := []struct {
901901 name string
902 err string
902903 options []func(*mergo.Config)
903 err string
904904 }{
905905 {
906906 "With override and append slice",
907 "cannot append two slices with different type",
907908 []func(*mergo.Config){mergo.WithOverride, mergo.WithAppendSlice},
908 "cannot append two slices with different type",
909909 },
910910 {
911911 "With override and type check",
912 "cannot override two slices with different type",
912913 []func(*mergo.Config){mergo.WithOverride, mergo.WithTypeCheck},
913 "cannot override two slices with different type",
914914 },
915915 }
916916 for _, tc := range testCases {
929929 }
930930 }
931931
932 func TestMergeSlicesIsNotSupported(t *testing.T) {
932 func TestMergeDifferentSlicesIsNotSupported(t *testing.T) {
933933 src := []string{"a", "b"}
934934 dst := []int{1, 2}
935935
936 if err := mergo.Merge(&src, &dst, mergo.WithOverride, mergo.WithAppendSlice); err != mergo.ErrNotSupported {
936 if err := mergo.Merge(&src, &dst, mergo.WithOverride, mergo.WithAppendSlice); err != mergo.ErrDifferentArgumentsTypes {
937937 t.Errorf("expected %q, got %q", mergo.ErrNotSupported, err)
938938 }
939939 }
0 package mergo
1
2 import (
3 "reflect"
4 "testing"
5 "time"
6 )
7
8 type transformer struct {
9 }
10
11 func (s *transformer) Transformer(t reflect.Type) func(dst, src reflect.Value) error {
12 return nil
13 }
14
15 func Test_deepMergeTransformerInvalidDestination(t *testing.T) {
16 foo := time.Time{}
17 src := reflect.ValueOf(foo)
18 deepMerge(reflect.Value{}, src, make(map[uintptr]*visit), 0, &Config{
19 Transformers: &transformer{},
20 })
21 // this test is intentionally not asserting on anything, it's sole
22 // purpose to verify deepMerge doesn't panic when a transformer is
23 // passed and the destination is invalid.
24 }
0 package mergo_test
1
2 import (
3 "reflect"
4 "testing"
5
6 "github.com/imdario/mergo"
7 )
8
9 func TestMergeWithTransformerZeroValue(t *testing.T) {
10 // This test specifically tests that a transformer can be used to
11 // prevent overwriting a zero value (in this case a bool). This would fail prior to #211
12 type fooWithBoolPtr struct {
13 b *bool
14 }
15 var Bool = func(b bool) *bool { return &b }
16 a := fooWithBoolPtr{b: Bool(false)}
17 b := fooWithBoolPtr{b: Bool(true)}
18
19 if err := mergo.Merge(&a, &b, mergo.WithTransformers(&transformer{
20 m: map[reflect.Type]func(dst, src reflect.Value) error{
21 reflect.TypeOf(Bool(false)): func(dst, src reflect.Value) error {
22 if dst.CanSet() && dst.IsNil() {
23 dst.Set(src)
24 }
25 return nil
26 },
27 },
28 })); err != nil {
29 t.Error(err)
30 }
31
32 if *a.b != false {
33 t.Errorf("b not merged in properly: a.b(%v) != expected(%v)", a.b, false)
34 }
35 }
5252 }
5353
5454 type issue146Foo struct {
55 B map[string]issue146Bar
5556 A string
56 B map[string]issue146Bar
5757 }
5858
5959 type issue146Bar struct {