lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, 29 Aug 2023 at 23:34, Sean Conner <sean@conman.org> wrote:
  Another approach that could possibly work would to be wrap a binary string
in a table, with a __tocbor() method to force encoding the string as a CBOR
BIN type.

Hi Sean,

yes I know about the __tocbor metamethod, and I know that there are also other alternatives with the org.conman.cbor library, but none has "zero impact".

The simplest proof-of-concept using __tocbor:

function b(data)
    local d={data="">    setmetatable(d,{__tocbor=function(self) return cbor.TYPE.BIN(self.data) end})
    return d
end

cbor.encode{string1='abc', buffer1=b'abc'}

works, but I have to transform fields in the table via the 'b' function.
I was looking for something that can mark buffers or strings "natively" in Lua (userdata? I'm a bit of a Lua newb), e.g. in pseudocode:

tbl={}
tbl.string1='abc'
tbl.buffer1='abc'
settypehint(tbl.buffer1,'bytes')
cbor.encode(tbl) -- somehow reads the type hint above

so that I can keep the original table content unchanged.
Is there anything closer to that hypothetical settypehint() function in the example above?

Cheers,
Federico Ferri