-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_bus.go
131 lines (100 loc) · 2.93 KB
/
command_bus.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
package cqrs
//go:generate mockgen -destination=./mocks/mock_$GOFILE -source=$GOFILE -package=mock_$GOPACKAGE
import (
"context"
"reflect"
"sync"
)
type CommandBus interface {
Use(middlewares ...CommandMiddlewareFunc)
Register(handler interface{}) error
Execute(ctx context.Context, command interface{}) error
}
func NewCommandBus() CommandBus {
return &commandBus{
middlewares: make([]CommandMiddlewareFunc, 0),
handlers: make(map[reflect.Type]CommandHandlerFunc[any]),
}
}
type commandBus struct {
middlewares []CommandMiddlewareFunc
handlers map[reflect.Type]CommandHandlerFunc[any]
mu sync.RWMutex
}
func (c *commandBus) validate(handler interface{}) error {
handlerVal := reflect.ValueOf(handler)
if handlerVal.Kind() != reflect.Func || handlerVal.IsNil() {
return ErrHandlerMustBeNonNilFunction
}
if handlerVal.Type().NumIn() != 2 {
return ErrHandlerMustHaveExactTwoArguments
}
firstArgType := handlerVal.Type().In(0)
secondArgType := handlerVal.Type().In(1)
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
if firstArgType != contextType {
return ErrFirstArgumentOfHandlerMustBeContext
}
if (secondArgType.Kind() != reflect.Pointer || secondArgType.Elem().Kind() != reflect.Struct) && secondArgType.Kind() != reflect.Struct {
return ErrSecondArgumentOfHandlerMustBeStructOrPointerOfStruct
}
if handlerVal.Type().NumOut() != 1 {
return ErrHandlerMustHaveExactOneResult
}
firstResultType := handlerVal.Type().Out(0)
errorType := reflect.TypeOf((*error)(nil)).Elem()
if firstResultType != errorType {
return ErrHandlerResultMustBeError
}
_, ok := c.handlers[secondArgType]
if ok {
return ErrCommandAlreadyRegistered
}
return nil
}
func (c *commandBus) wrapHandler(handler interface{}) CommandHandlerFunc[any] {
handlerVal := reflect.ValueOf(handler)
return func(ctx context.Context, command interface{}) error {
args := []reflect.Value{
reflect.ValueOf(ctx),
reflect.ValueOf(command),
}
results := handlerVal.Call(args)
if !results[0].IsNil() {
return results[0].Interface().(error)
}
return nil
}
}
func (c *commandBus) Use(middlewares ...CommandMiddlewareFunc) {
c.mu.Lock()
defer c.mu.Unlock()
c.middlewares = append(c.middlewares, middlewares...)
}
func (c *commandBus) Register(handler interface{}) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := c.validate(handler); err != nil {
return err
}
handlerType := reflect.TypeOf(handler)
commandType := handlerType.In(1)
c.handlers[commandType] = c.wrapHandler(handler)
return nil
}
func (c *commandBus) Execute(ctx context.Context, command interface{}) error {
c.mu.RLock()
defer c.mu.RUnlock()
commandType := reflect.TypeOf(command)
handler, ok := c.handlers[commandType]
if !ok {
return ErrCommandHasNotRegisteredYet
}
for i := len(c.middlewares) - 1; i >= 0; i-- {
handler = c.middlewares[i](handler)
}
if err := handler(ctx, command); err != nil {
return err
}
return nil
}