...

Source file src/github.com/imdario/mergo/pr211_test.go

Documentation: github.com/imdario/mergo

     1  package mergo_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/imdario/mergo"
     8  )
     9  
    10  func TestMergeWithTransformerZeroValue(t *testing.T) {
    11  	// This test specifically tests that a transformer can be used to
    12  	// prevent overwriting a zero value (in this case a bool). This would fail prior to #211
    13  	type fooWithBoolPtr struct {
    14  		b *bool
    15  	}
    16  	var Bool = func(b bool) *bool { return &b }
    17  	a := fooWithBoolPtr{b: Bool(false)}
    18  	b := fooWithBoolPtr{b: Bool(true)}
    19  
    20  	if err := mergo.Merge(&a, &b, mergo.WithTransformers(&transformer{
    21  		m: map[reflect.Type]func(dst, src reflect.Value) error{
    22  			reflect.TypeOf(Bool(false)): func(dst, src reflect.Value) error {
    23  				if dst.CanSet() && dst.IsNil() {
    24  					dst.Set(src)
    25  				}
    26  				return nil
    27  			},
    28  		},
    29  	})); err != nil {
    30  		t.Error(err)
    31  	}
    32  
    33  	if *a.b != false {
    34  		t.Errorf("b not merged in properly: a.b(%v) != expected(%v)", a.b, false)
    35  	}
    36  }
    37  

View as plain text