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" import "sync"
type SyncFilter interface { type SyncFilter[MsgType any] interface {
Then(what SyncFilter) SyncFilter Then(what SyncFilter[MsgType]) SyncFilter[MsgType]
Call(msg GenericMessage) Call(msg MsgType)
} }
type SyncFilterImpl struct { type SyncFilterImpl[MsgType any] struct {
next SyncFilter next SyncFilter[MsgType]
} }
func (impl *SyncFilterImpl) Then(what SyncFilter) SyncFilter { func (impl *SyncFilterImpl[MsgType]) Then(what SyncFilter[MsgType]) SyncFilter[MsgType] {
impl.next = what impl.next = what
return what return what
} }
func (impl *SyncFilterImpl) CallNext(msg GenericMessage) { func (impl *SyncFilterImpl[MsgType]) CallNext(msg MsgType) {
if impl.next != nil { if impl.next != nil {
impl.next.Call(msg) impl.next.Call(msg)
} }
} }
type CollectFromChannel struct{ SyncFilterImpl } type CollectFromChannel struct{ SyncFilterImpl[GenericMessage] }
func (collect *CollectFromChannel) Call(msg GenericMessage) { func (collect *CollectFromChannel) Call(msg GenericMessage) {
collect.CallNext(msg) collect.CallNext(msg)

View File

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

View File

@ -10,13 +10,19 @@ import (
) )
type SendToTelegramSync struct { type SendToTelegramSync struct {
SyncFilterImpl SyncFilterImpl[GenericMessage]
sender Sender sender Sender
logger *log.Logger logger *log.Logger
tpl *template.Template 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) { func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
err := sendToTg.sender.Send(msg, sendToTg.tpl) err := sendToTg.sender.Send(msg, sendToTg.tpl)
if err != nil { if err != nil {
@ -28,7 +34,7 @@ func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
} }
type SendToMatterbridgeSync struct { type SendToMatterbridgeSync struct {
SyncFilterImpl SyncFilterImpl[GenericMessage]
s SatelNameGetter s SatelNameGetter
config AppConfig config AppConfig

View File

@ -22,7 +22,7 @@ func TestSyncTelegramSender_NoChatIdsWontSendAnything(t *testing.T) {
s := MockSatelNameGetter{"test_name"} s := MockSatelNameGetter{"test_name"}
sender := TgSender{&mockBot, s, log.New(io.Discard, "", 0), []int64{}} 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}}) tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
a.Equal(0, len(mockBot.messages)) a.Equal(0, len(mockBot.messages))
} }
@ -34,7 +34,7 @@ func TestSyncTelegramSender_SendsMessageToAllChatIds(t *testing.T) {
s := MockSatelNameGetter{"test_name"} s := MockSatelNameGetter{"test_name"}
sender := TgSender{&mockBot, s, log.New(os.Stderr, "", 0), []int64{1, 2, 3}} 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}}) tested.Call(GenericMessage{[]satel.BasicEventElement{tgSenderMessageTest1}})
a.Equal(3, len(mockBot.messages)) a.Equal(3, len(mockBot.messages))
} }