From 0f8b180a5e5d31efe6430d16a1489f4c29a0a183 Mon Sep 17 00:00:00 2001 From: stanipetrosyan Date: Wed, 21 Aug 2024 08:59:24 +0200 Subject: [PATCH] refactor: made private methods that will not be used outside --- channel.go | 10 +++++----- client.go | 2 +- client_test.go | 2 +- context.go | 2 +- eventbus.go | 4 +++- network_bus.go | 4 ++-- processor.go | 4 ++-- publisher.go | 2 +- server.go | 2 +- server_test.go | 2 +- subscriber.go | 4 ++-- 11 files changed, 20 insertions(+), 18 deletions(-) diff --git a/channel.go b/channel.go index 2218db7..6801acd 100644 --- a/channel.go +++ b/channel.go @@ -35,7 +35,7 @@ func (c *defaultChannel) Listen() { func (c *defaultChannel) Publisher() Publisher { slog.Info("Publisher created", slog.String("channel", c.address)) - return NewPublisher(c.ch) + return newPublisher(c.ch) } func (c *defaultChannel) Subscriber() Subscriber { @@ -44,19 +44,19 @@ func (c *defaultChannel) Subscriber() Subscriber { slog.Info("Subscriber created", slog.String("channel", c.address)) - return NewSubscriber(ch) + return newSubscriber(ch) } func (c *defaultChannel) Processor(predicate func(message Message) bool) Channel { - c.processor = NewProcessorWithPredicate(predicate) + c.processor = newProcessorWithPredicate(predicate) slog.Info("Processor created", slog.String("channel", c.address)) return c } -func NewChannel(address string) Channel { +func newChannel(address string) Channel { ch := make(chan Message) - channel := defaultChannel{address: address, ch: ch, chs: []chan Message{}, processor: NewProcessor()} + channel := defaultChannel{address: address, ch: ch, chs: []chan Message{}, processor: newProcessor()} go channel.Listen() return &channel diff --git a/client.go b/client.go index dd706d5..e776a5d 100644 --- a/client.go +++ b/client.go @@ -43,6 +43,6 @@ func (s *tcpClient) Connect() { } } -func NewClient(address string, eventbus EventBus) Client { +func newClient(address string, eventbus EventBus) Client { return &tcpClient{address: address, eventbus: eventbus} } diff --git a/client_test.go b/client_test.go index 65b1cf8..fc0e486 100644 --- a/client_test.go +++ b/client_test.go @@ -33,7 +33,7 @@ func TestClient(t *testing.T) { } }() - client := NewClient("localhost:8083", eventbus) + client := newClient("localhost:8083", eventbus) go client.Connect() wg.Wait() diff --git a/context.go b/context.go index 487808b..1e1a316 100644 --- a/context.go +++ b/context.go @@ -12,6 +12,6 @@ func (c defaultContext) Result() Message { return c.message } -func NewConsumerContextWithMessage(message Message) Context { +func newConsumerContextWithMessage(message Message) Context { return defaultContext{message: message} } diff --git a/eventbus.go b/eventbus.go index f7300e0..5cd922d 100644 --- a/eventbus.go +++ b/eventbus.go @@ -10,16 +10,18 @@ type defaultEventBus struct { channels map[string]Channel } +// Returns a Channel with that address. If it doesn't exist, it returns a new one func (e *defaultEventBus) Channel(address string) Channel { _, exists := e.channels[address] if !exists { - e.channels[address] = NewChannel(address) + e.channels[address] = newChannel(address) } slog.Info("Channel created", slog.String("name", address)) return e.channels[address] } +// Create a new eventbus with default parameters func NewEventBus() EventBus { return &defaultEventBus{ channels: map[string]Channel{}, diff --git a/network_bus.go b/network_bus.go index b284e95..914cf42 100644 --- a/network_bus.go +++ b/network_bus.go @@ -15,9 +15,9 @@ func NewNetworkBus(bus EventBus, address string) NetworkBus { } func (b defaultNetworkBus) Server() Server { - return NewServer(b.address) + return newServer(b.address) } func (b defaultNetworkBus) Client() Client { - return NewClient(b.address, b.localBus) + return newClient(b.address, b.localBus) } diff --git a/processor.go b/processor.go index d5b7ce7..39fb3c4 100644 --- a/processor.go +++ b/processor.go @@ -8,11 +8,11 @@ type defaultProcessor struct { predicate func(message Message) bool } -func NewProcessor() Processor { +func newProcessor() Processor { return defaultProcessor{func(message Message) bool { return true }} } -func NewProcessorWithPredicate(predicate func(message Message) bool) Processor { +func newProcessorWithPredicate(predicate func(message Message) bool) Processor { return defaultProcessor{predicate: predicate} } diff --git a/publisher.go b/publisher.go index 5d75fda..62c8e63 100644 --- a/publisher.go +++ b/publisher.go @@ -8,7 +8,7 @@ type defaultPublisher struct { ch chan Message } -func NewPublisher(ch chan Message) Publisher { +func newPublisher(ch chan Message) Publisher { return defaultPublisher{ch: ch} } diff --git a/server.go b/server.go index b8442c5..ce06af3 100644 --- a/server.go +++ b/server.go @@ -56,6 +56,6 @@ func (s *tcpServer) Publish(channel string, message Message) { s.Unlock() } -func NewServer(address string) Server { +func newServer(address string) Server { return &tcpServer{address: address, clients: []net.Conn{}} } diff --git a/server_test.go b/server_test.go index f08dda5..6a5a951 100644 --- a/server_test.go +++ b/server_test.go @@ -14,7 +14,7 @@ func TestServer(t *testing.T) { var wg sync.WaitGroup wg.Add(1) - server := NewServer("localhost:8082") + server := newServer("localhost:8082") go server.Listen() var conn net.Conn diff --git a/subscriber.go b/subscriber.go index 09c00d3..91f004a 100644 --- a/subscriber.go +++ b/subscriber.go @@ -16,11 +16,11 @@ func (s defaultSubscriber) Listen(consumer func(context Context)) { return } - consumer(NewConsumerContextWithMessage(data)) + consumer(newConsumerContextWithMessage(data)) } }() } -func NewSubscriber(ch chan Message) Subscriber { +func newSubscriber(ch chan Message) Subscriber { return defaultSubscriber{ch: ch} }