cloudamqp / amq-protocol.cr

An AMQP 0.9.1 serialization library for Crystal
MIT License
10 stars 2 forks source link

no overload matches 'AMQ::Protocol::Table#[]' with type Int32 #4

Closed Blacksmoke16 closed 4 years ago

Blacksmoke16 commented 4 years ago

I'm currently working on a project that involves using dead letter queues. As such I need to be able to access the x-death header of a message to know how many times it has failed in order to implement a retry limit.

I'm currenting doing:

headers = msg.properties.headers.not_nil!
pp headers["x-death"][0]

However this fails to compile with the error no overload matches 'AMQ::Protocol::Table#[]' with type Int32

I think this is because headers is implemented as Table, which overrides #[], but only accepts String keys. Table type just needs to be updated in order to allow accessing arrays in addition to singular header values.

EDIT: Work around would be to do like pp headers["x-death"].as(Array)[0].as(AMQ::Protocol::Table)["count"].

carlhoerberg commented 4 years ago

Oh, totally missed this!

But your workaround is the correct way, a Table can't have Integers as keys, only short strings as per protocol. And at compile time we only know that headers["x-death"] will return an AMQ::Protocol::Field, which is an alias for among other types; Table, and that's why you get the error. I'm afraid type casting is required.