-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
51 lines (42 loc) · 1.62 KB
/
handlers.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
package eventuate
import "github.com/eventuate-clients/eventuate-client-golang/future"
type EventResultHandler func(interface{}, *EventMetadata) future.Settler
type EventResultHandlerMap map[string]map[string]EventResultHandler
type DispatcherMaker func(eventHandlers *EventResultHandlerMap) (Dispatcher, error)
func NewEventResultHandlerMap() *EventResultHandlerMap {
tmp := EventResultHandlerMap(make(map[string]map[string]EventResultHandler))
return &tmp
}
func (handlersMap *EventResultHandlerMap) AddHandler(aggType, eventType string, handler EventResultHandler) *EventResultHandlerMap {
aggregate, hasAggregate := (*handlersMap)[aggType]
if !hasAggregate {
aggregate = make(map[string]EventResultHandler)
(*handlersMap)[aggType] = aggregate
}
aggregate[eventType] = handler
return handlersMap
}
func (handlersMap *EventResultHandlerMap) GetHandler(aggType, eventType string) (*EventResultHandler, error) {
_, hasEntityType := (*handlersMap)[aggType]
if !hasEntityType {
return nil, AppError("Event handler for entity type %s is not registered", aggType)
}
handler, hasEventType := (*handlersMap)[aggType][eventType]
if !hasEventType {
return nil, AppError("Event handler for entity type/event type %s / %s is not registered",
aggType, eventType)
}
return &handler, nil
}
func (handlersMap *EventResultHandlerMap) transformToEntityEventGroups() map[string][]string {
result := make(map[string][]string)
for key1, val1 := range *handlersMap {
if _, hasKey := result[key1]; !hasKey {
result[key1] = []string{}
}
for key2 := range val1 {
result[key1] = append(result[key1], key2)
}
}
return result
}