-
-
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.
Merge pull request #211 from zaquestion/transformer_valid_destination
fix: gate transformers on valid non-nil destinations
- Loading branch information
Showing
3 changed files
with
62 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
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,25 @@ | ||
package mergo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type transformer struct { | ||
} | ||
|
||
func (s *transformer) Transformer(t reflect.Type) func(dst, src reflect.Value) error { | ||
return nil | ||
} | ||
|
||
func Test_deepMergeTransformerInvalidDestination(t *testing.T) { | ||
foo := time.Time{} | ||
src := reflect.ValueOf(foo) | ||
deepMerge(reflect.Value{}, src, make(map[uintptr]*visit), 0, &Config{ | ||
Transformers: &transformer{}, | ||
}) | ||
// this test is intentionally not asserting on anything, it's sole | ||
// purpose to verify deepMerge doesn't panic when a transformer is | ||
// passed and the destination is invalid. | ||
} |
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) | ||
} | ||
} |