...

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

Documentation: github.com/imdario/mergo

     1  package mergo_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/imdario/mergo"
     7  )
     8  
     9  type DstStructIssue84 struct {
    10  	A int
    11  	B int
    12  	C int
    13  }
    14  
    15  type DstNestedStructIssue84 struct {
    16  	A struct {
    17  		A int
    18  		B int
    19  		C int
    20  	}
    21  	B int
    22  	C int
    23  }
    24  
    25  func TestIssue84MergeMapWithNilValueToStructWithOverride(t *testing.T) {
    26  	p1 := DstStructIssue84{
    27  		A: 0, B: 1, C: 2,
    28  	}
    29  	p2 := map[string]interface{}{
    30  		"A": 3, "B": 4, "C": 0,
    31  	}
    32  
    33  	if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil {
    34  		t.Errorf("Error during the merge: %v", err)
    35  	}
    36  
    37  	if p1.C != 0 {
    38  		t.Error("C field should become '0'")
    39  	}
    40  }
    41  
    42  func TestIssue84MergeMapWithoutKeyExistsToStructWithOverride(t *testing.T) {
    43  	p1 := DstStructIssue84{
    44  		A: 0, B: 1, C: 2,
    45  	}
    46  	p2 := map[string]interface{}{
    47  		"A": 3, "B": 4,
    48  	}
    49  
    50  	if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil {
    51  		t.Errorf("Error during the merge: %v", err)
    52  	}
    53  
    54  	if p1.C != 2 {
    55  		t.Error("C field should be '2'")
    56  	}
    57  }
    58  
    59  func TestIssue84MergeNestedMapWithNilValueToStructWithOverride(t *testing.T) {
    60  	p1 := DstNestedStructIssue84{
    61  		A: struct {
    62  			A int
    63  			B int
    64  			C int
    65  		}{A: 1, B: 2, C: 0},
    66  		B: 0,
    67  		C: 2,
    68  	}
    69  	p2 := map[string]interface{}{
    70  		"A": map[string]interface{}{
    71  			"A": 0, "B": 0, "C": 5,
    72  		}, "B": 4, "C": 0,
    73  	}
    74  
    75  	if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil {
    76  		t.Errorf("Error during the merge: %v", err)
    77  	}
    78  
    79  	if p1.B != 4 {
    80  		t.Error("A.C field should become '4'")
    81  	}
    82  
    83  	if p1.A.C != 5 {
    84  		t.Error("A.C field should become '5'")
    85  	}
    86  
    87  	if p1.A.B != 0 || p1.A.A != 0 {
    88  		t.Error("A.A and A.B field should become '0'")
    89  	}
    90  }
    91  

View as plain text