-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for keeping zero values with transformer
- Loading branch information
Zaq? Wiedmann
committed
May 18, 2022
1 parent
ab6b270
commit 4bed36e
Showing
1 changed file
with
36 additions
and
0 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,36 @@ | ||
package mergo_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/imdario/mergo" | ||
) | ||
|
||
func TestMergeWithTransformerZeroValue(t *testing.T) { | ||
// This test specifically tests that a transformer can be used to | ||
// prevent overwriting a zero value (in this case a bool). This would fail prior to #211 | ||
type fooWithBoolPtr struct { | ||
b *bool | ||
} | ||
var Bool = func(b bool) *bool { return &b } | ||
a := fooWithBoolPtr{b: Bool(false)} | ||
b := fooWithBoolPtr{b: Bool(true)} | ||
|
||
if err := mergo.Merge(&a, &b, mergo.WithTransformers(&transformer{ | ||
m: map[reflect.Type]func(dst, src reflect.Value) error{ | ||
reflect.TypeOf(Bool(false)): func(dst, src reflect.Value) error { | ||
if dst.CanSet() && dst.IsNil() { | ||
dst.Set(src) | ||
} | ||
return nil | ||
}, | ||
}, | ||
})); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if *a.b != false { | ||
t.Errorf("b not merged in properly: a.b(%v) != expected(%v)", a.b, false) | ||
} | ||
} |