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 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package models
  2. import (
  3. "fmt"
  4. myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
  5. "gitlink.org.cn/cloudream/common/utils/serder"
  6. )
  7. /// TODO 将分散在各处的公共结构体定义集中到这里来
  8. const (
  9. RedundancyRep = "rep"
  10. RedundancyEC = "ec"
  11. )
  12. // 冗余模式的描述信息。
  13. // 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。
  14. type RedundancyInfo interface{}
  15. type RedundancyInfoConst interface {
  16. RepRedundancyInfo | ECRedundancyInfo
  17. }
  18. type RepRedundancyInfo struct {
  19. RepCount int `json:"repCount"`
  20. }
  21. func NewRepRedundancyInfo(repCount int) RepRedundancyInfo {
  22. return RepRedundancyInfo{
  23. RepCount: repCount,
  24. }
  25. }
  26. type ECRedundancyInfo struct {
  27. ECName string `json:"ecName"`
  28. PacketSize int64 `json:"packetSize"`
  29. }
  30. func NewECRedundancyInfo(ecName string, packetSize int64) ECRedundancyInfo {
  31. return ECRedundancyInfo{
  32. ECName: ecName,
  33. PacketSize: packetSize,
  34. }
  35. }
  36. type TypedRedundancyInfo struct {
  37. Type string `json:"type"`
  38. Info RedundancyInfo `json:"info"`
  39. }
  40. func NewTypedRedundancyInfo[T RedundancyInfoConst](info T) TypedRedundancyInfo {
  41. var typ string
  42. if myreflect.TypeOf[T]() == myreflect.TypeOf[RepRedundancyInfo]() {
  43. typ = RedundancyRep
  44. } else if myreflect.TypeOf[T]() == myreflect.TypeOf[ECRedundancyInfo]() {
  45. typ = RedundancyEC
  46. }
  47. return TypedRedundancyInfo{
  48. Type: typ,
  49. Info: info,
  50. }
  51. }
  52. func NewTypedRepRedundancyInfo(repCount int) TypedRedundancyInfo {
  53. return TypedRedundancyInfo{
  54. Type: RedundancyRep,
  55. Info: RepRedundancyInfo{
  56. RepCount: repCount,
  57. },
  58. }
  59. }
  60. func NewTypedECRedundancyInfo(ecName string, packetSize int64) TypedRedundancyInfo {
  61. return TypedRedundancyInfo{
  62. Type: RedundancyRep,
  63. Info: ECRedundancyInfo{
  64. ECName: ecName,
  65. PacketSize: packetSize,
  66. },
  67. }
  68. }
  69. func (i *TypedRedundancyInfo) IsRepInfo() bool {
  70. return i.Type == RedundancyRep
  71. }
  72. func (i *TypedRedundancyInfo) IsECInfo() bool {
  73. return i.Type == RedundancyEC
  74. }
  75. func (i *TypedRedundancyInfo) ToRepInfo() (RepRedundancyInfo, error) {
  76. var info RepRedundancyInfo
  77. err := serder.AnyToAny(i.Info, &info)
  78. return info, err
  79. }
  80. func (i *TypedRedundancyInfo) ToECInfo() (ECRedundancyInfo, error) {
  81. var info ECRedundancyInfo
  82. err := serder.AnyToAny(i.Info, &info)
  83. return info, err
  84. }
  85. func (i *TypedRedundancyInfo) Scan(src interface{}) error {
  86. data, ok := src.([]uint8)
  87. if !ok {
  88. return fmt.Errorf("unknow src type: %v", myreflect.TypeOfValue(data))
  89. }
  90. return serder.JSONToObject(data, i)
  91. }