Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: mergo fails merge properly when it comes to map of struct #146

Closed
iamhopaul123 opened this issue May 27, 2020 · 1 comment
Closed

Comments

@iamhopaul123
Copy link

package main

import (
	"fmt"
	"github.com/imdario/mergo"
)

type Foo struct {
	A string
	B map[string]Bar
}

type Bar struct {
	C *string
	D *string
}

func main() {
	s1 := "asd"
	s2 := "sdf"
	src := Foo{
		A: "one",
		B: map[string]Bar{
			"foo": {
				D: &s2,
			},
		},
	}
	dest := Foo{
		A: "two",
		B: map[string]Bar{
			"foo": {
				C: &s1,
			},
		},
	}
	mergo.Merge(&dest, src, mergo.WithOverride, mergo.WithOverwriteWithEmptyValue)
	fmt.Println(dest.B["foo"])
}

Ideally it should output something like &{0xc000010250 0xc000010260}. However, the real output is {0xc000010250 <nil>}, which means that D: &s2 is ignored and not merged to dest.

The only work around is to make it pointer of struct like the following:

package main

import (
	"fmt"
	"github.com/imdario/mergo"
)

type Foo struct {
	A string
	B map[string]*Bar
}

type Bar struct {
	C *string
	D *string
}

func main() {
	s1 := "asd"
	s2 := "sdf"
	src := Foo{
		A: "one",
		B: map[string]*Bar{
			"foo": {
				D: &s2,
			},
		},
	}
	dest := Foo{
		A: "two",
		B: map[string]*Bar{
			"foo": {
				C: &s1,
			},
		},
	}
	mergo.Merge(&dest, src, mergo.WithOverride, mergo.WithOverwriteWithEmptyValue)
	fmt.Println(dest.B["foo"])
}

And the output is &{0xc000010250 0xc000010260} which is expected.

@darccio
Copy link
Owner

darccio commented Jul 17, 2020

Resolved by reverting PR #105

@darccio darccio closed this as completed Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants