Skip to content

Commit

Permalink
added method extract and options in message
Browse files Browse the repository at this point in the history
  • Loading branch information
stanipetrosyan committed May 24, 2024
1 parent 4538ea2 commit 623ca4c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
14 changes: 7 additions & 7 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestSubscriberHandler(t *testing.T) {
wg.Add(1)

eventBus.Channel("my-channel").Subscriber().Listen(func(context Context) {
assert.Equal(t, "Hi There", context.Result().Data)
assert.Equal(t, "Hi There", context.Result().Extract())
wg.Done()
})

Expand All @@ -28,12 +28,12 @@ func TestSubscriberHandler(t *testing.T) {
wg.Add(2)

eventBus.Channel("newaddress").Subscriber().Listen(func(context Context) {
assert.Equal(t, "Hi There", context.Result().Data)
assert.Equal(t, "Hi There", context.Result().Extract())
wg.Done()
})

eventBus.Channel("newaddress").Subscriber().Listen(func(context Context) {
assert.Equal(t, "Hi There", context.Result().Data)
assert.Equal(t, "Hi There", context.Result().Extract())
wg.Done()
})

Expand All @@ -51,13 +51,13 @@ func TestProcessorHandler(t *testing.T) {
wg.Add(2)

eventBus.Channel("my-channel").Subscriber().Listen(func(context Context) {
assert.Equal(t, "Hi There", context.Result().Data)
assert.Equal(t, "Hi There", context.Result().Extract())
wg.Done()
})

eventBus.Channel("my-channel").Processor(func(message Message) bool {
wg.Done()
return message.Data == "Hi There"
return message.Extract() == "Hi There"
})

message := CreateMessage().SetBody("Hi There")
Expand All @@ -72,8 +72,8 @@ func TestMessageOptions(t *testing.T) {

wg.Add(1)
eventBus.Channel("address").Subscriber().Listen(func(context Context) {
assert.True(t, context.Result().Options.Headers().Contains("key"))
assert.Equal(t, "value", context.Result().Options.Headers().Header("key"))
assert.True(t, context.Result().Options().Headers().Contains("key"))
assert.Equal(t, "value", context.Result().Options().Headers().Header("key"))
wg.Done()
})

Expand Down
5 changes: 0 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ type tcpClient struct {
eventbus EventBus
}

type Request struct {
Channel string `json:"channel"`
Message Message `json:"message"`
}

func (s *tcpClient) Connect() {
var conn net.Conn
var err error
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestClient(t *testing.T) {
var eventbus EventBus = NewEventBus()

eventbus.Channel("channel").Subscriber().Listen(func(context Context) {
assert.Equal(t, "Hello there", context.Result().Data)
assert.Equal(t, "Hello there", context.Result().Extract())
wg.Done()
})

Expand Down
4 changes: 2 additions & 2 deletions examples/eventbus/eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
})

eventbus.Channel("topic1").Processor(func(message goeventbus.Message) bool {
return message.Options.Headers().Contains("header")
return message.Options().Headers().Contains("header")
})

eventbus.Channel("topic2").Subscriber().Listen(func(dc goeventbus.Context) {
Expand Down Expand Up @@ -54,5 +54,5 @@ func publishTo(address string, data string) {
}

func printMessage(data goeventbus.Message) {
fmt.Printf("Message %s, Headers %s\n", data.Data, data.Options.Headers())
fmt.Printf("Message %s, Headers %s\n", data.Extract(), data.Options().Headers())
}
2 changes: 1 addition & 1 deletion examples/networkbus/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func main() {
}

func printMessage(data goeventbus.Message) {
fmt.Printf("Message %s, Headers %s\n", data.Data, data.Options.Headers())
fmt.Printf("Message %s, Headers %s\n", data.Extract(), data.Options().Headers())
}
16 changes: 12 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package goeventbus

type Message struct {
Data interface{}
Options MessageOptions
Data interface{}
MessageOptions MessageOptions
}

func CreateMessage() Message {
return Message{}
}

func (m Message) SetBody(data interface{}) Message {
func (m Message) SetBody(data any) Message {
m.Data = data
return m
}

func (m Message) SetOptions(options MessageOptions) Message {
m.Options = options
m.MessageOptions = options
return m
}

func (m Message) Extract() any {
return m.Data
}

func (m Message) Options() MessageOptions {
return m.MessageOptions
}
6 changes: 6 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package goeventbus

type Request struct {
Channel string `json:"channel"`
Message Message `json:"message"`
}
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestServer(t *testing.T) {
return
}
assert.Equal(t, "my-channel", request.Channel)
assert.Equal(t, "Hello there", request.Message.Data)
assert.Equal(t, "Hello there", request.Message.Extract())
wg.Done()

}()
Expand Down

0 comments on commit 623ca4c

Please sign in to comment.