diff --git a/common/pkgs/mq/scanner/event/agent_check_cache.go b/common/pkgs/mq/scanner/event/agent_check_cache.go index 08b241a..4cdaec2 100644 --- a/common/pkgs/mq/scanner/event/agent_check_cache.go +++ b/common/pkgs/mq/scanner/event/agent_check_cache.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/agent_check_state.go b/common/pkgs/mq/scanner/event/agent_check_state.go index 54b9741..91d34be 100644 --- a/common/pkgs/mq/scanner/event/agent_check_state.go +++ b/common/pkgs/mq/scanner/event/agent_check_state.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/agent_check_storage.go b/common/pkgs/mq/scanner/event/agent_check_storage.go index 3c971f2..aeaffa1 100644 --- a/common/pkgs/mq/scanner/event/agent_check_storage.go +++ b/common/pkgs/mq/scanner/event/agent_check_storage.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/check_cache.go b/common/pkgs/mq/scanner/event/check_cache.go index 3325a0b..d6f7633 100644 --- a/common/pkgs/mq/scanner/event/check_cache.go +++ b/common/pkgs/mq/scanner/event/check_cache.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/check_package.go b/common/pkgs/mq/scanner/event/check_package.go index 526dda0..50fb664 100644 --- a/common/pkgs/mq/scanner/event/check_package.go +++ b/common/pkgs/mq/scanner/event/check_package.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/check_rep_count.go b/common/pkgs/mq/scanner/event/check_rep_count.go index 39f2c74..3fe055b 100644 --- a/common/pkgs/mq/scanner/event/check_rep_count.go +++ b/common/pkgs/mq/scanner/event/check_rep_count.go @@ -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]() +} diff --git a/common/pkgs/mq/scanner/event/event.go b/common/pkgs/mq/scanner/event/event.go index 8fc6578..e1f1488 100644 --- a/common/pkgs/mq/scanner/event/event.go +++ b/common/pkgs/mq/scanner/event/event.go @@ -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) -}