@@ -8,22 +8,28 @@ import (
8
8
"github.com/google/uuid"
9
9
)
10
10
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"`
13
16
}
14
17
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 {}
19
21
}
20
22
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 ) {}
22
26
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 ) {
24
29
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 )
27
33
response .SetID (uuid .New ().String ())
28
34
if err := response .SetData (cloudevents .ApplicationJSON , data ); err != nil {
29
35
return 0 , fmt .Errorf ("failed to set event data: %w" , err )
0 commit comments