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

fixes issue #187 #253

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions issue187_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package mergo_test

import (
"dario.cat/mergo"
"testing"
)

func TestIssue187MergeStructToMap(t *testing.T) {
dst := map[string]interface{}{
"empty": "data",
}

src := struct {
Foo string
Bar int
Empty string
}{
Foo: "hello",
Bar: 42,
}
if err := mergo.Map(&dst, src); err != nil {
t.Error(err)
}
if dst["foo"] != "hello" || dst["bar"] != 42 || dst["empty"] != "data" {
t.Errorf("expected dst to be {foo: hello, bar: 42, empty: data}, got {foo: %v, bar: %v, empty: %v}", dst["foo"], dst["bar"], dst["empty"])
}
}

func TestIssue187MergeStructToMapWithOverwrite(t *testing.T) {
dst := map[string]interface{}{
"foo": "initial",
"bar": 1,
"empty": "data",
}
src := struct {
Foo string
Bar int
Empty string
}{
Foo: "hello",
Bar: 42,
}
if err := mergo.Map(&dst, src, mergo.WithOverride); err != nil {
t.Error(err)
}
if dst["foo"] != "hello" || dst["bar"] != 42 || dst["empty"] != "data" {
t.Errorf("expected dst to be {foo: hello, bar: 42, empty: data}, got {foo: %v, bar: %v, empty: %v}", dst["foo"], dst["bar"], dst["empty"])
}
}

func TestIssue187MergeStructToMapWithOverwriteWithEmptyValue(t *testing.T) {
dst := map[string]interface{}{
"foo": "initial",
"bar": 1,
"empty": "data",
}
src := struct {
Foo string
Bar int
Empty string
}{
Foo: "hello",
Bar: 42,
}
if err := mergo.Map(&dst, src, mergo.WithOverwriteWithEmptyValue); err != nil {
t.Error(err)
}
if dst["foo"] != "hello" || dst["bar"] != 42 || dst["empty"] != "" {
t.Errorf("expected dst to be {foo: hello, bar: 42, empty: }, got {foo: %v, bar: %v, empty: %v}", dst["foo"], dst["bar"], dst["empty"])
}
}
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, conf
}
fieldName := field.Name
fieldName = changeInitialCase(fieldName, unicode.ToLower)
if v, ok := dstMap[fieldName]; !ok || (isEmptyValue(reflect.ValueOf(v), !config.ShouldNotDereference) || overwrite) {
if _, ok := dstMap[fieldName]; !ok || (!isEmptyValue(reflect.ValueOf(src.Field(i).Interface()), !config.ShouldNotDereference) && overwrite) || config.overwriteWithEmptyValue {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this change would break our use case, consider the following scenario

	dst := map[string]interface{}{
		"foo":   nil,
		"bar":   1,
	}
	src := struct {
		Foo   *string
		Bar   int
	}{
		Foo: &"hello",
		Bar: 42,
	}
	if err := mergo.Map(&dst, src); err != nil {
		t.Error(err)
	}

we expect *dst["foo"] == "hello" and dst["bar"] == 1

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frankdvd Your use case seems to be an unintended consequence. Mergo shouldn't flatten structures while mapping.

dstMap[fieldName] = src.Field(i).Interface()
}
}
Expand Down
Loading