diff --git a/filters_sync.go b/filters_sync.go index cc3c51a..6b709a2 100644 --- a/filters_sync.go +++ b/filters_sync.go @@ -2,27 +2,27 @@ package main import "sync" -type SyncFilter interface { - Then(what SyncFilter) SyncFilter - Call(msg GenericMessage) +type SyncFilter[MsgType any] interface { + Then(what SyncFilter[MsgType]) SyncFilter[MsgType] + Call(msg MsgType) } -type SyncFilterImpl struct { - next SyncFilter +type SyncFilterImpl[MsgType any] struct { + next SyncFilter[MsgType] } -func (impl *SyncFilterImpl) Then(what SyncFilter) SyncFilter { +func (impl *SyncFilterImpl[MsgType]) Then(what SyncFilter[MsgType]) SyncFilter[MsgType] { impl.next = what return what } -func (impl *SyncFilterImpl) CallNext(msg GenericMessage) { +func (impl *SyncFilterImpl[MsgType]) CallNext(msg MsgType) { if impl.next != nil { impl.next.Call(msg) } } -type CollectFromChannel struct{ SyncFilterImpl } +type CollectFromChannel struct{ SyncFilterImpl[GenericMessage] } func (collect *CollectFromChannel) Call(msg GenericMessage) { collect.CallNext(msg) diff --git a/filters_test.go b/filters_test.go index 4c1c57e..1ba83d4 100644 --- a/filters_test.go +++ b/filters_test.go @@ -332,7 +332,7 @@ func TestThrottle_ManyMessagesInOneEvent(t *testing.T) { } type SyncMockFilter struct { - SyncFilterImpl + SyncFilterImpl[GenericMessage] collected []GenericMessage } diff --git a/sender_sync.go b/sender_sync.go index a6d9984..a61d5eb 100644 --- a/sender_sync.go +++ b/sender_sync.go @@ -10,13 +10,19 @@ import ( ) type SendToTelegramSync struct { - SyncFilterImpl + SyncFilterImpl[GenericMessage] sender Sender logger *log.Logger tpl *template.Template } +func MakeSendToTelegramSync(sender Sender, + logger *log.Logger, + tpl *template.Template) SendToTelegramSync { + return SendToTelegramSync{SyncFilterImpl[GenericMessage]{}, sender, logger, tpl} +} + func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) { err := sendToTg.sender.Send(msg, sendToTg.tpl) if err != nil { @@ -28,7 +34,7 @@ func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) { } type SendToMatterbridgeSync struct { - SyncFilterImpl + SyncFilterImpl[GenericMessage] s SatelNameGetter config AppConfig diff --git a/sender_sync_test.go b/sender_sync_test.go index 20e841c..0eb3200 100644 --- a/sender_sync_test.go +++ b/sender_sync_test.go @@ -22,7 +22,7 @@ func TestSyncTelegramSender_NoChatIdsWontSendAnything(t *testing.T) { s := MockSatelNameGetter{"test_name"} sender := TgSender{&mockBot, s, log.New(io.Discard, "", 0), []int64{}} - tested := SendToTelegramSync{SyncFilterImpl{}, &sender, log.New(io.Discard, "", 0), tpl} + tested := MakeSendToTelegramSync(&sender, log.New(io.Discard, "", 0), tpl) tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}) a.Equal(0, len(mockBot.messages)) } @@ -34,7 +34,7 @@ func TestSyncTelegramSender_SendsMessageToAllChatIds(t *testing.T) { s := MockSatelNameGetter{"test_name"} sender := TgSender{&mockBot, s, log.New(os.Stderr, "", 0), []int64{1, 2, 3}} - tested := SendToTelegramSync{SyncFilterImpl{}, &sender, log.New(os.Stderr, "", 0), tpl} + tested := MakeSendToTelegramSync(&sender, log.New(os.Stderr, "", 0), tpl) tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}}) a.Equal(3, len(mockBot.messages)) }