Skip to content

Commit dcaa0c1

Browse files
authored
fix 枚举类型 (#13)
* fix 枚举类型 * add return
1 parent d565b28 commit dcaa0c1

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

deepcopy.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,40 @@ func (d *deepCopy) cpyDefault(dst, src reflect.Value, depth int) error {
308308
if dst.Kind() != src.Kind() {
309309
return nil
310310
}
311+
312+
switch src.Kind() {
313+
case
314+
reflect.Int,
315+
reflect.Int8,
316+
reflect.Int16,
317+
reflect.Int32,
318+
reflect.Int64:
319+
dst.SetInt(src.Int())
320+
return nil
321+
case
322+
reflect.Uint,
323+
reflect.Uint8,
324+
reflect.Uint16,
325+
reflect.Uint32,
326+
reflect.Uint64:
327+
dst.SetUint(src.Uint())
328+
return nil
329+
case
330+
reflect.String:
331+
dst.SetString(src.String())
332+
return nil
333+
case
334+
reflect.Bool:
335+
dst.SetBool(src.Bool())
336+
return nil
337+
case
338+
reflect.Float32,
339+
reflect.Float64:
340+
dst.SetFloat(src.Float())
341+
return nil
342+
}
343+
344+
// 如果这里是枚举类型(type newType oldType),底层的数据类型(oldType)一样,set也会报错, 所以在前面加个前置判断保护下
311345
dst.Set(src)
312346
return nil
313347
}

deepcopy_enum_fix2_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package deepcopy
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
type DiaryResourceTypeDst int32
10+
11+
const (
12+
// 文本
13+
DiaryResourceType_DiaryResourceTypeTextDst DiaryResourceTypeDst = 0
14+
// 图片
15+
DiaryResourceType_DiaryResourceTypeImageDst DiaryResourceTypeDst = 1
16+
// 视频
17+
DiaryResourceType_DiaryResourceTypeVideoDst DiaryResourceTypeDst = 2
18+
// Ours类型
19+
DiaryResourceType_DiaryResourceTypeOursDst DiaryResourceTypeDst = 3
20+
)
21+
22+
type SquareDiaryItemDst struct {
23+
// 资源类型
24+
ResourceType DiaryResourceTypeDst `protobuf:"varint,7,opt,name=ResourceType,proto3,enum=topic.v1.DiaryResourceType" json:"ResourceType,omitempty"`
25+
// 资源链接数组
26+
}
27+
28+
type GetRecommendedListResp_GetRecommendedListRespData struct {
29+
30+
// 列表
31+
List []*SquareDiaryItemDst `protobuf:"bytes,1,rep,name=List,proto3" json:"List,omitempty"`
32+
// 是否有更多记录
33+
HasMore bool `protobuf:"varint,2,opt,name=HasMore,proto3" json:"HasMore,omitempty"`
34+
// 记录位置
35+
Pos int32 `protobuf:"varint,3,opt,name=Pos,proto3" json:"Pos,omitempty"`
36+
// 相同记录的偏移量
37+
Offset int32 `protobuf:"varint,4,opt,name=Offset,proto3" json:"Offset,omitempty"`
38+
}
39+
40+
type DiaryResourceTypeSrc int32
41+
42+
const (
43+
// 文本
44+
DiaryResourceType_DiaryResourceTypeText DiaryResourceTypeSrc = 0
45+
// 图片
46+
DiaryResourceType_DiaryResourceTypeImage DiaryResourceTypeSrc = 1
47+
// 视频
48+
DiaryResourceType_DiaryResourceTypeVideo DiaryResourceTypeSrc = 2
49+
// Ours类型
50+
DiaryResourceType_DiaryResourceTypeOurs DiaryResourceTypeSrc = 3
51+
)
52+
53+
type SquareDiaryItemSrc struct {
54+
// 资源类型
55+
ResourceType DiaryResourceTypeSrc `protobuf:"varint,7,opt,name=ResourceType,proto3,enum=topic.v1.DiaryResourceType" json:"ResourceType,omitempty"`
56+
// 资源链接数组
57+
}
58+
59+
type GetRecommendedListResp struct {
60+
// 列表
61+
List []*SquareDiaryItemSrc `protobuf:"bytes,1,rep,name=List,proto3" json:"List,omitempty"`
62+
// 是否有更多记录
63+
HasMore bool `protobuf:"varint,2,opt,name=HasMore,proto3" json:"HasMore,omitempty"`
64+
// 记录位置
65+
Pos int32 `protobuf:"varint,3,opt,name=Pos,proto3" json:"Pos,omitempty"`
66+
// 相同记录的偏移量
67+
Offset int32 `protobuf:"varint,4,opt,name=Offset,proto3" json:"Offset,omitempty"`
68+
}
69+
70+
func Test_Fix2(t *testing.T) {
71+
72+
var r *GetRecommendedListResp_GetRecommendedListRespData
73+
resp := GetRecommendedListResp{
74+
// 帮我优化这个代码
75+
List: []*SquareDiaryItemSrc{&SquareDiaryItemSrc{ResourceType: DiaryResourceType_DiaryResourceTypeVideo}},
76+
HasMore: true,
77+
Pos: 10,
78+
Offset: 11,
79+
}
80+
err := Copy(&r, &resp).Do()
81+
assert.NoError(t, err)
82+
assert.NotEqual(t, r, nil)
83+
assert.NotEqual(t, len(r.List), 0)
84+
}

0 commit comments

Comments
 (0)