Skip to content

Commit d565b28

Browse files
authored
fix (#12)
1 parent 49eb0d1 commit d565b28

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

deepcopy.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func isArraySlice(v reflect.Value) bool {
8787
// 拷贝slice array
8888
func (d *deepCopy) cpySliceArray(dst, src reflect.Value, depth int) error {
8989

90+
// dst只能是slice和array类型
9091
if !isArraySlice(dst) {
9192
return nil
9293
}
@@ -96,17 +97,11 @@ func (d *deepCopy) cpySliceArray(dst, src reflect.Value, depth int) error {
9697
l = dst.Len()
9798
}
9899

100+
// 被拷贝dst类型是slice, 长度是0, src的长度有值
99101
if dst.Kind() == reflect.Slice && dst.Len() == 0 && src.Len() > 0 {
100-
elemType := src.Type().Elem()
101-
if dst.Type().Elem().Kind() != elemType.Kind() {
102-
return nil
103-
}
104102

105-
// MakeSlice的类型用reflect.SliceOf(src.Index(0).Type())
106-
// 而不用src.Type()的原因如下
107-
// src.Type()拿到的是类型可能是array和slice。
108-
// 实际需要的是元素T的slice类型, 使用src.Type(), 这是错误的。
109-
newDst := reflect.MakeSlice(reflect.SliceOf(elemType), l, l)
103+
dstElemType := dst.Type().Elem()
104+
newDst := reflect.MakeSlice(reflect.SliceOf(dstElemType), l, l)
110105
dst.Set(newDst)
111106
}
112107

@@ -293,7 +288,7 @@ func (d *deepCopy) cpyPtr(dst, src reflect.Value, depth int) error {
293288
if !dst.CanSet() {
294289
return nil
295290
}
296-
p := reflect.New(src.Type().Elem())
291+
p := reflect.New(dst.Type().Elem())
297292
dst.Set(p)
298293
}
299294

@@ -354,5 +349,4 @@ func (d *deepCopy) deepCopy(dst, src reflect.Value, depth int) error {
354349
return d.cpyDefault(dst, src, depth)
355350
}
356351

357-
return nil
358352
}

deepcopy_fix_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package deepcopy
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// src
11+
type TopicItemSrc struct {
12+
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
13+
Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"`
14+
CreateTime string `protobuf:"bytes,3,opt,name=CreateTime,proto3" json:"CreateTime,omitempty"`
15+
}
16+
17+
// src
18+
type AllTopicResp struct {
19+
TopicItem []*TopicItemSrc `protobuf:"bytes,1,rep,name=TopicItem,proto3" json:"TopicItem,omitempty"`
20+
Total int32 `protobuf:"varint,2,opt,name=Total,proto3" json:"Total,omitempty"` //总条数
21+
}
22+
23+
// desc
24+
type AllTopicResp_AllTopicRespData struct {
25+
TopicItem []*TopicItemDst `protobuf:"bytes,1,rep,name=TopicItem,proto3" json:"TopicItem,omitempty"`
26+
Total int32 `protobuf:"varint,2,opt,name=Total,proto3" json:"Total,omitempty"` //总条数
27+
}
28+
29+
type TopicItemDst struct {
30+
ID string `protobuf:"bytes,1,opt,name=ID,proto3" json:"ID,omitempty"`
31+
Name string `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"`
32+
CreateTime string `protobuf:"bytes,3,opt,name=CreateTime,proto3" json:"CreateTime,omitempty"`
33+
}
34+
35+
type AllTopicResp2 struct {
36+
Code int32 `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
37+
Message string `protobuf:"bytes,2,opt,name=Message,proto3" json:"Message,omitempty"`
38+
Data *AllTopicResp_AllTopicRespData `protobuf:"bytes,3,opt,name=Data,proto3" json:"Data,omitempty"`
39+
}
40+
41+
func TestCopy(t *testing.T) {
42+
resp := AllTopicResp{Total: 100, TopicItem: []*TopicItemSrc{
43+
{ID: "111", Name: "111"},
44+
{ID: "111", Name: "222"},
45+
}}
46+
47+
rsp := AllTopicResp2{Data: &AllTopicResp_AllTopicRespData{}}
48+
Copy(rsp.Data, resp).Do()
49+
50+
fmt.Printf("%#v\n", resp)
51+
fmt.Printf("%#v\n", rsp.Data)
52+
assert.NotNil(t, rsp.Data)
53+
assert.Equal(t, rsp.Data.Total, resp.Total)
54+
assert.Equal(t, len(rsp.Data.TopicItem), len(resp.TopicItem))
55+
assert.Greater(t, len(rsp.Data.TopicItem), 0)
56+
assert.Greater(t, len(resp.TopicItem), 0)
57+
for i, v := range resp.TopicItem {
58+
assert.Equal(t, v.ID, rsp.Data.TopicItem[i].ID)
59+
assert.Equal(t, v.Name, rsp.Data.TopicItem[i].Name)
60+
}
61+
}

0 commit comments

Comments
 (0)