Skip to content

Commit

Permalink
improved message building
Browse files Browse the repository at this point in the history
  • Loading branch information
stanipetrosyan committed Mar 18, 2024
1 parent 1ab91ed commit 865794c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ A processor works like a middleware, in fact forwards messages only if the predi
```go

eventbus.Channel("topic1").Processor(func(message goeventbus.Message) bool {
return logic
return message.
})
```

Expand Down
6 changes: 4 additions & 2 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ func TestMessageOptions(t *testing.T) {

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

message := CreateMessage().SetBody("Hi There").SetOptions(NewMessageOptions().AddHeader("key", "value"))
options := NewMessageOptions().SetHeaders(NewHeaders().Add("key", "value"))
message := CreateMessage().SetBody("Hi There").SetOptions(options)
eventBus.Channel("address").Publisher().Publish(message)
wg.Wait()
}
3 changes: 2 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func main() {
}

func publishTo(address string, data string) {
options := goeventbus.NewMessageOptions().AddHeader("header", "value")

options := goeventbus.NewMessageOptions().SetHeaders(goeventbus.NewHeaders().Add("header", "value"))
message := goeventbus.CreateMessage().SetBody(data).SetOptions(options)
for {
eventbus.Channel(address).Publisher().Publish(message)
Expand Down
37 changes: 27 additions & 10 deletions message_options.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package goeventbus

type MessageOptions struct {
headers map[string]string
}

func (op MessageOptions) AddHeader(key string, value string) MessageOptions {
op.headers[key] = value
return op
headers Headers
}

func (op MessageOptions) SetHeader(headers map[string]string) MessageOptions {
func (op MessageOptions) SetHeaders(headers Headers) MessageOptions {
op.headers = headers
return op
}

func (op MessageOptions) Header(key string) string {
return op.headers[key]
func (op MessageOptions) Headers() Headers {
return op.headers
}

func NewMessageOptions() MessageOptions {
return MessageOptions{
headers: map[string]string{},
headers: Headers{},
}
}

type Headers struct {
headers map[string]string
}

func (h Headers) Add(key string, value string) Headers {
h.headers[key] = value
return h
}

func (h Headers) Header(key string) string {
return h.headers[key]
}

func (h Headers) Contains(key string) bool {
_, exist := h.headers[key]
return exist
}

func NewHeaders() Headers {
return Headers{}
}

0 comments on commit 865794c

Please sign in to comment.