-
Notifications
You must be signed in to change notification settings - Fork 217
/
encoding.go
178 lines (153 loc) · 3.96 KB
/
encoding.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package http
import (
"context"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
cecontext "github.com/cloudevents/sdk-go/pkg/cloudevents/context"
)
// Encoding to use for HTTP transport.
type Encoding int32
type EncodingSelector func(context.Context, cloudevents.Event) Encoding
const (
// Default
Default Encoding = iota
// BinaryV01 is Binary CloudEvents spec v0.1.
BinaryV01
// StructuredV01 is Structured CloudEvents spec v0.1.
StructuredV01
// BinaryV02 is Binary CloudEvents spec v0.2.
BinaryV02
// StructuredV02 is Structured CloudEvents spec v0.2.
StructuredV02
// BinaryV03 is Binary CloudEvents spec v0.3.
BinaryV03
// StructuredV03 is Structured CloudEvents spec v0.3.
StructuredV03
// BatchedV03 is Batched CloudEvents spec v0.3.
BatchedV03
// Unknown is unknown.
Unknown
// Binary is used for Context Based Encoding Selections to use the
// DefaultBinaryEncodingSelectionStrategy
Binary = "binary"
// Structured is used for Context Based Encoding Selections to use the
// DefaultStructuredEncodingSelectionStrategy
Structured = "structured"
)
func ContextBasedEncodingSelectionStrategy(ctx context.Context, e cloudevents.Event) Encoding {
encoding := cecontext.EncodingFrom(ctx)
switch encoding {
case "", Binary:
return DefaultBinaryEncodingSelectionStrategy(ctx, e)
case Structured:
return DefaultStructuredEncodingSelectionStrategy(ctx, e)
}
return Default
}
// DefaultBinaryEncodingSelectionStrategy implements a selection process for
// which binary encoding to use based on spec version of the event.
func DefaultBinaryEncodingSelectionStrategy(ctx context.Context, e cloudevents.Event) Encoding {
switch e.SpecVersion() {
case cloudevents.CloudEventsVersionV01:
return BinaryV01
case cloudevents.CloudEventsVersionV02:
return BinaryV02
case cloudevents.CloudEventsVersionV03:
return BinaryV03
}
// Unknown version, return Default.
return Default
}
// DefaultStructuredEncodingSelectionStrategy implements a selection process
// for which structured encoding to use based on spec version of the event.
func DefaultStructuredEncodingSelectionStrategy(ctx context.Context, e cloudevents.Event) Encoding {
switch e.SpecVersion() {
case cloudevents.CloudEventsVersionV01:
return StructuredV01
case cloudevents.CloudEventsVersionV02:
return StructuredV02
case cloudevents.CloudEventsVersionV03:
return StructuredV03
}
// Unknown version, return Default.
return Default
}
// String pretty-prints the encoding as a string.
func (e Encoding) String() string {
switch e {
case Default:
return "Default Encoding " + e.Version()
// Binary
case BinaryV01:
fallthrough
case BinaryV02:
fallthrough
case BinaryV03:
return "Binary Encoding " + e.Version()
// Structured
case StructuredV01:
fallthrough
case StructuredV02:
fallthrough
case StructuredV03:
return "Structured Encoding " + e.Version()
// Batched
case BatchedV03:
return "Batched Encoding " + e.Version()
default:
return "Unknown Encoding"
}
}
// Version pretty-prints the encoding version as a string.
func (e Encoding) Version() string {
switch e {
case Default:
return "Default"
// Version 0.1
case BinaryV01:
fallthrough
case StructuredV01:
return "v0.1"
// Version 0.2
case BinaryV02:
fallthrough
case StructuredV02:
return "v0.2"
// Version 0.3
case BinaryV03:
fallthrough
case StructuredV03:
fallthrough
case BatchedV03:
return "v0.3"
// Unknown
default:
return "Unknown"
}
}
// Codec creates a structured string to represent the the codec version.
func (e Encoding) Codec() string {
switch e {
case Default:
return "default"
// Version 0.1
case BinaryV01:
return "binary/v0.1"
case StructuredV01:
return "structured/v0.1"
// Version 0.2
case BinaryV02:
return "binary/v0.2"
case StructuredV02:
return "structured/v0.2"
// Version 0.3
case BinaryV03:
return "binary/v0.3"
case StructuredV03:
return "structured/v0.3"
case BatchedV03:
return "batched/v0.3"
// Unknown
default:
return "unknown"
}
}