/
event.go
51 lines (43 loc) · 1.3 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
package service
// Event describes a service task.
type Event struct {
// Key is the key of event.
Key string `hash:"name:1"`
// Name is the name of event.
Name string `hash:"name:2"`
// Description is the description of event.
Description string `hash:"name:3"`
// Data holds the input parameters of event.
Data []*Parameter `hash:"name:4"`
// serviceName is the event's service's name.
serviceName string `hash:"-"`
}
// GetEvent returns event eventKey of service.
func (s *Service) GetEvent(eventKey string) (*Event, error) {
for _, event := range s.Events {
if event.Key == eventKey {
event.serviceName = s.Name
return event, nil
}
}
return nil, &EventNotFoundError{
EventKey: eventKey,
ServiceName: s.Name,
}
}
// ValidateData produces warnings for event datas that doesn't satisfy their parameter schemas.
func (e *Event) ValidateData(eventData map[string]interface{}) []*ParameterWarning {
return validateParametersSchema(e.Data, eventData)
}
// RequireData requires event datas to be matched with parameter schemas.
func (e *Event) RequireData(eventData map[string]interface{}) error {
warnings := e.ValidateData(eventData)
if len(warnings) > 0 {
return &InvalidEventDataError{
EventKey: e.Key,
ServiceName: e.serviceName,
Warnings: warnings,
}
}
return nil
}