Skip to content

Commit 1c0f9fa

Browse files
committed
For bindings, use strings for source and destination names.
1 parent db65a92 commit 1c0f9fa

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

rabbitmq_amqp/amqp_binding.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ type AMQPBindingInfo struct {
99
}
1010

1111
type AMQPBinding struct {
12-
sourceExchange IExchangeSpecification
13-
destinationQueue IQueueSpecification
14-
bindingKey string
15-
management *AmqpManagement
12+
sourceName string
13+
destinationName string
14+
toQueue bool
15+
bindingKey string
16+
management *AmqpManagement
1617
}
1718

1819
func newAMQPBinding(management *AmqpManagement) *AMQPBinding {
@@ -25,28 +26,54 @@ func (b *AMQPBinding) Key(bindingKey string) IBindingSpecification {
2526
}
2627

2728
func (b *AMQPBinding) SourceExchange(exchangeSpec IExchangeSpecification) IBindingSpecification {
28-
b.sourceExchange = exchangeSpec
29+
b.sourceName = exchangeSpec.GetName()
30+
b.toQueue = false
31+
return b
32+
}
33+
34+
func (b *AMQPBinding) SourceExchangeName(exchangeName string) IBindingSpecification {
35+
b.sourceName = exchangeName
36+
b.toQueue = false
37+
return b
38+
}
39+
40+
func (b *AMQPBinding) DestinationExchange(exchangeSpec IExchangeInfo) IBindingSpecification {
41+
b.destinationName = exchangeSpec.GetName()
42+
b.toQueue = false
43+
return b
44+
}
45+
46+
func (b *AMQPBinding) DestinationExchangeName(exchangeName string) IBindingSpecification {
47+
b.destinationName = exchangeName
48+
b.toQueue = false
2949
return b
3050
}
3151

3252
func (b *AMQPBinding) DestinationQueue(queueSpec IQueueSpecification) IBindingSpecification {
33-
b.destinationQueue = queueSpec
53+
b.destinationName = queueSpec.GetName()
54+
b.toQueue = true
55+
return b
56+
}
57+
58+
func (b *AMQPBinding) DestinationQueueName(queueName string) IBindingSpecification {
59+
b.destinationName = queueName
60+
b.toQueue = true
3461
return b
3562
}
3663

3764
func (b *AMQPBinding) Bind(ctx context.Context) error {
3865
path := bindingPath()
3966
kv := make(map[string]any)
4067
kv["binding_key"] = b.bindingKey
41-
kv["source"] = b.sourceExchange.GetName()
42-
kv["destination_queue"] = b.destinationQueue.GetName()
68+
kv["source"] = b.sourceName
69+
kv["destination_queue"] = b.destinationName
4370
kv["arguments"] = make(map[string]any)
4471
_, err := b.management.Request(ctx, kv, path, commandPost, []int{responseCode204})
4572
return err
4673
}
4774

4875
func (b *AMQPBinding) Unbind(ctx context.Context) error {
49-
bindingPathWithExchangeQueueKey := bindingPathWithExchangeQueueKey(b.sourceExchange, b.destinationQueue, b.bindingKey)
76+
bindingPathWithExchangeQueueKey := bindingPathWithExchangeQueueKey(b.toQueue, b.sourceName, b.destinationName, b.bindingKey)
5077
_, err := b.management.Request(ctx, amqp.Null{}, bindingPathWithExchangeQueueKey, commandDelete, []int{responseCode204})
5178
return err
5279
}

rabbitmq_amqp/common.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ func bindingPath() string {
8686
return "/" + Bindings
8787
}
8888

89-
func bindingPathWithExchangeQueueKey(exchangeSpec IExchangeSpecification, queueSpec IQueueSpecification, key string) string {
90-
exchangeName := exchangeSpec.GetName()
91-
exchangeNameEncoded := encodePathSegments(exchangeName)
92-
queueName := queueSpec.GetName()
93-
queueNameEncoded := encodePathSegments(queueName)
89+
func bindingPathWithExchangeQueueKey(toQueue bool, sourceName, destinationName, key string) string {
90+
sourceNameEncoded := encodePathSegments(sourceName)
91+
destinationNameEncoded := encodePathSegments(destinationName)
9492
keyEncoded := encodePathSegments(key)
95-
format := "/%s/src=%s;dstq=%s;key=%s;args="
96-
return fmt.Sprintf(format, Bindings, exchangeNameEncoded, queueNameEncoded, keyEncoded)
93+
destinationType := "dste"
94+
if toQueue {
95+
destinationType = "dstq"
96+
}
97+
format := "/%s/src=%s;%s=%s;key=%s;args="
98+
return fmt.Sprintf(format, Bindings, sourceNameEncoded, destinationType, destinationNameEncoded, keyEncoded)
9799

98100
}
99101

rabbitmq_amqp/entities.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ type IExchangeSpecification interface {
8787

8888
type IBindingSpecification interface {
8989
SourceExchange(exchangeSpec IExchangeSpecification) IBindingSpecification
90+
SourceExchangeName(exchangeName string) IBindingSpecification
9091
DestinationQueue(queueSpec IQueueSpecification) IBindingSpecification
92+
DestinationQueueName(queueName string) IBindingSpecification
9193
Key(bindingKey string) IBindingSpecification
9294
Bind(ctx context.Context) error
9395
Unbind(ctx context.Context) error

0 commit comments

Comments
 (0)