-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
234 lines (197 loc) · 6.4 KB
/
message.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
package saga
import (
"time"
)
type MessageConstructor[S Session, M Message] func(S) M
func convertMessage[S Session, M Message](constructor MessageConstructor[S, M]) MessageConstructor[Session, Message] {
return func(s Session) Message {
return constructor(s.(S))
}
}
// Message is a value object.
type Message interface {
ID() string
SessionID() string
Trigger() string
CreatedAt() time.Time
}
func NewAbstractMessage(id, sessionID, trigger string) AbstractMessage {
return AbstractMessage{
id: id,
sessionID: sessionID,
trigger: trigger,
createdAt: time.Now(),
}
}
func NewAbstractMessageWithTime(id, sessionID, trigger string, createdAt time.Time) AbstractMessage {
return AbstractMessage{
id: id,
sessionID: sessionID,
trigger: trigger,
createdAt: createdAt,
}
}
// AbstractMessage is a value object that represents a message.
// It contains the common fields of a message.
// If you want to create a new message, you should embed this struct.
type AbstractMessage struct {
id string
sessionID string
trigger string
createdAt time.Time
}
func (m AbstractMessage) ID() string {
return m.id
}
func (m AbstractMessage) SessionID() string {
return m.sessionID
}
func (m AbstractMessage) Trigger() string {
return m.trigger
}
func (m AbstractMessage) CreatedAt() time.Time {
return m.createdAt
}
func ConvertMessageRepository[M Message, Tx TxContext](repository AbstractMessageRepository[M, Tx]) AbstractMessageRepository[Message, Tx] {
getMessagesFromOutbox := func(batchSize int) ([]Message, error) {
ms, err := repository.GetMessagesFromOutbox(batchSize)
if err != nil {
return nil, err
}
var messages []Message
for _, m := range ms {
messages = append(messages, m)
}
return messages, nil
}
getMessagesFromDeadLetter := func(batchSize int) ([]Message, error) {
ms, err := repository.GetMessagesFromDeadLetter(batchSize)
if err != nil {
return nil, err
}
var messages []Message
for _, m := range ms {
messages = append(messages, m)
}
return messages, nil
}
saveMessages := func(messages []Message) Executable[Tx] {
var ms []M
for _, m := range messages {
ms = append(ms, m.(M))
}
return repository.SaveMessages(ms)
}
saveDeadLetters := func(messages []Message) Executable[Tx] {
var ms []M
for _, m := range messages {
ms = append(ms, m.(M))
}
return repository.SaveDeadLetters(ms)
}
deleteMessages := func(messages []Message) Executable[Tx] {
var ms []M
for _, m := range messages {
ms = append(ms, m.(M))
}
return repository.DeleteMessages(ms)
}
deleteDeadLetters := func(messages []Message) Executable[Tx] {
var ms []M
for _, m := range messages {
ms = append(ms, m.(M))
}
return repository.DeleteDeadLetters(ms)
}
return messageRepository[Tx]{
saveMessage: func(m Message) Executable[Tx] { return repository.SaveMessage(m.(M)) },
saveMessages: saveMessages,
saveDeadLetter: func(m Message) Executable[Tx] { return repository.SaveDeadLetter(m.(M)) },
saveDeadLetters: saveDeadLetters,
deleteMessage: func(m Message) Executable[Tx] { return repository.DeleteMessage(m.(M)) },
deleteMessages: deleteMessages,
deleteDeadLetter: func(m Message) Executable[Tx] { return repository.DeleteDeadLetter(m.(M)) },
deleteDeadLetters: deleteDeadLetters,
getMessagesFromOutbox: getMessagesFromOutbox,
getMessagesFromDeadLetter: getMessagesFromDeadLetter,
}
}
type AbstractMessageRepository[M Message, Tx TxContext] interface {
AbstractMessageLoadRepository[M]
SaveMessage(message M) Executable[Tx]
SaveMessages(messages []M) Executable[Tx]
SaveDeadLetter(message M) Executable[Tx]
SaveDeadLetters(message []M) Executable[Tx]
DeleteMessage(message M) Executable[Tx]
DeleteMessages(messages []M) Executable[Tx]
DeleteDeadLetter(message M) Executable[Tx]
DeleteDeadLetters(messages []M) Executable[Tx]
}
type AbstractMessageLoadRepository[M Message] interface {
GetMessagesFromOutbox(batchSize int) ([]M, error)
GetMessagesFromDeadLetter(batchSize int) ([]M, error)
}
type messageRepository[Tx TxContext] struct {
saveMessage func(Message) Executable[Tx]
saveMessages func([]Message) Executable[Tx]
saveDeadLetter func(Message) Executable[Tx]
saveDeadLetters func([]Message) Executable[Tx]
deleteMessage func(Message) Executable[Tx]
deleteMessages func([]Message) Executable[Tx]
deleteDeadLetter func(Message) Executable[Tx]
deleteDeadLetters func([]Message) Executable[Tx]
getMessagesFromOutbox func(int) ([]Message, error)
getMessagesFromDeadLetter func(int) ([]Message, error)
}
func (r messageRepository[Tx]) SaveMessage(message Message) Executable[Tx] {
return r.saveMessage(message)
}
func (r messageRepository[Tx]) SaveMessages(messages []Message) Executable[Tx] {
return r.saveMessages(messages)
}
func (r messageRepository[Tx]) SaveDeadLetter(message Message) Executable[Tx] {
return r.saveDeadLetter(message)
}
func (r messageRepository[Tx]) SaveDeadLetters(messages []Message) Executable[Tx] {
return r.saveDeadLetters(messages)
}
func (r messageRepository[Tx]) DeleteMessage(message Message) Executable[Tx] {
return r.deleteMessage(message)
}
func (r messageRepository[Tx]) DeleteMessages(messages []Message) Executable[Tx] {
return r.deleteMessages(messages)
}
func (r messageRepository[Tx]) DeleteDeadLetter(message Message) Executable[Tx] {
return r.deleteDeadLetter(message)
}
func (r messageRepository[Tx]) DeleteDeadLetters(messages []Message) Executable[Tx] {
return r.deleteDeadLetters(messages)
}
func (r messageRepository[Tx]) GetMessagesFromOutbox(batchSize int) ([]Message, error) {
return r.getMessagesFromOutbox(batchSize)
}
func (r messageRepository[Tx]) GetMessagesFromDeadLetter(batchSize int) ([]Message, error) {
return r.getMessagesFromDeadLetter(batchSize)
}
// messagePacket is a value object that represents a AbstractMessage packet.
// The packet contains the Message and the origin channel of the AbstractMessage.
type messagePacket struct {
origin ChannelName
Message
}
func (m messagePacket) Origin() ChannelName {
return m.origin
}
func (m messagePacket) Payload() Message {
return m.Message
}
func newMessagePacket(origin ChannelName, message Message) messagePacket {
return messagePacket{
Message: message,
origin: origin,
}
}
type Command struct {
}
type Response struct {
}