From 930abe9fb75e8a8e46d0ddcce94d705864e76699 Mon Sep 17 00:00:00 2001 From: Michal Kuratczyk Date: Thu, 16 Sep 2021 12:58:58 +0200 Subject: [PATCH] Handle "undefined" as an auto_delete value --- common.go | 21 +++++++++++++++++++++ exchanges.go | 2 +- queues.go | 2 +- rabbithole_test.go | 6 +++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/common.go b/common.go index 8c767b7..47ef868 100644 --- a/common.go +++ b/common.go @@ -2,6 +2,7 @@ package rabbithole import ( "encoding/json" + "errors" "strconv" ) @@ -99,3 +100,23 @@ func (s *URISet) UnmarshalJSON(b []byte) error { *s = uris return nil } + +// AutoDelete is a boolean but RabbitMQ may return the string "undefined" +type AutoDelete bool + +// UnmarshalJSON can unmarshal a string or a boolean +func (d *AutoDelete) UnmarshalJSON(b []byte) error { + switch string(b) { + case "\"undefined\"": + // auto_delete is "undefined", map it to true + *d = AutoDelete(true) + case "true": + *d = AutoDelete(true) + case "false": + *d = AutoDelete(false) + default: + return errors.New("Unknown value of auto_delete") + } + + return nil +} diff --git a/exchanges.go b/exchanges.go index 7af8cb8..cc9a372 100644 --- a/exchanges.go +++ b/exchanges.go @@ -25,7 +25,7 @@ type ExchangeInfo struct { Vhost string `json:"vhost"` Type string `json:"type"` Durable bool `json:"durable"` - AutoDelete bool `json:"auto_delete"` + AutoDelete AutoDelete `json:"auto_delete"` Internal bool `json:"internal"` Arguments map[string]interface{} `json:"arguments"` diff --git a/queues.go b/queues.go index 47a82c1..c423986 100644 --- a/queues.go +++ b/queues.go @@ -94,7 +94,7 @@ type QueueInfo struct { // Is this queue durable? Durable bool `json:"durable"` // Is this queue auto-deleted? - AutoDelete bool `json:"auto_delete"` + AutoDelete AutoDelete `json:"auto_delete"` // Is this queue exclusive? Exclusive bool `json:"exclusive"` // Extra queue arguments diff --git a/rabbithole_test.go b/rabbithole_test.go index d6cf840..25c6946 100644 --- a/rabbithole_test.go +++ b/rabbithole_test.go @@ -452,7 +452,7 @@ var _ = Describe("RabbitMQ HTTP API client", func() { Ω(err).Should(BeNil()) Ω(x.Name).Should(Equal("amq.fanout")) Ω(x.Durable).Should(Equal(true)) - Ω(x.AutoDelete).Should(Equal(false)) + Ω(bool(x.AutoDelete)).Should(Equal(false)) Ω(x.Internal).Should(Equal(false)) Ω(x.Type).Should(Equal("fanout")) Ω(x.Vhost).Should(Equal("rabbit/hole")) @@ -1637,7 +1637,7 @@ var _ = Describe("RabbitMQ HTTP API client", func() { Ω(err).Should(BeNil()) Ω(x.Name).Should(Equal(xn)) Ω(x.Durable).Should(Equal(false)) - Ω(x.AutoDelete).Should(Equal(false)) + Ω(bool(x.AutoDelete)).Should(Equal(false)) Ω(x.Type).Should(Equal("fanout")) Ω(x.Vhost).Should(Equal(vh)) @@ -1678,7 +1678,7 @@ var _ = Describe("RabbitMQ HTTP API client", func() { Ω(err).Should(BeNil()) Ω(x.Name).Should(Equal(qn)) Ω(x.Durable).Should(Equal(true)) - Ω(x.AutoDelete).Should(Equal(false)) + Ω(bool(x.AutoDelete)).Should(Equal(false)) Ω(x.Vhost).Should(Equal(vh)) Ω(x.Arguments).To(HaveKeyWithValue("x-queue-type", "quorum"))