mirror of https://github.com/dapr/go-sdk.git
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:
parent
9944bfebcc
commit
365078470b
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue