SyncFilter is now generic
This commit is contained in:
parent
5f93bc084e
commit
76bd8e42d4
|
@ -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)
|
||||||
|
|
|
@ -332,7 +332,7 @@ func TestThrottle_ManyMessagesInOneEvent(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncMockFilter struct {
|
type SyncMockFilter struct {
|
||||||
SyncFilterImpl
|
SyncFilterImpl[GenericMessage]
|
||||||
collected []GenericMessage
|
collected []GenericMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue