-
Notifications
You must be signed in to change notification settings - Fork 217
/
eventcontext_v03.go
331 lines (298 loc) · 9.63 KB
/
eventcontext_v03.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package cloudevents
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
)
const (
// CloudEventsVersionV03 represents the version 0.3 of the CloudEvents spec.
CloudEventsVersionV03 = "0.3"
)
// EventContextV03 represents the non-data attributes of a CloudEvents v0.3
// event.
type EventContextV03 struct {
// SpecVersion - The version of the CloudEvents specification used by the event.
SpecVersion string `json:"specversion"`
// Type - The type of the occurrence which has happened.
Type string `json:"type"`
// Source - A URI describing the event producer.
Source types.URLRef `json:"source"`
// Subject - The subject of the event in the context of the event producer
// (identified by `source`).
Subject *string `json:"subject,omitempty"`
// ID of the event; must be non-empty and unique within the scope of the producer.
ID string `json:"id"`
// Time - A Timestamp when the event happened.
Time *types.Timestamp `json:"time,omitempty"`
// DataSchema - A link to the schema that the `data` attribute adheres to.
SchemaURL *types.URLRef `json:"schemaurl,omitempty"`
// GetDataMediaType - A MIME (RFC2046) string describing the media type of `data`.
// TODO: Should an empty string assume `application/json`, `application/octet-stream`, or auto-detect the content?
DataContentType *string `json:"datacontenttype,omitempty"`
// DeprecatedDataContentEncoding describes the content encoding for the `data` attribute. Valid: nil, `Base64`.
DataContentEncoding *string `json:"datacontentencoding,omitempty"`
// Extensions - Additional extension metadata beyond the base spec.
Extensions map[string]interface{} `json:"-"`
}
// Adhere to EventContext
var _ EventContext = (*EventContextV03)(nil)
// ExtensionAs implements EventContext.ExtensionAs
func (ec EventContextV03) ExtensionAs(name string, obj interface{}) error {
value, ok := ec.Extensions[name]
if !ok {
return fmt.Errorf("extension %q does not exist", name)
}
// Try to unmarshal extension if we find it as a RawMessage.
switch v := value.(type) {
case json.RawMessage:
if err := json.Unmarshal(v, obj); err == nil {
// if that worked, return with obj set.
return nil
}
}
// else try as a string ptr.
// Only support *string for now.
switch v := obj.(type) {
case *string:
if valueAsString, ok := value.(string); ok {
*v = valueAsString
return nil
} else {
return fmt.Errorf("invalid type for extension %q", name)
}
default:
return fmt.Errorf("unknown extension type %T", obj)
}
}
// SetExtension adds the extension 'name' with value 'value' to the CloudEvents context.
func (ec *EventContextV03) SetExtension(name string, value interface{}) error {
if ec.Extensions == nil {
ec.Extensions = make(map[string]interface{})
}
if value == nil {
delete(ec.Extensions, name)
} else {
v, err := types.Validate(value)
if err == nil {
ec.Extensions[name] = v
}
return err
}
return nil
}
// Clone implements EventContextConverter.Clone
func (ec EventContextV03) Clone() EventContext {
return ec.AsV03()
}
// AsV01 implements EventContextConverter.AsV01
func (ec EventContextV03) AsV01() *EventContextV01 {
ecv2 := ec.AsV02()
return ecv2.AsV01()
}
// AsV02 implements EventContextConverter.AsV02
func (ec EventContextV03) AsV02() *EventContextV02 {
ret := EventContextV02{
SpecVersion: CloudEventsVersionV02,
ID: ec.ID,
Time: ec.Time,
Type: ec.Type,
SchemaURL: ec.SchemaURL,
ContentType: ec.DataContentType,
Source: ec.Source,
Extensions: make(map[string]interface{}),
}
// Subject was introduced in 0.3, so put it in an extension for 0.2.
if ec.Subject != nil {
_ = ret.SetExtension(SubjectKey, *ec.Subject)
}
// DeprecatedDataContentEncoding was introduced in 0.3, so put it in an extension for 0.2.
if ec.DataContentEncoding != nil {
_ = ret.SetExtension(DataContentEncodingKey, *ec.DataContentEncoding)
}
if ec.Extensions != nil {
for k, v := range ec.Extensions {
ret.Extensions[k] = v
}
}
if len(ret.Extensions) == 0 {
ret.Extensions = nil
}
return &ret
}
// AsV03 implements EventContextConverter.AsV03
func (ec EventContextV03) AsV03() *EventContextV03 {
ec.SpecVersion = CloudEventsVersionV03
return &ec
}
// AsV04 implements EventContextConverter.AsV04
func (ec EventContextV03) AsV1() *EventContextV1 {
ret := EventContextV1{
SpecVersion: CloudEventsVersionV1,
ID: ec.ID,
Time: ec.Time,
Type: ec.Type,
DataContentType: ec.DataContentType,
Source: types.URIRef{URL: ec.Source.URL},
Subject: ec.Subject,
Extensions: make(map[string]interface{}),
}
if ec.SchemaURL != nil {
ret.DataSchema = &types.URI{URL: ec.SchemaURL.URL}
}
// DataContentEncoding was removed in 1.0, so put it in an extension for 1.0.
if ec.DataContentEncoding != nil {
_ = ret.SetExtension(DataContentEncodingKey, *ec.DataContentEncoding)
}
if ec.Extensions != nil {
for k, v := range ec.Extensions {
k = strings.ToLower(k)
ret.Extensions[k] = v
}
}
if len(ret.Extensions) == 0 {
ret.Extensions = nil
}
return &ret
}
// Validate returns errors based on requirements from the CloudEvents spec.
// For more details, see https://github.com/cloudevents/spec/blob/master/spec.md
// As of Feb 26, 2019, commit 17c32ea26baf7714ad027d9917d03d2fff79fc7e
// + https://github.com/cloudevents/spec/pull/387 -> datacontentencoding
// + https://github.com/cloudevents/spec/pull/406 -> subject
func (ec EventContextV03) Validate() error {
errors := []string(nil)
// type
// Type: String
// Constraints:
// REQUIRED
// MUST be a non-empty string
// SHOULD be prefixed with a reverse-DNS name. The prefixed domain dictates the organization which defines the semantics of this event type.
eventType := strings.TrimSpace(ec.Type)
if eventType == "" {
errors = append(errors, "type: MUST be a non-empty string")
}
// specversion
// Type: String
// Constraints:
// REQUIRED
// MUST be a non-empty string
specVersion := strings.TrimSpace(ec.SpecVersion)
if specVersion == "" {
errors = append(errors, "specversion: MUST be a non-empty string")
}
// source
// Type: URI-reference
// Constraints:
// REQUIRED
source := strings.TrimSpace(ec.Source.String())
if source == "" {
errors = append(errors, "source: REQUIRED")
}
// subject
// Type: String
// Constraints:
// OPTIONAL
// MUST be a non-empty string
if ec.Subject != nil {
subject := strings.TrimSpace(*ec.Subject)
if subject == "" {
errors = append(errors, "subject: if present, MUST be a non-empty string")
}
}
// id
// Type: String
// Constraints:
// REQUIRED
// MUST be a non-empty string
// MUST be unique within the scope of the producer
id := strings.TrimSpace(ec.ID)
if id == "" {
errors = append(errors, "id: MUST be a non-empty string")
// no way to test "MUST be unique within the scope of the producer"
}
// time
// Type: Timestamp
// Constraints:
// OPTIONAL
// If present, MUST adhere to the format specified in RFC 3339
// --> no need to test this, no way to set the time without it being valid.
// schemaurl
// Type: URI
// Constraints:
// OPTIONAL
// If present, MUST adhere to the format specified in RFC 3986
if ec.SchemaURL != nil {
schemaURL := strings.TrimSpace(ec.SchemaURL.String())
// empty string is not RFC 3986 compatible.
if schemaURL == "" {
errors = append(errors, "schemaurl: if present, MUST adhere to the format specified in RFC 3986")
}
}
// datacontenttype
// Type: String per RFC 2046
// Constraints:
// OPTIONAL
// If present, MUST adhere to the format specified in RFC 2046
if ec.DataContentType != nil {
dataContentType := strings.TrimSpace(*ec.DataContentType)
if dataContentType == "" {
// TODO: need to test for RFC 2046
errors = append(errors, "datacontenttype: if present, MUST adhere to the format specified in RFC 2046")
}
}
// datacontentencoding
// Type: String per RFC 2045 Section 6.1
// Constraints:
// The attribute MUST be set if the data attribute contains string-encoded binary data.
// Otherwise the attribute MUST NOT be set.
// If present, MUST adhere to RFC 2045 Section 6.1
if ec.DataContentEncoding != nil {
dataContentEncoding := strings.ToLower(strings.TrimSpace(*ec.DataContentEncoding))
if dataContentEncoding != Base64 {
// TODO: need to test for RFC 2046
errors = append(errors, "datacontentencoding: if present, MUST adhere to RFC 2045 Section 6.1")
}
}
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "\n"))
}
return nil
}
// String returns a pretty-printed representation of the EventContext.
func (ec EventContextV03) String() string {
b := strings.Builder{}
b.WriteString("Context Attributes,\n")
b.WriteString(" specversion: " + ec.SpecVersion + "\n")
b.WriteString(" type: " + ec.Type + "\n")
b.WriteString(" source: " + ec.Source.String() + "\n")
if ec.Subject != nil {
b.WriteString(" subject: " + *ec.Subject + "\n")
}
b.WriteString(" id: " + ec.ID + "\n")
if ec.Time != nil {
b.WriteString(" time: " + ec.Time.String() + "\n")
}
if ec.SchemaURL != nil {
b.WriteString(" schemaurl: " + ec.SchemaURL.String() + "\n")
}
if ec.DataContentType != nil {
b.WriteString(" datacontenttype: " + *ec.DataContentType + "\n")
}
if ec.DataContentEncoding != nil {
b.WriteString(" datacontentencoding: " + *ec.DataContentEncoding + "\n")
}
if ec.Extensions != nil && len(ec.Extensions) > 0 {
b.WriteString("Extensions,\n")
keys := make([]string, 0, len(ec.Extensions))
for k := range ec.Extensions {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
b.WriteString(fmt.Sprintf(" %s: %v\n", key, ec.Extensions[key]))
}
}
return b.String()
}