New upstream snapshot.
Debian Janitor
10 months ago
0 | # These are supported funding model platforms | |
1 | ||
2 | github: imdario | |
3 | ko_fi: dariocc | |
4 | custom: https://beerpay.io/imdario/mergo |
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)! |
7 | 7 | [![Coverage Status][9]][10] |
8 | 8 | [![Sourcegraph][11]][12] |
9 | 9 | [![FOSSA Status][13]][14] |
10 | ||
11 | [![GoCenter Kudos][15]][16] | |
10 | [![Become my sponsor][15]][16] | |
12 | 11 | |
13 | 12 | [1]: https://travis-ci.org/imdario/mergo.png |
14 | 13 | [2]: https://travis-ci.org/imdario/mergo |
24 | 23 | [12]: https://sourcegraph.com/github.com/imdario/mergo?badge |
25 | 24 | [13]: https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=shield |
26 | 25 | [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 | |
29 | 28 | |
30 | 29 | A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements. |
31 | 30 | |
35 | 34 | |
36 | 35 | ## Status |
37 | 36 | |
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). | |
39 | 38 | |
40 | 39 | ### Important note |
41 | 40 | |
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. | |
43 | 42 | |
44 | 43 | 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. |
45 | 44 | |
50 | 49 | 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: |
51 | 50 | |
52 | 51 | <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 | [](https://beerpay.io/imdario/mergo) | |
54 | [](https://beerpay.io/imdario/mergo) | |
55 | 52 | <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> | |
56 | 54 | |
57 | 55 | ### Mergo in the wild |
58 | 56 | |
57 | - [cli/cli](https://github.com/cli/cli) | |
59 | 58 | - [moby/moby](https://github.com/moby/moby) |
60 | 59 | - [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes) |
61 | 60 | - [vmware/dispatch](https://github.com/vmware/dispatch) |
97 | 96 | - [jnuthong/item_search](https://github.com/jnuthong/item_search) |
98 | 97 | - [bukalapak/snowboard](https://github.com/bukalapak/snowboard) |
99 | 98 | - [containerssh/containerssh](https://github.com/containerssh/containerssh) |
99 | - [goreleaser/goreleaser](https://github.com/goreleaser/goreleaser) | |
100 | - [tjpnz/structbot](https://github.com/tjpnz/structbot) | |
100 | 101 | |
101 | 102 | ## Install |
102 | 103 | |
167 | 168 | |
168 | 169 | Note: if test are failing due missing package, please execute: |
169 | 170 | |
170 | go get gopkg.in/yaml.v2 | |
171 | go get gopkg.in/yaml.v3 | |
171 | 172 | |
172 | 173 | ### Transformers |
173 | 174 | |
217 | 218 | } |
218 | 219 | ``` |
219 | 220 | |
220 | ||
221 | 221 | ## Contact me |
222 | 222 | |
223 | 223 | 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) |
226 | 226 | |
227 | 227 | Written by [Dario Castañé](http://dario.im). |
228 | 228 | |
229 | ## Top Contributors | |
230 | ||
231 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/0) | |
232 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/1) | |
233 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/2) | |
234 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/3) | |
235 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/4) | |
236 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/5) | |
237 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/6) | |
238 | [](https://sourcerer.io/fame/imdario/imdario/mergo/links/7) | |
239 | ||
240 | ||
241 | 229 | ## License |
242 | 230 | |
243 | 231 | [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 | ## Security contact information | |
10 | ||
11 | To report a security vulnerability, please use the | |
12 | [Tidelift security contact](https://tidelift.com/security). | |
13 | Tidelift will coordinate the fix and disclosure. |
0 | golang-github-imdario-mergo (0.3.13+git20230126.1.6d2237a-1) UNRELEASED; urgency=low | |
1 | ||
2 | * New upstream snapshot. | |
3 | ||
4 | -- Debian Janitor <janitor@jelmer.uk> Sat, 28 Jan 2023 01:15:28 -0000 | |
5 | ||
0 | 6 | golang-github-imdario-mergo (0.3.12-1) unstable; urgency=medium |
1 | 7 | |
2 | 8 | * Team upload. |
0 | 0 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
1 | 1 | 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= |
22 | 22 | t.Fatal(err) |
23 | 23 | } |
24 | 24 | testCases := []struct { |
25 | expected interface{} | |
25 | 26 | key string |
26 | expected interface{} | |
27 | 27 | }{ |
28 | 28 | { |
29 | nil, | |
29 | 30 | "col1", |
30 | nil, | |
31 | 31 | }, |
32 | 32 | { |
33 | 4, | |
33 | 34 | "col2", |
34 | 4, | |
35 | 35 | }, |
36 | 36 | { |
37 | nil, | |
37 | 38 | "col3", |
38 | nil, | |
39 | 39 | }, |
40 | 40 | } |
41 | 41 | for _, tC := range testCases { |
11 | 11 | } |
12 | 12 | |
13 | 13 | type embeddedTestB struct { |
14 | Address string | |
14 | 15 | embeddedTestA |
15 | Address string | |
16 | 16 | } |
17 | 17 | |
18 | 18 | func TestMergeEmbedded(t *testing.T) { |
8 | 8 | |
9 | 9 | func TestIssue143(t *testing.T) { |
10 | 10 | testCases := []struct { |
11 | expected func(map[string]interface{}) error | |
11 | 12 | options []func(*mergo.Config) |
12 | expected func(map[string]interface{}) error | |
13 | 13 | }{ |
14 | 14 | { |
15 | 15 | 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 | } |
32 | 32 | t.Error(err) |
33 | 33 | } |
34 | 34 | testCases := []struct { |
35 | expected interface{} | |
35 | 36 | key string |
36 | expected interface{} | |
37 | 37 | }{ |
38 | 38 | { |
39 | 3, | |
39 | 40 | "A", |
40 | 3, | |
41 | 41 | }, |
42 | 42 | { |
43 | "", | |
43 | 44 | "B", |
44 | "", | |
45 | 45 | }, |
46 | 46 | { |
47 | false, | |
47 | 48 | "C", |
48 | false, | |
49 | 49 | }, |
50 | 50 | } |
51 | 51 | for _, tC := range testCases { |
9 | 9 | Data map[string]string |
10 | 10 | } |
11 | 11 | |
12 | ||
13 | 12 | 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, | |
17 | 16 | }, |
18 | 17 | } |
19 | 18 | src := map[string]structWithStringMap{ |
43 | 43 | } |
44 | 44 | } |
45 | 45 | // Remember, remember... |
46 | visited[h] = &visit{addr, typ, seen} | |
46 | visited[h] = &visit{typ, seen, addr} | |
47 | 47 | } |
48 | 48 | zeroValue := reflect.Value{} |
49 | 49 | switch dst.Kind() { |
37 | 37 | } |
38 | 38 | |
39 | 39 | type Config struct { |
40 | Transformers Transformers | |
40 | 41 | Overwrite bool |
41 | 42 | AppendSlice bool |
42 | 43 | TypeCheck bool |
43 | Transformers Transformers | |
44 | 44 | overwriteWithEmptyValue bool |
45 | 45 | overwriteSliceWithEmptyValue bool |
46 | 46 | sliceDeepCopy bool |
75 | 75 | } |
76 | 76 | } |
77 | 77 | // 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() { | |
82 | 82 | if fn := config.Transformers.Transformer(dst.Type()); fn != nil { |
83 | 83 | err = fn(dst, src) |
84 | 84 | return |
18 | 18 | } |
19 | 19 | |
20 | 20 | type foo struct { |
21 | Bar *bar | |
21 | 22 | s string |
22 | Bar *bar | |
23 | 23 | } |
24 | 24 | |
25 | 25 | type bar struct { |
26 | s map[string]string | |
26 | 27 | i int |
27 | s map[string]string | |
28 | 28 | } |
29 | 29 | |
30 | 30 | func TestMergeWithTransformerNilStruct(t *testing.T) { |
16 | 16 | var ( |
17 | 17 | ErrNilArguments = errors.New("src and dst must not be nil") |
18 | 18 | 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") | |
20 | 20 | ErrExpectedMapAsDestination = errors.New("dst was expected to be a map") |
21 | 21 | ErrExpectedStructAsDestination = errors.New("dst was expected to be a struct") |
22 | 22 | ErrNonPointerAgument = errors.New("dst must be a pointer") |
27 | 27 | // checks in progress are true when it reencounters them. |
28 | 28 | // Visited are stored in a map indexed by 17 * a1 + a2; |
29 | 29 | type visit struct { |
30 | ptr uintptr | |
31 | 30 | typ reflect.Type |
32 | 31 | next *visit |
32 | ptr uintptr | |
33 | 33 | } |
34 | 34 | |
35 | 35 | // From src/pkg/encoding/json/encode.go. |
64 | 64 | return |
65 | 65 | } |
66 | 66 | 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 { | |
68 | 68 | err = ErrNotSupported |
69 | 69 | return |
70 | 70 | } |
12 | 12 | "time" |
13 | 13 | |
14 | 14 | "github.com/imdario/mergo" |
15 | "gopkg.in/yaml.v2" | |
15 | "gopkg.in/yaml.v3" | |
16 | 16 | ) |
17 | 17 | |
18 | 18 | type simpleTest struct { |
20 | 20 | } |
21 | 21 | |
22 | 22 | type complexTest struct { |
23 | ID string | |
23 | 24 | St simpleTest |
24 | 25 | sz int |
25 | ID string | |
26 | 26 | } |
27 | 27 | |
28 | 28 | type mapTest struct { |
49 | 49 | |
50 | 50 | func TestKb(t *testing.T) { |
51 | 51 | type testStruct struct { |
52 | KeyValue map[string]interface{} | |
52 | 53 | Name string |
53 | KeyValue map[string]interface{} | |
54 | 54 | } |
55 | 55 | |
56 | 56 | akv := make(map[string]interface{}) |
115 | 115 | func TestComplexStruct(t *testing.T) { |
116 | 116 | a := complexTest{} |
117 | 117 | a.ID = "athing" |
118 | b := complexTest{simpleTest{42}, 1, "bthing"} | |
118 | b := complexTest{"bthing", simpleTest{42}, 1} | |
119 | 119 | if err := mergo.Merge(&a, b); err != nil { |
120 | 120 | t.FailNow() |
121 | 121 | } |
131 | 131 | } |
132 | 132 | |
133 | 133 | 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} | |
138 | 138 | if err := mergo.MergeWithOverwrite(&a, b); err != nil { |
139 | 139 | t.FailNow() |
140 | 140 | } |
416 | 416 | Msg2 string |
417 | 417 | } |
418 | 418 | type params struct { |
419 | Name string | |
420 | 419 | Multi *multiPtr |
421 | 420 | Final *final |
421 | Name string | |
422 | 422 | } |
423 | 423 | type config struct { |
424 | Params *params | |
424 | 425 | Foo string |
425 | 426 | Bar string |
426 | Params *params | |
427 | 427 | } |
428 | 428 | |
429 | 429 | cases := []struct { |
430 | name string | |
431 | overwrite bool | |
432 | 430 | changes *config |
433 | 431 | target *config |
434 | 432 | output *config |
433 | name string | |
434 | overwrite bool | |
435 | 435 | }{ |
436 | 436 | { |
437 | 437 | name: "Should overwrite values in target for non-nil values in source", |
602 | 602 | func TestYAMLMaps(t *testing.T) { |
603 | 603 | thing := loadYAML("testdata/thing.yml") |
604 | 604 | 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{}) | |
607 | 607 | // license has one extra field (site) and another already existing in thing (author) that Mergo won't override. |
608 | 608 | expectedLength := len(ft) + len(fl) - 1 |
609 | 609 | if err := mergo.Merge(&license, thing); err != nil { |
610 | 610 | t.Error(err.Error()) |
611 | 611 | } |
612 | currentLength := len(license["fields"].(map[interface{}]interface{})) | |
612 | currentLength := len(license["fields"].(map[string]interface{})) | |
613 | 613 | if currentLength != expectedLength { |
614 | 614 | t.Errorf(`thing not merged in license properly, license must have %d elements instead of %d`, expectedLength, currentLength) |
615 | 615 | } |
616 | fields := license["fields"].(map[interface{}]interface{}) | |
616 | fields := license["fields"].(map[string]interface{}) | |
617 | 617 | if _, ok := fields["id"]; !ok { |
618 | 618 | t.Errorf(`thing not merged in license properly, license must have a new id field from thing`) |
619 | 619 | } |
720 | 720 | } |
721 | 721 | |
722 | 722 | type pointerMapTest struct { |
723 | B *simpleTest | |
723 | 724 | A int |
724 | 725 | hidden int |
725 | B *simpleTest | |
726 | 726 | } |
727 | 727 | |
728 | 728 | func TestBackAndForth(t *testing.T) { |
729 | pt := pointerMapTest{42, 1, &simpleTest{66}} | |
729 | pt := pointerMapTest{&simpleTest{66}, 42, 1} | |
730 | 730 | m := make(map[string]interface{}) |
731 | 731 | if err := mergo.Map(&m, pt); err != nil { |
732 | 732 | t.FailNow() |
762 | 762 | |
763 | 763 | func TestEmbeddedPointerUnpacking(t *testing.T) { |
764 | 764 | 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}}, | |
767 | 767 | } |
768 | 768 | newValue := 77 |
769 | 769 | m := map[string]interface{}{ |
899 | 899 | func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) { |
900 | 900 | testCases := []struct { |
901 | 901 | name string |
902 | err string | |
902 | 903 | options []func(*mergo.Config) |
903 | err string | |
904 | 904 | }{ |
905 | 905 | { |
906 | 906 | "With override and append slice", |
907 | "cannot append two slices with different type", | |
907 | 908 | []func(*mergo.Config){mergo.WithOverride, mergo.WithAppendSlice}, |
908 | "cannot append two slices with different type", | |
909 | 909 | }, |
910 | 910 | { |
911 | 911 | "With override and type check", |
912 | "cannot override two slices with different type", | |
912 | 913 | []func(*mergo.Config){mergo.WithOverride, mergo.WithTypeCheck}, |
913 | "cannot override two slices with different type", | |
914 | 914 | }, |
915 | 915 | } |
916 | 916 | for _, tc := range testCases { |
929 | 929 | } |
930 | 930 | } |
931 | 931 | |
932 | func TestMergeSlicesIsNotSupported(t *testing.T) { | |
932 | func TestMergeDifferentSlicesIsNotSupported(t *testing.T) { | |
933 | 933 | src := []string{"a", "b"} |
934 | 934 | dst := []int{1, 2} |
935 | 935 | |
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 { | |
937 | 937 | t.Errorf("expected %q, got %q", mergo.ErrNotSupported, err) |
938 | 938 | } |
939 | 939 | } |
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 | } |