Skip to content

ExtensionType API incomplete #49

@nsslh

Description

@nsslh

Is the API for supporting MsgPack extensions complete?

(So as not to waste anyone's time, note that this is an adaptation of a question on Discourse.)

I am trying to write the Julia end of an RPC-over-RabbitMQ relationship. The other side (written in Ruby) uses a MsgPack extension for dates. I receive this:

MsgPack.Extension(1, UInt8[0x07, 0xe6, 0x02, 0x1c])

I can make my end send this with pack, but I can’t make my end convert this to a Date with unpack. This is what I have so far, based on the documentation for MsgPack.ExtensionType:

module test_msg_pack_ext

using Dates
using MsgPack
using Test

function pack_date(date::Date)::MsgPack.Extension
        year_u16 = convert(UInt16, year(date))
        year_hi = UInt8(year_u16 >> 8)
        year_lo = UInt8(year_u16 << 8 >> 8)
        return MsgPack.Extension(0x01, UInt8[year_hi, year_lo, month(date), day(date)])
end

function unpack_date(ext::MsgPack.Extension)::Date
        year = ext.data[1] << 8 | ext.data[2]
        return Date(year, ext.data[3], ext.data[4])
end

MsgPack.msgpack_type(::Type{Dates.Date}) = MsgPack.ExtensionType()
MsgPack.to_msgpack(::MsgPack.ExtensionType, date::Dates.Date) = pack_date(date)
MsgPack.from_msgpack(::Type{Dates.Date}, ext::MsgPack.Extension) = unpack_date(ext)

@test pack(MsgPack.Extension(1, UInt8[0x07, 0xe6, 0x02, 0x1c])) == pack(Date(2022, 2, 28))
@test Date(2022, 2, 28) == unpack(UInt8[0xd6, 0x01, 0x07, 0xe6, 0x02, 0x1c])
    
end

# =>
engine/msg_pack_ext.jl: Test Failed at .../msg_pack_ext.jl:25
  Expression: Date(2022, 2, 28) == unpack(UInt8[0xd6, 0x01, 0x07, 0xe6, 0x02, 0x1c])
   Evaluated: Dates.Date("2022-02-28") == MsgPack.Extension(1, UInt8[0x07, 0xe6, 0x02, 0x1c])

That makes sense to me. I haven’t done anything to map extension type 1 to my code. But I can't see anything in the MsgPack code or test suite that lets me do this. The discussion in #7 makes it look like the contributor was satisfied with receiving a MsgPack.Extension and handling it themselves?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions