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

Map() map[string]interface{} -> struct, overwrite not working for zero values #84

Closed
requilence opened this issue Jun 15, 2018 · 2 comments

Comments

@requilence
Copy link
Contributor

package main

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

type Foo struct {
	A int
	B int
	C int
}

func main() {
	dst := Foo{A: 0, B: 1, C: 2}

	src := map[string]interface{}{
		"A": 3, "B": 4, "C": 0,
	}

	mergo.Map(&dst, src, mergo.WithOverride)
	fmt.Printf("%+v\n", dst)
}

Expected result:
{A:3 B:4 C:0}

Actual result:
{A:3 B:4 C:2}

Looks like Overwrite ignores Value if it is zero.
But it must overwrite struct field in case map have the corresponding key set. Regardless of it's value

@darccio
Copy link
Owner

darccio commented Jul 3, 2018

I'm aware I have to check this but it will take two weeks to be able to do it. Sorry for my slowness.

@vilipwong
Copy link

vilipwong commented May 12, 2020

Hello. i faced the same issue with string and boolean value, using mergo.WithOverwriteWithEmptyValue e.g:

p1 := map[string]interface{}{
	"A": 3, "B": "note", "C": true,
}
p2 := map[string]interface{}{
	"B": "", "C": false,
}
mergo.Merge(&p1, &p2, mergo.WithOverwriteWithEmptyValue)

Expected result:
{A:3 B:"" C:false}

Actual result:
{A:3 B:"note" C:true}

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

3 participants