From 00cc11214d3add183713461a2efda1db69dc2874 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Thu, 9 Nov 2023 17:23:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9C=A8=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BB=8EJSON=E8=BD=AC=E4=B8=BAObject=E7=9A=84=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=E5=A4=84=E7=90=86TypeUnion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/pkgs/mq/scanner/event/agent_check_cache.go | 6 ++++-- common/pkgs/mq/scanner/event/agent_check_state.go | 6 ++++-- common/pkgs/mq/scanner/event/agent_check_storage.go | 6 ++++-- common/pkgs/mq/scanner/event/check_cache.go | 6 ++++-- common/pkgs/mq/scanner/event/check_package.go | 6 ++++-- common/pkgs/mq/scanner/event/check_rep_count.go | 6 ++++-- common/pkgs/mq/scanner/event/event.go | 11 +++-------- 7 files changed, 27 insertions(+), 20 deletions(-) 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) -}