-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from vsemichev/master
fixes issue #187
- Loading branch information
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters