-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_options.go
87 lines (72 loc) · 2.75 KB
/
response_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cloudevents
import (
"fmt"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/uuid"
)
// Optional headers
const (
StatefulWorkflowHeader = "statefulbridge"
StatefulWorkflowInstanceHeader = "statefulid"
ProcessedTypeHeader = "processedtype"
ProcessedSourceHeader = "processedsource"
ProcessedIDHeader = "processedid"
)
// EventResponseOption given the incoming and outgoing event,
// modifies the response event before being sent.
type EventResponseOption func(in, out *cloudevents.Event) error
// ResponseWithSubject is an option for modifying returned event subject.
func ResponseWithSubject(subject string) EventResponseOption {
return func(in, out *cloudevents.Event) error {
return out.Context.SetSubject(subject)
}
}
// ResponseWithID is an option for modifying returned event ID.
func ResponseWithID(id string) EventResponseOption {
return func(in, out *cloudevents.Event) error {
return out.Context.SetID(id)
}
}
// ResponseWithStatefulHeaders creates stateful headers if not present.
func ResponseWithStatefulHeaders(bridge string) EventResponseOption {
return func(in, out *cloudevents.Event) error {
ext := in.Context.GetExtensions()
b, ok := ext[StatefulWorkflowHeader]
if !ok {
b = bridge
}
if err := out.Context.SetExtension(StatefulWorkflowHeader, b); err != nil {
return fmt.Errorf("error ensuring %q extension: %w", StatefulWorkflowHeader, err)
}
instance, ok := ext[StatefulWorkflowInstanceHeader]
if !ok {
instance = uuid.New().String()
}
if err := out.Context.SetExtension(StatefulWorkflowInstanceHeader, instance); err != nil {
return fmt.Errorf("error ensuring %q extension: %w", StatefulWorkflowInstanceHeader, err)
}
return nil
}
}
// ResponseWithProcessedHeaders creates processed headers, propagating
// information about the incoming event headers into the outgoing event.
func ResponseWithProcessedHeaders() EventResponseOption {
return func(in, out *cloudevents.Event) error {
if err := out.Context.SetExtension(ProcessedTypeHeader, in.Context.GetType()); err != nil {
return fmt.Errorf("error setting %q extension: %w", ProcessedTypeHeader, err)
}
if err := out.Context.SetExtension(ProcessedSourceHeader, in.Context.GetSource()); err != nil {
return fmt.Errorf("error setting %q extension: %w", ProcessedSourceHeader, err)
}
if err := out.Context.SetExtension(ProcessedIDHeader, in.Context.GetID()); err != nil {
return fmt.Errorf("error setting %q extension: %w", ProcessedIDHeader, err)
}
return nil
}
}
// ResponseWithDataContentType is an option for modifying returned event content type.
func ResponseWithDataContentType(dct string) EventResponseOption {
return func(in, out *cloudevents.Event) error {
return out.Context.SetDataContentType(dct)
}
}