-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
cloudevents.go
31 lines (27 loc) · 1.16 KB
/
cloudevents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package pubsub
import (
contrib_contenttype "github.com/dapr/components-contrib/contenttype"
contrib_pubsub "github.com/dapr/components-contrib/pubsub"
"github.com/google/uuid"
)
// CloudEvent is a reqeust object to create a Dapr compliant cloudevent
type CloudEvent struct {
ID string
Data []byte
Topic string
Pubsub string
DataContentType string
TraceID string
}
// NewCloudEvent encapusalates the creation of a Dapr cloudevent from an existing cloudevent or a raw payload
func NewCloudEvent(req *CloudEvent) (map[string]interface{}, error) {
if contrib_contenttype.IsCloudEventContentType(req.DataContentType) {
return contrib_pubsub.FromCloudEvent(req.Data, req.Topic, req.Pubsub, req.TraceID)
}
return contrib_pubsub.NewCloudEventsEnvelope(uuid.New().String(), req.ID, contrib_pubsub.DefaultCloudEventType, "", req.Topic, req.Pubsub,
req.DataContentType, req.Data, req.TraceID), nil
}