Simplify the use of pubsub rawPayload (#264)

* fix: simplify the use of pubsub rawPayload

Signed-off-by: zhangchao <zchao9100@gmail.com>

* add test

Signed-off-by: zhangchao <zchao9100@gmail.com>
This commit is contained in:
Taction 2022-03-26 00:18:39 +08:00 committed by GitHub
parent 9944bfebcc
commit 365078470b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -23,6 +23,11 @@ import (
pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
)
const (
rawPayload = "rawPayload"
trueValue = "true"
)
// PublishEventOption is the type for the functional option.
type PublishEventOption func(*pb.PublishEventRequest)
@ -81,6 +86,17 @@ func PublishEventWithMetadata(metadata map[string]string) PublishEventOption {
}
}
// PublishEventWithRawPayload can be passed as option to PublishEvent to set rawPayload metadata.
func PublishEventWithRawPayload() PublishEventOption {
return func(e *pb.PublishEventRequest) {
if e.Metadata == nil {
e.Metadata = map[string]string{rawPayload: trueValue}
} else {
e.Metadata[rawPayload] = trueValue
}
}
}
// PublishEventfromCustomContent serializes an struct and publishes its contents as data (JSON) onto topic in specific pubsub component.
// Deprecated: This method is deprecated and will be removed in a future version of the SDK. Please use `PublishEvent` instead.
func (c *GRPCClient) PublishEventfromCustomContent(ctx context.Context, pubsubName, topicName string, data interface{}) error {

View File

@ -84,4 +84,9 @@ func TestPublishEvent(t *testing.T) {
err := testClient.PublishEventfromCustomContent(ctx, "messages", "test", make(chan struct{}))
assert.Error(t, err)
})
t.Run("raw payload", func(t *testing.T) {
err := testClient.PublishEvent(ctx, "messages", "test", []byte("ping"), PublishEventWithRawPayload())
assert.Nil(t, err)
})
}