Skip to content

Commit 3650784

Browse files
authored
Simplify the use of pubsub rawPayload (dapr#264)
* fix: simplify the use of pubsub rawPayload Signed-off-by: zhangchao <zchao9100@gmail.com> * add test Signed-off-by: zhangchao <zchao9100@gmail.com>
1 parent 9944bfe commit 3650784

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

client/pubsub.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import (
2323
pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
2424
)
2525

26+
const (
27+
rawPayload = "rawPayload"
28+
trueValue = "true"
29+
)
30+
2631
// PublishEventOption is the type for the functional option.
2732
type PublishEventOption func(*pb.PublishEventRequest)
2833

@@ -81,6 +86,17 @@ func PublishEventWithMetadata(metadata map[string]string) PublishEventOption {
8186
}
8287
}
8388

89+
// PublishEventWithRawPayload can be passed as option to PublishEvent to set rawPayload metadata.
90+
func PublishEventWithRawPayload() PublishEventOption {
91+
return func(e *pb.PublishEventRequest) {
92+
if e.Metadata == nil {
93+
e.Metadata = map[string]string{rawPayload: trueValue}
94+
} else {
95+
e.Metadata[rawPayload] = trueValue
96+
}
97+
}
98+
}
99+
84100
// PublishEventfromCustomContent serializes an struct and publishes its contents as data (JSON) onto topic in specific pubsub component.
85101
// Deprecated: This method is deprecated and will be removed in a future version of the SDK. Please use `PublishEvent` instead.
86102
func (c *GRPCClient) PublishEventfromCustomContent(ctx context.Context, pubsubName, topicName string, data interface{}) error {

client/pubsub_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ func TestPublishEvent(t *testing.T) {
8484
err := testClient.PublishEventfromCustomContent(ctx, "messages", "test", make(chan struct{}))
8585
assert.Error(t, err)
8686
})
87+
88+
t.Run("raw payload", func(t *testing.T) {
89+
err := testClient.PublishEvent(ctx, "messages", "test", []byte("ping"), PublishEventWithRawPayload())
90+
assert.Nil(t, err)
91+
})
8792
}

0 commit comments

Comments
 (0)