@@ -30,7 +30,7 @@ import (
30
30
31
31
const contentType = "application/cloudevents+json"
32
32
33
- type ceBody struct {
33
+ type ceBinaryStructure struct {
34
34
ID string `json:"id"`
35
35
Type string `json:"type"`
36
36
Time string `json:"time"`
@@ -42,6 +42,10 @@ type ceBody struct {
42
42
43
43
// CloudEvent is a data structure required to map KLR responses to cloudevents
44
44
type CloudEvent struct {
45
+ // FunctionResponseMode describes what data is returned from the function:
46
+ // only data payload or full event in binary format
47
+ FunctionResponseMode string `envconfig:"function_response_mode" default:"data"`
48
+
45
49
EventType string `envconfig:"type" default:"ce.klr.triggermesh.io"`
46
50
Source string `envconfig:"source" default:"knative-lambda-runtime"`
47
51
Subject string `envconfig:"subject" default:"klr-response"`
@@ -56,6 +60,10 @@ func New() (*CloudEvent, error) {
56
60
}
57
61
58
62
func (ce * CloudEvent ) Response (data []byte ) ([]byte , error ) {
63
+ if ce .FunctionResponseMode == "event" {
64
+ return ce .fillInContext (data )
65
+ }
66
+
59
67
// If response format is set to CloudEvents
60
68
// and CE_TYPE is empty,
61
69
// then reply with the empty response
@@ -78,7 +86,7 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
78
86
body = string (data )
79
87
}
80
88
81
- b := ceBody {
89
+ b := ceBinaryStructure {
82
90
ID : uuid .NewString (),
83
91
Type : ce .EventType ,
84
92
Time : time .Now ().Format (time .RFC3339 ),
@@ -90,6 +98,26 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
90
98
return json .Marshal (b )
91
99
}
92
100
101
+ func (ce * CloudEvent ) fillInContext (data []byte ) ([]byte , error ) {
102
+ var response ceBinaryStructure
103
+ if err := json .Unmarshal (data , & response ); err != nil {
104
+ return nil , fmt .Errorf ("cannot unmarshal function response into binary CE: %w" , err )
105
+ }
106
+
107
+ switch {
108
+ case response .ID == "" :
109
+ response .ID = uuid .NewString ()
110
+ case response .Type == "" :
111
+ response .Type = ce .EventType
112
+ case response .Source == "" :
113
+ response .Source = ce .Source
114
+ case response .Specversion == "" :
115
+ response .Specversion = "1.0"
116
+ }
117
+
118
+ return json .Marshal (response )
119
+ }
120
+
93
121
func (ce * CloudEvent ) Request (request []byte , headers http.Header ) ([]byte , map [string ]string , error ) {
94
122
var context map [string ]string
95
123
var body []byte
0 commit comments