@@ -30,7 +30,7 @@ import (
3030
3131const contentType = "application/cloudevents+json"
3232
33- type ceBody struct {
33+ type ceBinaryStructure struct {
3434 ID string `json:"id"`
3535 Type string `json:"type"`
3636 Time string `json:"time"`
@@ -42,6 +42,10 @@ type ceBody struct {
4242
4343// CloudEvent is a data structure required to map KLR responses to cloudevents
4444type 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+
4549 EventType string `envconfig:"type" default:"ce.klr.triggermesh.io"`
4650 Source string `envconfig:"source" default:"knative-lambda-runtime"`
4751 Subject string `envconfig:"subject" default:"klr-response"`
@@ -56,6 +60,10 @@ func New() (*CloudEvent, error) {
5660}
5761
5862func (ce * CloudEvent ) Response (data []byte ) ([]byte , error ) {
63+ if ce .FunctionResponseMode == "event" {
64+ return ce .fillInContext (data )
65+ }
66+
5967 // If response format is set to CloudEvents
6068 // and CE_TYPE is empty,
6169 // then reply with the empty response
@@ -78,7 +86,7 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
7886 body = string (data )
7987 }
8088
81- b := ceBody {
89+ b := ceBinaryStructure {
8290 ID : uuid .NewString (),
8391 Type : ce .EventType ,
8492 Time : time .Now ().Format (time .RFC3339 ),
@@ -90,6 +98,26 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
9098 return json .Marshal (b )
9199}
92100
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+
93121func (ce * CloudEvent ) Request (request []byte , headers http.Header ) ([]byte , map [string ]string , error ) {
94122 var context map [string ]string
95123 var body []byte
0 commit comments