Skip to content

Commit

Permalink
fix 枚举类型 (#13)
Browse files Browse the repository at this point in the history
* fix 枚举类型

* add return
  • Loading branch information
guonaihong authored Mar 30, 2023
1 parent d565b28 commit dcaa0c1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
34 changes: 34 additions & 0 deletions deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,40 @@ func (d *deepCopy) cpyDefault(dst, src reflect.Value, depth int) error {
if dst.Kind() != src.Kind() {
return nil
}

switch src.Kind() {
case
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
dst.SetInt(src.Int())
return nil
case
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
dst.SetUint(src.Uint())
return nil
case
reflect.String:
dst.SetString(src.String())
return nil
case
reflect.Bool:
dst.SetBool(src.Bool())
return nil
case
reflect.Float32,
reflect.Float64:
dst.SetFloat(src.Float())
return nil
}

// 如果这里是枚举类型(type newType oldType),底层的数据类型(oldType)一样,set也会报错, 所以在前面加个前置判断保护下
dst.Set(src)
return nil
}
Expand Down
84 changes: 84 additions & 0 deletions deepcopy_enum_fix2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package deepcopy

import (
"testing"

"github.com/stretchr/testify/assert"
)

type DiaryResourceTypeDst int32

const (
// 文本
DiaryResourceType_DiaryResourceTypeTextDst DiaryResourceTypeDst = 0
// 图片
DiaryResourceType_DiaryResourceTypeImageDst DiaryResourceTypeDst = 1
// 视频
DiaryResourceType_DiaryResourceTypeVideoDst DiaryResourceTypeDst = 2
// Ours类型
DiaryResourceType_DiaryResourceTypeOursDst DiaryResourceTypeDst = 3
)

type SquareDiaryItemDst struct {
// 资源类型
ResourceType DiaryResourceTypeDst `protobuf:"varint,7,opt,name=ResourceType,proto3,enum=topic.v1.DiaryResourceType" json:"ResourceType,omitempty"`
// 资源链接数组
}

type GetRecommendedListResp_GetRecommendedListRespData struct {

// 列表
List []*SquareDiaryItemDst `protobuf:"bytes,1,rep,name=List,proto3" json:"List,omitempty"`
// 是否有更多记录
HasMore bool `protobuf:"varint,2,opt,name=HasMore,proto3" json:"HasMore,omitempty"`
// 记录位置
Pos int32 `protobuf:"varint,3,opt,name=Pos,proto3" json:"Pos,omitempty"`
// 相同记录的偏移量
Offset int32 `protobuf:"varint,4,opt,name=Offset,proto3" json:"Offset,omitempty"`
}

type DiaryResourceTypeSrc int32

const (
// 文本
DiaryResourceType_DiaryResourceTypeText DiaryResourceTypeSrc = 0
// 图片
DiaryResourceType_DiaryResourceTypeImage DiaryResourceTypeSrc = 1
// 视频
DiaryResourceType_DiaryResourceTypeVideo DiaryResourceTypeSrc = 2
// Ours类型
DiaryResourceType_DiaryResourceTypeOurs DiaryResourceTypeSrc = 3
)

type SquareDiaryItemSrc struct {
// 资源类型
ResourceType DiaryResourceTypeSrc `protobuf:"varint,7,opt,name=ResourceType,proto3,enum=topic.v1.DiaryResourceType" json:"ResourceType,omitempty"`
// 资源链接数组
}

type GetRecommendedListResp struct {
// 列表
List []*SquareDiaryItemSrc `protobuf:"bytes,1,rep,name=List,proto3" json:"List,omitempty"`
// 是否有更多记录
HasMore bool `protobuf:"varint,2,opt,name=HasMore,proto3" json:"HasMore,omitempty"`
// 记录位置
Pos int32 `protobuf:"varint,3,opt,name=Pos,proto3" json:"Pos,omitempty"`
// 相同记录的偏移量
Offset int32 `protobuf:"varint,4,opt,name=Offset,proto3" json:"Offset,omitempty"`
}

func Test_Fix2(t *testing.T) {

var r *GetRecommendedListResp_GetRecommendedListRespData
resp := GetRecommendedListResp{
// 帮我优化这个代码
List: []*SquareDiaryItemSrc{&SquareDiaryItemSrc{ResourceType: DiaryResourceType_DiaryResourceTypeVideo}},
HasMore: true,
Pos: 10,
Offset: 11,
}
err := Copy(&r, &resp).Do()
assert.NoError(t, err)
assert.NotEqual(t, r, nil)
assert.NotEqual(t, len(r.List), 0)
}

0 comments on commit dcaa0c1

Please sign in to comment.