You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.go 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package models
  2. import "gitlink.org.cn/cloudream/common/utils/serder"
  3. /// TODO 将分散在各处的公共结构体定义集中到这里来
  4. const (
  5. RedundancyRep = "rep"
  6. RedundancyEC = "ec"
  7. )
  8. // 冗余模式的描述信息。
  9. // 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。
  10. type RedundancyInfo interface{}
  11. type RedundancyInfoConst interface {
  12. RedundancyInfo | RepRedundancyInfo | ECRedundancyInfo
  13. }
  14. type RepRedundancyInfo struct {
  15. RepCount int `json:"repCount"`
  16. }
  17. func NewRepRedundancyInfo(repCount int) RepRedundancyInfo {
  18. return RepRedundancyInfo{
  19. RepCount: repCount,
  20. }
  21. }
  22. type ECRedundancyInfo struct {
  23. ECName string `json:"ecName"`
  24. }
  25. func NewECRedundancyInfo(ecName string) ECRedundancyInfo {
  26. return ECRedundancyInfo{
  27. ECName: ecName,
  28. }
  29. }
  30. type TypedRedundancyInfo struct {
  31. Type string `json:"type"`
  32. Info RedundancyInfo `json:"info"`
  33. }
  34. func NewTypedRedundancyInfo[T RedundancyInfoConst](typ string, info T) TypedRedundancyInfo {
  35. return TypedRedundancyInfo{
  36. Type: typ,
  37. Info: info,
  38. }
  39. }
  40. func NewTypedRepRedundancyInfo(repCount int) TypedRedundancyInfo {
  41. return TypedRedundancyInfo{
  42. Type: RedundancyRep,
  43. Info: RepRedundancyInfo{
  44. RepCount: repCount,
  45. },
  46. }
  47. }
  48. func (i *TypedRedundancyInfo) ToRepInfo() (RepRedundancyInfo, error) {
  49. var info RepRedundancyInfo
  50. err := serder.AnyToAny(i.Info, &info)
  51. return info, err
  52. }
  53. func (i *TypedRedundancyInfo) ToECInfo() (ECRedundancyInfo, error) {
  54. var info ECRedundancyInfo
  55. err := serder.AnyToAny(i.Info, &info)
  56. return info, err
  57. }

公共库