Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 356748f

Browse files
authored
Merge pull request #31 from triggermesh/json-body
JSON in CE response body
2 parents 2e35ee4 + 526a8db commit 356748f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pkg/converter/cloudevents/cloudevents.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
)
2727

2828
type ceBody struct {
29-
ID string `json:"id"`
30-
Type string `json:"type"`
31-
Time string `json:"time"`
32-
Source string `json:"source"`
33-
Specversion string `json:"specversion"`
34-
Data string `json:"data"`
29+
ID string `json:"id"`
30+
Type string `json:"type"`
31+
Time string `json:"time"`
32+
Source string `json:"source"`
33+
Specversion string `json:"specversion"`
34+
Data interface{} `json:"data"`
3535
}
3636

3737
const contentType = "application/cloudevents+json"
@@ -59,13 +59,21 @@ func (ce *CloudEvent) Convert(data []byte) ([]byte, error) {
5959
return nil, nil
6060
}
6161

62+
var body interface{}
63+
body = string(data)
64+
65+
// try to decode function's response into JSON
66+
if json.Valid(data) {
67+
body = json.RawMessage(data)
68+
}
69+
6270
b := ceBody{
6371
ID: uuid.NewString(),
6472
Type: ce.EventType,
6573
Time: time.Now().Format(time.RFC3339),
6674
Source: ce.Source,
6775
Specversion: "1.0",
68-
Data: string(data),
76+
Data: body,
6977
}
7078
return json.Marshal(b)
7179
}

0 commit comments

Comments
 (0)