-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
249 lines (213 loc) · 4.23 KB
/
options.go
File metadata and controls
249 lines (213 loc) · 4.23 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package rabbitmq
import (
amqp "github.com/rabbitmq/amqp091-go"
)
const (
DefaultExchangeKind = "topic"
DefaultQueueBindKey = "*"
)
var (
DefaultEncoder = NewJsonEncoder()
DefaultExchangeOpts = &ExchangeOpts{
Durable: true,
AutoDelete: false,
Internal: false,
NoWait: false,
}
DefaultQueueOpts = &QueueOpts{
Durable: true,
AutoDelete: false,
Exclusive: false,
NoWait: false,
}
DefaultConsumerOpts = &ConsumerOpts{
AutoAck: false,
Exclusive: false,
NoLocal: false,
NoWait: false,
}
DefaultPublisherOpts = &PublisherOpts{
Mandatory: false,
Immediate: false,
}
)
type ExchangeOpts struct {
Durable bool
AutoDelete bool
Internal bool
NoWait bool
}
type QueueOpts struct {
Durable bool
AutoDelete bool
Exclusive bool
NoWait bool
}
type ConsumerOpts struct {
AutoAck bool
Exclusive bool
NoLocal bool
NoWait bool
}
type PublisherOpts struct {
Mandatory bool
Immediate bool
}
type Options struct {
dsn string
topic string
encoder MessageEncoder
queue *QueueOptions
exchange *ExchangeOptions
queueBind *QueueBindOptions
consumer *ConsumerOptions
publisher *PublisherOptions
}
type Option func(*Options)
type QueueOptions struct {
Name string
Opts *QueueOpts
Args amqp.Table
}
type ExchangeOptions struct {
Name string
Kind string
Opts *ExchangeOpts
Args amqp.Table
}
type QueueBindOptions struct {
Key string
NoWait bool
Args amqp.Table
}
type ConsumerOptions struct {
Opts *ConsumerOpts
Args amqp.Table
}
type PublisherOptions struct {
Opts *PublisherOpts
}
func DSN(val string) Option {
return func(opts *Options) {
opts.dsn = val
}
}
func Topic(val string) Option {
return func(opts *Options) {
opts.topic = val
}
}
func MsgEncoder(val MessageEncoder) Option {
return func(opts *Options) {
opts.encoder = val
}
}
func Queue(val *QueueOptions) Option {
return func(opts *Options) {
opts.queue = val
}
}
func Exchange(val *ExchangeOptions) Option {
return func(opts *Options) {
opts.exchange = val
}
}
func QueueBind(val *QueueBindOptions) Option {
return func(opts *Options) {
opts.queueBind = val
}
}
func Consumer(val *ConsumerOptions) Option {
return func(opts *Options) {
opts.consumer = val
}
}
func Publisher(val *PublisherOptions) Option {
return func(opts *Options) {
opts.publisher = val
}
}
func (m *Options) GetMessageEncoder() MessageEncoder {
if m.encoder == nil {
return DefaultEncoder
}
return m.encoder
}
func (m *Options) GetQueueName() string {
if m.queue == nil || m.queue.Name == "" {
return m.GetExchangeName() + ".queue"
}
return m.queue.Name
}
func (m *Options) GetExchangeName() string {
if m.exchange == nil || m.exchange.Name == "" {
return m.topic
}
return m.exchange.Name
}
func (m *Options) GetExchangeKind() string {
if m.exchange == nil || m.exchange.Kind == "" {
return DefaultExchangeKind
}
return m.exchange.Kind
}
func (m *Options) GetQueueBindKey() string {
if m.queueBind == nil || m.queueBind.Key == "" {
return DefaultQueueBindKey
}
return m.queueBind.Key
}
func (m *Options) GetQueueOpts() *QueueOpts {
if m.queue == nil || m.queue.Opts == nil {
return DefaultQueueOpts
}
return m.queue.Opts
}
func (m *Options) GetExchangeOpts() *ExchangeOpts {
if m.exchange == nil || m.exchange.Opts == nil {
return DefaultExchangeOpts
}
return m.exchange.Opts
}
func (m *Options) GetConsumerOpts() *ConsumerOpts {
if m.consumer == nil || m.consumer.Opts == nil {
return DefaultConsumerOpts
}
return m.consumer.Opts
}
func (m *Options) GetPublisherOpts() *PublisherOpts {
if m.publisher == nil || m.publisher.Opts == nil {
return DefaultPublisherOpts
}
return m.publisher.Opts
}
func (m *Options) GetQueueArgs() amqp.Table {
if m.queue == nil {
return nil
}
return m.queue.Args
}
func (m *Options) GetExchangeArgs() amqp.Table {
if m.exchange == nil {
return nil
}
return m.exchange.Args
}
func (m *Options) GetQueueBindArgs() amqp.Table {
if m.queueBind == nil {
return nil
}
return m.queueBind.Args
}
func (m *Options) GetQueueBindNoWait() bool {
if m.queueBind == nil {
return false
}
return m.queueBind.NoWait
}
func (m *Options) GetConsumerArgs() amqp.Table {
if m.consumer == nil {
return nil
}
return m.consumer.Args
}