-
Notifications
You must be signed in to change notification settings - Fork 3
/
event_def.go
54 lines (42 loc) · 1.18 KB
/
event_def.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
package eventline
import (
"encoding/json"
"fmt"
"reflect"
)
type UnknownEventDefError struct {
Connector string
Name string
}
func (err UnknownEventDefError) Error() string {
return fmt.Sprintf("unknown event %q in connector %q",
err.Name, err.Connector)
}
type EventDef struct {
Name string
Data EventData
SubscriptionParameters SubscriptionParameters
}
type EventData interface {
}
func NewEventDef(name string, data EventData, subscriptionParameters SubscriptionParameters) *EventDef {
return &EventDef{
Name: name,
Data: data,
SubscriptionParameters: subscriptionParameters,
}
}
func (edef *EventDef) DecodeSubscriptionParameters(data []byte) (SubscriptionParameters, error) {
eparams := reflect.New(reflect.TypeOf(edef.SubscriptionParameters).Elem()).Interface()
if err := json.Unmarshal(data, eparams); err != nil {
return nil, err
}
return eparams.(SubscriptionParameters), nil
}
func (edef *EventDef) DecodeData(data []byte) (EventData, error) {
edata := reflect.New(reflect.TypeOf(edef.Data).Elem()).Interface()
if err := json.Unmarshal(data, edata); err != nil {
return nil, err
}
return edata.(EventData), nil
}