Browse Source

优化公共类型

pull/3/head
Sydonian 2 years ago
parent
commit
888cd7a203
3 changed files with 51 additions and 15 deletions
  1. +7
    -12
      api/storage/object.go
  2. +2
    -2
      api/storage/storage_test.go
  3. +42
    -1
      models/models.go

+ 7
- 12
api/storage/object.go View File

@@ -7,6 +7,7 @@ import (
"strings" "strings"


"gitlink.org.cn/cloudream/common/consts/errorcode" "gitlink.org.cn/cloudream/common/consts/errorcode"
"gitlink.org.cn/cloudream/common/models"
myhttp "gitlink.org.cn/cloudream/common/utils/http" myhttp "gitlink.org.cn/cloudream/common/utils/http"
"gitlink.org.cn/cloudream/common/utils/serder" "gitlink.org.cn/cloudream/common/utils/serder"
) )
@@ -48,19 +49,13 @@ func (c *Client) ObjectDownload(req ObjectDownloadReq) (io.ReadCloser, error) {
} }


type ObjectUploadReq struct { type ObjectUploadReq struct {
UserID int64 `json:"userID"`
BucketID int64 `json:"bucketID"`
FileSize int64 `json:"fileSize"`
ObjectName string `json:"objectName"`
Redundancy MyRedundancyInfo `json:"redundancy"`
File io.Reader `json:"-"`
UserID int64 `json:"userID"`
BucketID int64 `json:"bucketID"`
FileSize int64 `json:"fileSize"`
ObjectName string `json:"objectName"`
Redundancy models.TypedRedundancyInfo `json:"redundancy"`
File io.Reader `json:"-"`
} }

type MyRedundancyInfo struct {
Type string `json:"type"`
Info any `json:"config"`
}

type ObjectUploadResp struct { type ObjectUploadResp struct {
ObjectID int64 `json:"objectID,string"` ObjectID int64 `json:"objectID,string"`
} }


+ 2
- 2
api/storage/storage_test.go View File

@@ -24,7 +24,7 @@ func Test_Object(t *testing.T) {
BucketID: 1, BucketID: 1,
FileSize: 4096, FileSize: 4096,
ObjectName: uuid.NewString(), ObjectName: uuid.NewString(),
Redundancy: MyRedundancyInfo{
Redundancy: models.TypedRedundancyInfo{
Type: models.RedundancyRep, Type: models.RedundancyRep,
Info: models.NewRepRedundancyInfo(1), Info: models.NewRepRedundancyInfo(1),
}, },
@@ -65,7 +65,7 @@ func Test_Storage(t *testing.T) {
BucketID: 1, BucketID: 1,
FileSize: 4096, FileSize: 4096,
ObjectName: uuid.NewString(), ObjectName: uuid.NewString(),
Redundancy: MyRedundancyInfo{
Redundancy: models.TypedRedundancyInfo{
Type: models.RedundancyRep, Type: models.RedundancyRep,
Info: models.NewRepRedundancyInfo(1), Info: models.NewRepRedundancyInfo(1),
}, },


+ 42
- 1
models/models.go View File

@@ -1,5 +1,7 @@
package models package models


import "gitlink.org.cn/cloudream/common/utils/serder"

/// TODO 将分散在各处的公共结构体定义集中到这里来 /// TODO 将分散在各处的公共结构体定义集中到这里来


const ( const (
@@ -11,7 +13,7 @@ const (
// 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。 // 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。
type RedundancyInfo interface{} type RedundancyInfo interface{}
type RedundancyInfoConst interface { type RedundancyInfoConst interface {
RepRedundancyInfo | ECRedundancyInfo | RedundancyInfo
RedundancyInfo | RepRedundancyInfo | ECRedundancyInfo
} }
type RepRedundancyInfo struct { type RepRedundancyInfo struct {
RepCount int `json:"repCount"` RepCount int `json:"repCount"`
@@ -24,4 +26,43 @@ func NewRepRedundancyInfo(repCount int) RepRedundancyInfo {
} }


type ECRedundancyInfo struct { type ECRedundancyInfo struct {
ECName string `json:"ecName"`
}

func NewECRedundancyInfo(ecName string) ECRedundancyInfo {
return ECRedundancyInfo{
ECName: ecName,
}
}

type TypedRedundancyInfo struct {
Type string `json:"type"`
Info RedundancyInfo `json:"info"`
}

func NewTypedRedundancyInfo[T RedundancyInfoConst](typ string, info T) TypedRedundancyInfo {
return TypedRedundancyInfo{
Type: typ,
Info: info,
}
}
func NewTypedRepRedundancyInfo(repCount int) TypedRedundancyInfo {
return TypedRedundancyInfo{
Type: RedundancyRep,
Info: RepRedundancyInfo{
RepCount: repCount,
},
}
}

func (i *TypedRedundancyInfo) ToRepInfo() (RepRedundancyInfo, error) {
var info RepRedundancyInfo
err := serder.AnyToAny(i.Info, &info)
return info, err
}

func (i *TypedRedundancyInfo) ToECInfo() (ECRedundancyInfo, error) {
var info ECRedundancyInfo
err := serder.AnyToAny(i.Info, &info)
return info, err
} }

Loading…
Cancel
Save