fixes double envelop
@@ -15,7 +15,7 @@ const (
// CloudEventsSpecVersion is the specversion used by Dapr for the cloud events implementation
CloudEventsSpecVersion = "0.3"
//ContentType is the Cloud Events HTTP content type
- ContentType = "application/cloudevents+json"
+ ContentType = "application/json"
)
// CloudEventsEnvelope describes the Dapr implementation of the Cloud Events spec
@@ -30,29 +30,55 @@ type CloudEventsEnvelope struct {
Subject string `json:"subject"`
}
-// NewCloudEventsEnvelope returns a new CloudEventsEnvelope
+// NewCloudEventsEnvelope returns CloudEventsEnvelope from data or a new one when data content was not
+// already an existing CE. Invoked by either the HTTP or gRPC pug handles where:
+// id = new UUID
+// source = handling app ID
+// eventType = const DefaultCloudEventType from this package ("com.dapr.event.sent")
+// subject = diagnostics span context W3C as string
func NewCloudEventsEnvelope(id, source, eventType, subject string, data []byte) *CloudEventsEnvelope {
- if eventType == "" {
- eventType = DefaultCloudEventType
+ var ce CloudEventsEnvelope
+ err := jsoniter.Unmarshal(data, &ce)
+ if err != nil {
+ // data content is not JSON
+ ce = CloudEventsEnvelope{
+ Data: string(data),
+ DataContentType: "text/plain",
+ }
}
- contentType := ""
- var i interface{}
- err := jsoniter.Unmarshal(data, &i)
- if err != nil {
- i = string(data)
- contentType = "text/plain"
+ // populate the values where CloudEvent has none
+ if ce.ID == "" {
+ ce.ID = id
+ }
+
+ if ce.Source == "" {
+ ce.Source = source
+ }
+
+ if ce.Type == "" {
+ if eventType == "" {
+ ce.Type = DefaultCloudEventType
} else {
- contentType = "application/json"
+ ce.Type = eventType
+ }
+ }
+
+ if ce.SpecVersion == "" {
+ ce.SpecVersion = CloudEventsSpecVersion
+ }
+
+ if ce.DataContentType == "" {
+ ce.DataContentType = ContentType
+ }
+
+ if ce.Data == nil {
+ ce.Data = data
}
- return &CloudEventsEnvelope{
- ID: id,
- Source: source,
- Type: eventType,
- Data: i,
- SpecVersion: CloudEventsSpecVersion,
- DataContentType: contentType,
- Subject: subject,
+ if ce.Subject == "" {
+ ce.Subject = subject
}
+
+ return &ce
}
