Browse Source

支持在直接从JSON转为Object的过程中处理TypeUnion

gitlink
Sydonian 2 years ago
parent
commit
00cc11214d
7 changed files with 27 additions and 20 deletions
  1. +4
    -2
      common/pkgs/mq/scanner/event/agent_check_cache.go
  2. +4
    -2
      common/pkgs/mq/scanner/event/agent_check_state.go
  3. +4
    -2
      common/pkgs/mq/scanner/event/agent_check_storage.go
  4. +4
    -2
      common/pkgs/mq/scanner/event/check_cache.go
  5. +4
    -2
      common/pkgs/mq/scanner/event/check_package.go
  6. +4
    -2
      common/pkgs/mq/scanner/event/check_rep_count.go
  7. +3
    -8
      common/pkgs/mq/scanner/event/event.go

+ 4
- 2
common/pkgs/mq/scanner/event/agent_check_cache.go View File

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

var _ = Register[*AgentCheckCache]()

type AgentCheckCache struct {
EventBase
NodeID int64 `json:"nodeID"`
@@ -14,3 +12,7 @@ func NewAgentCheckCache(nodeID int64, fileHashes []string) *AgentCheckCache {
FileHashes: fileHashes,
}
}

func init() {
Register[*AgentCheckCache]()
}

+ 4
- 2
common/pkgs/mq/scanner/event/agent_check_state.go View File

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

var _ = Register[*AgentCheckState]()

type AgentCheckState struct {
EventBase
NodeID int64 `json:"nodeID"`
@@ -12,3 +10,7 @@ func NewAgentCheckState(nodeID int64) *AgentCheckState {
NodeID: nodeID,
}
}

func init() {
Register[*AgentCheckState]()
}

+ 4
- 2
common/pkgs/mq/scanner/event/agent_check_storage.go View File

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

var _ = Register[*AgentCheckStorage]()

type AgentCheckStorage struct {
EventBase
StorageID int64 `json:"storageID"`
@@ -14,3 +12,7 @@ func NewAgentCheckStorage(storageID int64, packageIDs []int64) *AgentCheckStorag
PackageIDs: packageIDs,
}
}

func init() {
Register[*AgentCheckStorage]()
}

+ 4
- 2
common/pkgs/mq/scanner/event/check_cache.go View File

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

var _ = Register[*CheckCache]()

type CheckCache struct {
EventBase
NodeID int64 `json:"nodeID"`
@@ -12,3 +10,7 @@ func NewCheckCache(nodeID int64) *CheckCache {
NodeID: nodeID,
}
}

func init() {
Register[*CheckCache]()
}

+ 4
- 2
common/pkgs/mq/scanner/event/check_package.go View File

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

var _ = Register[*CheckPackage]()

type CheckPackage struct {
EventBase
PackageIDs []int64 `json:"packageIDs"`
@@ -12,3 +10,7 @@ func NewCheckPackage(packageIDs []int64) *CheckPackage {
PackageIDs: packageIDs,
}
}

func init() {
Register[*CheckPackage]()
}

+ 4
- 2
common/pkgs/mq/scanner/event/check_rep_count.go View File

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

var _ = Register[*CheckRepCount]()

type CheckRepCount struct {
EventBase
FileHashes []string `json:"fileHashes"`
@@ -12,3 +10,7 @@ func NewCheckRepCount(fileHashes []string) *CheckRepCount {
FileHashes: fileHashes,
}
}

func init() {
Register[*CheckRepCount]()
}

+ 3
- 8
common/pkgs/mq/scanner/event/event.go View File

@@ -1,28 +1,23 @@
package event

import (
"gitlink.org.cn/cloudream/common/pkgs/mq"
"gitlink.org.cn/cloudream/common/pkgs/types"
myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
"gitlink.org.cn/cloudream/common/utils/serder"
)

type Event interface {
Noop()
}

var EventTypeUnino = types.NewTypeUnion[Event]()
var EventTypeUnino = serder.UseTypeUnionExternallyTagged(types.Ref(types.NewTypeUnion[Event]()))

type EventBase struct{}

func (e *EventBase) Noop() {}

// 注:此函数必须以var _ = Register[xxx]()的形式被调用,这样才能保证init中RegisterUnionType时
// TypeUnion不是空的。(因为包级变量初始化比init函数调用先进行)
// 只能在init函数中调用,因为包级变量初始化比init函数调用先进行
func Register[T Event]() any {
EventTypeUnino.Add(myreflect.TypeOf[T]())
return nil
}

func init() {
mq.RegisterUnionType(EventTypeUnino)
}

Loading…
Cancel
Save