diff --git a/copier.go b/copier.go index 5fba6b8..637435c 100644 --- a/copier.go +++ b/copier.go @@ -34,7 +34,7 @@ const ( // Option sets copy options type Option struct { - // setting this value to true will ignore copying zero values of all the fields, including bools, as well as a + // setting this value to true will ignore copying zero values of all the fields, including booleans, as well as a // struct having all it's fields set to their zero values respectively (see IsZero() in reflect/value.go) IgnoreEmpty bool DeepCopy bool @@ -84,7 +84,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) converters map[converterPair]TypeConverter ) - // save convertes into map for faster lookup + // save converters into map for faster lookup for i := range opt.Converters { if converters == nil { converters = make(map[converterPair]TypeConverter) @@ -313,7 +313,7 @@ func copier(toValue interface{}, fromValue interface{}, opt Option) (err error) } } - // Copy from from method to dest field + // Copy from src method to dest field for _, field := range deepFields(toType) { name := field.Name srcFieldName, destFieldName := getFieldName(name, flgs) diff --git a/copier_test.go b/copier_test.go index f6d68a4..0bd99b2 100644 --- a/copier_test.go +++ b/copier_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/jinzhu/copier" + "github.com/jinzhu/copier/other" ) type User struct { @@ -1203,7 +1204,7 @@ func TestCopyMapOfInt(t *testing.T) { func TestCopyMapOfSliceValue(t *testing.T) { // case1: map's value is a simple slice key, value := 2, 3 - src := map[int][]int{key: []int{value} } + src := map[int][]int{key: []int{value}} dst1 := map[int][]int{} var dst2 map[int][]int err := copier.Copy(&dst1, src) @@ -1240,10 +1241,10 @@ func TestCopyMapOfSliceValue(t *testing.T) { // case2: map's value is a slice whose element is map key1, key2 := 2, 3 value = 4 - s := map[int][]map[int]int{key1: []map[int]int{ {key2: value} } } - d1 := map[int][]map[int]int{key1: []map[int]int{ {key1: key2 } } } - d2 := map[int][]map[int]int{key1: []map[int]int{ } } - d3 := map[int][]map[int]int{key1: nil } + s := map[int][]map[int]int{key1: []map[int]int{{key2: value}}} + d1 := map[int][]map[int]int{key1: []map[int]int{{key1: key2}}} + d2 := map[int][]map[int]int{key1: []map[int]int{}} + d3 := map[int][]map[int]int{key1: nil} d4 := map[int][]map[int]int{} d5 := map[int][]map[int]int(nil) ms := []map[int][]map[int]int{d1, d2, d3, d4, d5} @@ -1267,7 +1268,7 @@ func TestCopyMapOfSliceValue(t *testing.T) { for k, v := range m { if k != key2 || v != value { t.Errorf("Map's slice value should be copied recursively") - } + } } } } @@ -1276,7 +1277,7 @@ func TestCopyMapOfSliceValue(t *testing.T) { func TestCopyMapOfPtrValue(t *testing.T) { intV := 3 intv := intV - src := map[int]*int{2: &intv } + src := map[int]*int{2: &intv} dst1 := map[int]*int{} var dst2 map[int]*int err := copier.Copy(&dst1, src) @@ -1295,7 +1296,7 @@ func TestCopyMapOfPtrValue(t *testing.T) { } v3, ok := dst2[k] - if !ok || v3 == nil ||*v3 != *v1 || *v3 != intV { + if !ok || v3 == nil || *v3 != *v1 || *v3 != intV { t.Errorf("Map should be copied") } } @@ -1520,7 +1521,6 @@ func TestDeepCopyTime(t *testing.T) { } } - func TestNestedPrivateData(t *testing.T) { type hasPrivate struct { data int @@ -1558,7 +1558,6 @@ func TestNestedPrivateData(t *testing.T) { } } - func TestDeepMapCopyTime(t *testing.T) { t1 := time.Now() t2 := t1.Add(time.Second) @@ -1611,7 +1610,7 @@ func TestDeepCopySimpleTime(t *testing.T) { } } -type TimeWrapper struct{ +type TimeWrapper struct { time.Time } @@ -1627,3 +1626,39 @@ func TestDeepCopyAnonymousFieldTime(t *testing.T) { t.Errorf("to (%v) value should equal from (%v) value", to.Time, from.Time) } } + +type TestEnum int64 + +const ( + Hello TestEnum = 1 + Hey TestEnum = 2 +) + +type SecondLayer struct { + Test *TestEnum +} +type Foo struct { + Second *other.SecondLayer +} +type Bar struct { + Second *SecondLayer +} + +func TestAAA(t *testing.T) { + hello := other.Hello + others := "others" + foo := Foo{ + Second: &other.SecondLayer{ + Test: &hello, + Others: &others, + }, + } + bar := Bar{} + fmt.Println(copier.Copy(&bar, &foo)) + + if fmt.Sprint(*bar.Second.Test) != fmt.Sprint(*foo.Second.Test) { + t.Fatalf("expects: %v, got %v", *bar.Second.Test, *foo.Second.Test) + } + fmt.Printf("%#v \n", *bar.Second.Test) + fmt.Printf("%#v \n", *foo.Second.Test) +} diff --git a/other/other.go b/other/other.go new file mode 100644 index 0000000..ad3ea3a --- /dev/null +++ b/other/other.go @@ -0,0 +1,13 @@ +package other + +type TestEnum int64 + +const ( + Hello TestEnum = 1 + Hey TestEnum = 2 +) + +type SecondLayer struct { + Test *TestEnum + Others *string +}