@@ -8,22 +8,28 @@ import (
88 "github.com/google/uuid"
99)
1010
11- type CloudEvents struct {
12- Service string
11+ // CloudEvent is a data structure required to map KLR responses to cloudevents
12+ type CloudEvent struct {
13+ EventType string `envconfig:"type" default:"ce.klr.triggermesh.io"`
14+ Source string `envconfig:"k_service" default:"knative-lambda-runtime"`
15+ Subject string `envconfig:"subject" default:"klr-response"`
1316}
1417
15- func NewMapper (service string ) * CloudEvents {
16- return & CloudEvents {
17- Service : service ,
18- }
18+ // NewMapper reurns an empty instance of CloudEvent structure
19+ func NewMapper () * CloudEvent {
20+ return & CloudEvent {}
1921}
2022
21- func (c * CloudEvents ) Request (r * http.Request ) {}
23+ // Request method can be used to customize incoming requests before passing them
24+ // to the KLR function
25+ func (c * CloudEvent ) Request (r * http.Request ) {}
2226
23- func (c * CloudEvents ) Response (w http.ResponseWriter , statusCode int , data []byte ) (int , error ) {
27+ // Response method converts generic KLR response to the Cloudevent format
28+ func (c * CloudEvent ) Response (w http.ResponseWriter , statusCode int , data []byte ) (int , error ) {
2429 response := cloudevents .NewEvent ()
25- response .SetType ("ce.klr.triggermesh.io" )
26- response .SetSource (c .Service )
30+ response .SetType (c .EventType )
31+ response .SetSource (c .Source )
32+ response .SetSubject (c .Subject )
2733 response .SetID (uuid .New ().String ())
2834 if err := response .SetData (cloudevents .ApplicationJSON , data ); err != nil {
2935 return 0 , fmt .Errorf ("failed to set event data: %w" , err )
0 commit comments