Skip to content

Commit 791f4c7

Browse files
roblaszczakm110
authored andcommitted
added docs for router context (ThreeDotsLabs#167)
1 parent c9213ee commit 791f4c7

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

docs/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ else
3838
"message/message.go"
3939
"message/pubsub.go"
4040
"message/router.go"
41+
"message/router_context.go"
4142
"pubsub/gochannel/pubsub.go"
4243

4344
"components/cqrs/command_bus.go"

docs/content/docs/messages-router.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ A full list of standard middlewares can be found in [Middlewares]({{< ref "/docs
104104
{{% /render-md %}}
105105

106106
A full list of standard plugins can be found in [message/router/plugin](https://github.com/ThreeDotsLabs/watermill/tree/master/message/router/plugin).
107+
108+
### Context
109+
110+
Each message received by handler holds some useful values in the `context`:
111+
112+
{{% render-md %}}
113+
{{% load-snippet-partial file="src-link/message/router_context.go" first_line_contains="// HandlerNameFromCtx" last_line_contains="func PublishTopicFromCtx" padding_after="2" %}}
114+
{{% /render-md %}}

docs/content/pubsubs/kafka.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ When `nil` is passed, default config is used (`DefaultSaramaSubscriberConfig`).
4040

4141
##### Publisher
4242
{{% render-md %}}
43-
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/publisher.go" first_line_contains="// NewPublisher" last_line_contains="(message.Publisher, error)" padding_after="0" %}}
43+
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/publisher.go" first_line_contains="// NewPublisher" last_line_contains="(*Publisher, error)" padding_after="0" %}}
4444

4545
Example:
4646
{{% load-snippet-partial file="src-link/_examples/pubsubs/kafka/main.go" first_line_contains="saramaSubscriberConfig :=" last_line_contains="panic(err)" padding_after="1" %}}
@@ -49,7 +49,7 @@ Example:
4949

5050
##### Subscriber
5151
{{% render-md %}}
52-
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/subscriber.go" first_line_contains="// NewSubscriber" last_line_contains="(message.Subscriber, error)" padding_after="0" %}}
52+
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/subscriber.go" first_line_contains="// NewSubscriber" last_line_contains="(*Subscriber, error)" padding_after="0" %}}
5353

5454
Example:
5555
{{% load-snippet-partial file="src-link/_examples/pubsubs/kafka/main.go" first_line_contains="publisher, err := kafka.NewPublisher" last_line_contains="panic(err)" padding_after="1" %}}

message/context.go renamed to message/router_context.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,29 @@ func valFromCtx(ctx context.Context, key ctxKey) string {
2222
return val
2323
}
2424

25+
// HandlerNameFromCtx returns the name of the message handler in the router that consumed the message.
2526
func HandlerNameFromCtx(ctx context.Context) string {
2627
return valFromCtx(ctx, handlerNameKey)
2728
}
2829

30+
// PublisherNameFromCtx returns the name of the message publisher type that published the message in the router.
31+
// For example, for Kafka it will be `kafka.Publisher`.
2932
func PublisherNameFromCtx(ctx context.Context) string {
3033
return valFromCtx(ctx, publisherNameKey)
3134
}
3235

36+
// SubscriberNameFromCtx returns the name of the message subscriber type that subscribed to the message in the router.
37+
// For example, for Kafka it will be `kafka.Subscriber`.
3338
func SubscriberNameFromCtx(ctx context.Context) string {
3439
return valFromCtx(ctx, subscriberNameKey)
3540
}
3641

42+
// SubscribeTopicFromCtx returns the topic from which message was received in the router.
3743
func SubscribeTopicFromCtx(ctx context.Context) string {
3844
return valFromCtx(ctx, subscribeTopicKey)
3945
}
4046

47+
// PublishTopicFromCtx returns the topic to which message will be published by the router.
4148
func PublishTopicFromCtx(ctx context.Context) string {
4249
return valFromCtx(ctx, publishTopicKey)
4350
}
File renamed without changes.

0 commit comments

Comments
 (0)