lua-users home
lua-l archive

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


Hi all,

I'm very pleasure to announce that a new protobuf
serailizer/deserializer module is avaliable in Lua. it's here:
http://github.com/starwing/lua-protobuf

I have used cloudwu(云风)'s pbc before, but the extension support
confuse me. I just want to use a very simple module, with one dll,
plus some lua files to do the work.

So I start to write lua-protobuf. The first version meets a diffcult:
I must implement several hash tables in C, so I have a idea to just
make a low level interface in C, and use Lua module to handle complex
high level logic. That's lua-protobuf.

You can use pb.dll/so to get the low level interface, it consist with
several sub modules:
  - pb.buffer: a buffer to encode basic values into protobuf wire format.
  - pb.decoder: a decoder to get Lua values from protobuf data.
  - pb.conv: a small module to do the conversion work between integers
and floats.
  - pb.io: a module helps to read/write binary data from stdin/stdout.

to use pb.buffer and pb.decoder together, lua-protobuf support
streaming read of protobuf data. pb.io module enable the ability to
write protoc plugins with lua-protobuf, there is a simple examples in
source repo.

above that, pb.lua supports a high level interface. it first read a
.pb file from protoc: the google's protobuf compiler, then it convert
it into a Lua table. this table used by pb.decode/pb.encode to handle
complex, nested data. In fact, the first version of this table is hand
write, but after that, I use pb.dump() to make a generated version,
makes lua-protobuf bootstrap itself now.

So you see, it can pb.load()/loadfile()/loadproto() to get a internal
Lua table to handle encode/decode work, you can also dump() it as a
normal Lua module to avoid generate it each time. you can even merge()
several tables into lua-protobuf's internal database to handle data
from different protobuf package.

Now the function is complete, so I decide to make others know it's
usable. to use it, just use your favourite compiler build pb.c into
pb.dll/pb.so, and copy pb.lua, pb_typeinfo.lua to some folder that
your Lua can find, or, simply use luarocks:

luarocks install lua-protobuf

the documentation are imcomplete, but it's very simple to use it.

this is a small example to use it:

local pb = require "pb"
pb.loadfile "addressbook.pb"

addressbook = {
    name = "Alice",
    id = 12345,
    phone = {
        { number = "1301234567" },
        { number = "87654321", type = "WORK" },
    }
}

local Person = pb.type "tutorial.Person"
code = pb.encode(addressbook, Person)
-- or just
-- code = pb.encode(addressbook, "tutorial.Person")

local decoded = protobuf.decode(code, Person) -- Person can be string, also
assert(decoded.name == "Alice")
assert(decoded.phone[1].number == "1301234567")


-- 
regards,
Xavier Wang.