-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathevent.go
301 lines (227 loc) · 7.95 KB
/
event.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
/*
Copyright 2017 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nuclio
import (
"errors"
"strconv"
"time"
)
// ErrUnsupported is returned when an unsupported interface on the event is called
var ErrUnsupported = errors.New("Event does not support this interface")
// ErrTypeConversion is returned when a type conversion for headers / fields fails
var ErrTypeConversion = errors.New("Cannot convert to this type")
// TriggerInfoProvider provides information about the trigger in which this event originated
type TriggerInfoProvider interface {
// GetClass gets the class of source (sync, async, etc)
GetClass() string
// GetKind gets specific kind of source (http, rabbit mq, etc)
GetKind() string
// GetName get given name of trigger
GetName() string
}
// Event allows access to the concrete event
type Event interface {
// GetID returns the ID of the event
GetID() ID
// SetID sets the ID of the event
SetID(ID)
// SetTriggerInfoProvider sets the information about the trigger who triggered this event
SetTriggerInfoProvider(TriggerInfoProvider)
// GetTriggerInfo retruns a trigger info provider
GetTriggerInfo() TriggerInfoProvider
// GetContentType returns the content type of the body
GetContentType() string
// GetBody returns the body of the event
GetBody() []byte
// GetBodyObject returns the body of the event as an object
GetBodyObject() interface{}
// GetHeader returns the header by name as an interface{}
GetHeader(string) interface{}
// GetHeaderByteSlice returns the header by name as a byte slice
GetHeaderByteSlice(string) []byte
// GetHeaderString returns the header by name as a string
GetHeaderString(string) string
// GetHeaderInt returns the field by name as an integer
GetHeaderInt(string) (int, error)
// GetHeaders loads all headers into a map of string / interface{}
GetHeaders() map[string]interface{}
// GetField returns the field by name as an interface{}
GetField(string) interface{}
// GetFieldByteSlice returns the field by name as a byte slice
GetFieldByteSlice(string) []byte
// GetFieldString returns the field by name as a string
GetFieldString(string) string
// GetFieldInt returns the field by name as an integer
GetFieldInt(string) (int, error)
// GetFields loads all fields into a map of string / interface{}
GetFields() map[string]interface{}
// GetTimestamp returns when the event originated
GetTimestamp() time.Time
// GetPath returns the path of the event
GetPath() string
// GetURL returns the URL of the event
GetURL() string
// GetMethod returns the method of the event, if applicable
GetMethod() string
// GetShardID returns the ID of the shard from which this event arrived, if applicable
GetShardID() int
// GetTotalNumShards returns the total number of shards, if applicable
GetTotalNumShards() int
// GetType returns the type of event
GetType() string
// GetTypeVersion returns the version of the type
GetTypeVersion() string
// GetVersion returns the version of the event
GetVersion() string
// GetLastInBatch returns whether the event is the last event in a trigger specific batch
GetLastInBatch() bool
// GetOffset returns the offset of the event
GetOffset() int
// GetTopic returns the topic of the event
GetTopic() string
}
// AbstractEvent provides a base implementation of an event
type AbstractEvent struct {
triggerInfoProvider TriggerInfoProvider
id ID
emptyByteArray []byte
emptyHeaders map[string]interface{}
}
// SetTriggerInfoProvider sets the information about the trigger who triggered this event
func (ae *AbstractEvent) SetTriggerInfoProvider(triggerInfoProvider TriggerInfoProvider) {
ae.triggerInfoProvider = triggerInfoProvider
}
// GetTriggerInfo returns a trigger info provider
func (ae *AbstractEvent) GetTriggerInfo() TriggerInfoProvider {
return ae.triggerInfoProvider
}
// GetID returns the ID of the event
func (ae *AbstractEvent) GetID() ID {
return ae.id
}
// SetID sets the ID of the event
func (ae *AbstractEvent) SetID(id ID) {
ae.id = id
}
// GetContentType returns the content type of the body
func (ae *AbstractEvent) GetContentType() string {
return ""
}
// GetBody returns the body of the event
func (ae *AbstractEvent) GetBody() []byte {
return ae.emptyByteArray
}
// GetBodyObject returns the body of the event as an object
func (ae *AbstractEvent) GetBodyObject() interface{} {
return ae.GetBody()
}
// GetHeader returns the header by name as an interface{}
func (ae *AbstractEvent) GetHeader(key string) interface{} {
return nil
}
// GetHeaderByteSlice returns the header by name as a byte slice
func (ae *AbstractEvent) GetHeaderByteSlice(key string) []byte {
return ae.emptyByteArray
}
// GetHeaderString returns the header by name as a string
func (ae *AbstractEvent) GetHeaderString(key string) string {
return string(ae.GetHeaderByteSlice(key))
}
// GetHeaderInt returns the field by name as an integer
func (ae *AbstractEvent) GetHeaderInt(key string) (int, error) {
// try to get header as an interface
headerAsInterface := ae.GetHeader(key)
// if the header value is not an integer
switch typedHeader := headerAsInterface.(type) {
case int:
return typedHeader, nil
case string:
return strconv.Atoi(typedHeader)
case []byte:
return strconv.Atoi(string(typedHeader))
default:
return 0, ErrTypeConversion
}
}
// GetHeaders loads all headers into a map of string / interface{}
func (ae *AbstractEvent) GetHeaders() map[string]interface{} {
return ae.emptyHeaders
}
// GetTimestamp returns when the event originated
func (ae *AbstractEvent) GetTimestamp() time.Time {
return time.Now()
}
// GetPath returns the path of the event
func (ae *AbstractEvent) GetPath() string {
return ""
}
// GetURL returns the URL of the event
func (ae *AbstractEvent) GetURL() string {
return ""
}
// GetMethod returns the method of the event, if applicable
func (ae *AbstractEvent) GetMethod() string {
return ""
}
// GetField returns the field by name as an interface{}
func (ae *AbstractEvent) GetField(key string) interface{} {
return nil
}
// GetFieldByteSlice returns the field by name as a byte slice
func (ae *AbstractEvent) GetFieldByteSlice(key string) []byte {
return nil
}
// GetFieldString returns the field by name as a string
func (ae *AbstractEvent) GetFieldString(key string) string {
return ""
}
// GetFieldInt returns the field by name as an integer
func (ae *AbstractEvent) GetFieldInt(key string) (int, error) {
return 0, ErrUnsupported
}
// GetFields loads all fields into a map of string / interface{}
func (ae *AbstractEvent) GetFields() map[string]interface{} {
return nil
}
// GetShardID returns the ID of the shard from which this event arrived, if applicable
func (ae *AbstractEvent) GetShardID() int {
return -1
}
// GetTotalNumShards returns the total number of shards, if applicable
func (ae *AbstractEvent) GetTotalNumShards() int {
return 0
}
// GetType returns the type of event
func (ae *AbstractEvent) GetType() string {
return ""
}
// GetTypeVersion returns the version of the type
func (ae *AbstractEvent) GetTypeVersion() string {
return ""
}
// GetVersion returns the version of the event
func (ae *AbstractEvent) GetVersion() string {
return ""
}
// GetLastInBatch returns whether the event is the last event in a trigger specific batch
func (ae *AbstractEvent) GetLastInBatch() bool {
return false
}
// GetOffset returns the offset of the event
func (ae *AbstractEvent) GetOffset() int {
return 0
}
// GetTopic returns the topic of the event
func (ae *AbstractEvent) GetTopic() string {
return ""
}