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

Commit 78f410b

Browse files
authored
Merge pull request #35 from triggermesh/ce-overrides-update
CE context handling improvements
2 parents 437cd73 + 1810bfd commit 78f410b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

pkg/converter/cloudevents/cloudevents.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ const contentType = "application/cloudevents+json"
3333
type ceBinaryStructure struct {
3434
ID string `json:"id"`
3535
Type string `json:"type"`
36-
Time string `json:"time"`
3736
Source string `json:"source"`
3837
Specversion string `json:"specversion"`
39-
Contenttype string `json:"datacontenttype"`
40-
Data interface{} `json:"data"`
38+
Time string `json:"time,omitempty"`
39+
Contenttype string `json:"datacontenttype,omitempty"`
40+
Data interface{} `json:"data,omitempty"`
4141
}
4242

4343
// CloudEvent is a data structure required to map KLR responses to cloudevents
@@ -46,6 +46,10 @@ type CloudEvent struct {
4646
// only data payload or full event in binary format
4747
FunctionResponseMode string `envconfig:"function_response_mode" default:"data"`
4848

49+
Overrides Overrides `envconfig:"overrides"`
50+
}
51+
52+
type Overrides struct {
4953
EventType string `envconfig:"type" default:"ce.klr.triggermesh.io"`
5054
Source string `envconfig:"source" default:"knative-lambda-runtime"`
5155
Subject string `envconfig:"subject" default:"klr-response"`
@@ -67,7 +71,7 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
6771
// If response format is set to CloudEvents
6872
// and CE_TYPE is empty,
6973
// then reply with the empty response
70-
if ce.EventType == "" {
74+
if ce.Overrides.EventType == "" {
7175
return nil, nil
7276
}
7377

@@ -88,9 +92,9 @@ func (ce *CloudEvent) Response(data []byte) ([]byte, error) {
8892

8993
b := ceBinaryStructure{
9094
ID: uuid.NewString(),
91-
Type: ce.EventType,
95+
Type: ce.Overrides.EventType,
9296
Time: time.Now().Format(time.RFC3339),
93-
Source: ce.Source,
97+
Source: ce.Overrides.Source,
9498
Specversion: "1.0",
9599
Contenttype: contentType,
96100
Data: body,
@@ -104,14 +108,16 @@ func (ce *CloudEvent) fillInContext(data []byte) ([]byte, error) {
104108
return nil, fmt.Errorf("cannot unmarshal function response into binary CE: %w", err)
105109
}
106110

107-
switch {
108-
case response.ID == "":
111+
if response.ID == "" {
109112
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 == "":
113+
}
114+
if response.Type == "" {
115+
response.Type = ce.Overrides.EventType
116+
}
117+
if response.Source == "" {
118+
response.Source = ce.Overrides.Source
119+
}
120+
if response.Specversion == "" {
115121
response.Specversion = "1.0"
116122
}
117123

0 commit comments

Comments
 (0)