Skip to content

Commit

Permalink
added docs for router context (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak authored and m110 committed Dec 8, 2019
1 parent c9213ee commit 791f4c7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ else
"message/message.go"
"message/pubsub.go"
"message/router.go"
"message/router_context.go"
"pubsub/gochannel/pubsub.go"

"components/cqrs/command_bus.go"
Expand Down
8 changes: 8 additions & 0 deletions docs/content/docs/messages-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,11 @@ A full list of standard middlewares can be found in [Middlewares]({{< ref "/docs
{{% /render-md %}}

A full list of standard plugins can be found in [message/router/plugin](https://github.com/ThreeDotsLabs/watermill/tree/master/message/router/plugin).

### Context

Each message received by handler holds some useful values in the `context`:

{{% render-md %}}
{{% load-snippet-partial file="src-link/message/router_context.go" first_line_contains="// HandlerNameFromCtx" last_line_contains="func PublishTopicFromCtx" padding_after="2" %}}
{{% /render-md %}}
4 changes: 2 additions & 2 deletions docs/content/pubsubs/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ When `nil` is passed, default config is used (`DefaultSaramaSubscriberConfig`).

##### Publisher
{{% render-md %}}
{{% 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" %}}
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/publisher.go" first_line_contains="// NewPublisher" last_line_contains="(*Publisher, error)" padding_after="0" %}}

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

##### Subscriber
{{% render-md %}}
{{% 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" %}}
{{% load-snippet-partial file="src-link/watermill-kafka/pkg/kafka/subscriber.go" first_line_contains="// NewSubscriber" last_line_contains="(*Subscriber, error)" padding_after="0" %}}

Example:
{{% 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" %}}
Expand Down
7 changes: 7 additions & 0 deletions message/context.go → message/router_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@ func valFromCtx(ctx context.Context, key ctxKey) string {
return val
}

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

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

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

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

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

0 comments on commit 791f4c7

Please sign in to comment.