darccio/mergo

bool issue

mybigman opened this issue · 3 comments

type Foo struct {
	A string
	B int64
	C bool
	D string
}

src := Foo{
	A: "a",
	B: 52,
	C: false,
	D: "2012-02-02",
}

dest := Foo{
	A: "b",
	B: 12,
	C: true,
	D: "2012-02-03",
}

mergo.Merge(&dest, src, mergo.WithOverride)
fmt.Println(src)
fmt.Println(dest)
fmt.Printf("%+v", dest)

got

{a 52 false 2012-02-02}
{a 52 true 2012-02-02}
{A:a B:52 C:true D:2012-02-02}

expected

{a 52 false 2012-02-02}
{a 52 false 2012-02-02}
{A:a B:52 C:false D:2012-02-02}
type Foo struct {
	A string
	B int64
	C bool
	D string
}

src := Foo{
	A: "a",
	B: 52,
	C: true,
	D: "2012-02-02",
}

dest := Foo{
	A: "b",
	B: 12,
	C: false,
	D: "2012-02-03",
}

mergo.Merge(&dest, src, mergo.WithOverride)
fmt.Println(src)
fmt.Println(dest)
fmt.Printf("%+v", dest)

got

{a 52 true 2012-02-02}
{a 52 true 2012-02-02}
{A:a B:52 C:true D:2012-02-02}

expected

{a 52 true 2012-02-02}
{a 52 true 2012-02-02}
{A:a B:52 C:true D:2012-02-02}

As you can see overriding true with false fails, however overriding false with true works

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Stuck with the same issue. Fixed with mergo.WithoutDereference and pointer to bool in structure

You can try using mergo.WithOverwriteWithEmptyValue instead of mergo.WithOverride.

For anyone having encountering this bug from helm (or sprig), you can use mergeOverwrite or mustMergeOverwrite and reverse the list of merged maps to achieve the correct behavior.