darccio / mergo

Mergo: merging Go structs and maps since 2013

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mergo.WithOverride not overriding empty string while type is Stringptr

MatanTubul opened this issue · comments

I am new with mergo lib, i build the following POC, looks like
Foo.A = "" count like an empty field, not sure why. i expected that Foo.A ="" will override Foo.A="two". i will glad to get an advise how to overcome it.
thanks


import (
	"fmt"
	"github.com/imdario/mergo"
)
var truePtr = true
var falsePtr = false
var routerName = "router name"
type Lolo struct {
	V string
}
type Boo struct {
	C string
	Lolo Lolo
}
type Foo struct {
	A *string
	B int64
	Boo Boo
	N *string
}
func main() {
	s1 := ""
	s2 := "two"
	src := Foo{
		A:   &s1,
		Boo: Boo{
			C:    "dddd",
			Lolo: Lolo{
				V: "vvvv",
			},
		},
	}
	dest := Foo{
		A: &s2,
		B : 2,
		Boo: Boo{
			Lolo: Lolo{
				V: "qqqq",
			},
		},
		N: s2,
	}

	mergo.Merge(&dest, src, mergo.WithOverride)

	fmt.Println(dest)
	fmt.Println(*dest.A)
	// Will print
	// {two 2 {dddd {vvvv}} two}
	// expected
	// {"" 2 {dddd {vvvv}} ""}
}

@imdario it might be i miss something regarding how to use this lib?

Thanks for opening a new issue. The team has been notified and will review it as soon as possible.
For urgent issues and priority support, visit https://xscode.com/imdario/mergo

@MatanTubul This kind of behaviour can be achieved with mergo.WithoutDereference option.