-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to not overwrite empty fields in destination structs if the structs are not empty.
- Loading branch information
Showing
2 changed files
with
67 additions
and
9 deletions.
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,49 @@ | ||
package mergo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/imdario/mergo" | ||
) | ||
|
||
type parent struct { | ||
S string | ||
Child *child | ||
} | ||
|
||
type child struct { | ||
S string | ||
} | ||
|
||
func TestDoNotOverwriteEmptyValueWithinStruct(t *testing.T) { | ||
src := parent{ | ||
Child: &child{S: "src"}, | ||
S: "src", | ||
} | ||
dest := parent{ | ||
Child: &child{S: ""}, | ||
S: "dest", | ||
} | ||
if err := mergo.Merge(&dest, src, mergo.WithNoOverrideEmptyStructValues); err != nil { | ||
t.Error(err) | ||
} | ||
if dest.Child.S != "" { | ||
t.Errorf("dest.Child.S overwritten") | ||
} | ||
} | ||
|
||
func TestOverwriteEmptyStruct(t *testing.T) { | ||
src := parent{ | ||
Child: &child{S: "src"}, | ||
S: "src", | ||
} | ||
dest := parent{ | ||
S: "dest", | ||
} | ||
if err := mergo.Merge(&dest, src, mergo.WithNoOverrideEmptyStructValues); err != nil { | ||
t.Error(err) | ||
} | ||
if dest.Child.S != "src" { | ||
t.Errorf("dest.Child.S not overwritten") | ||
} | ||
} |
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