1
0
Fork 0

SyncFilter is now generic

This commit is contained in:
Michał Rudowicz 2025-01-06 23:29:54 +01:00
parent 5f93bc084e
commit 76bd8e42d4
4 changed files with 19 additions and 13 deletions

View File

@ -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)

View File

@ -332,7 +332,7 @@ func TestThrottle_ManyMessagesInOneEvent(t *testing.T) {
}
type SyncMockFilter struct {
SyncFilterImpl
SyncFilterImpl[GenericMessage]
collected []GenericMessage
}

View File

@ -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

View File

@ -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))
}