meta data for this page
This is an old revision of the document!
Protocol
- Protocol Buffer Polymorphism http://www.indelible.org/ink/protobuf-polymorphism/
tools
Libraries
sean-lin based
https://github.com/sean-lin/protoc-gen-lua
- Latest commit 12 Jun 2014
https://github.com/djungelorm/protobuf-lua
- Latest commit 26 Jan 2016
- Prerequisities
sudo apt-get install python-protobuf
- protoc plugin used:
/usr/local/bin/protoc-gen-lua
which is symlinked to/usr/local/lib/luarocks/rocks/protobuf/1.1.1-0/protoc-plugin/protoc-gen-lua
https://github.com/urbanairship/protobuf-lua
- Latest commit 13 Nov 2015
- fork of above https://github.com/djungelorm/protobuf-lua
- Lua 5.2 support
- .proto compilation required. Contains protoc plugin for lua
others
https://github.com/indygreg/lua-protobuf
- Last commit 27 Mar 2011
https://github.com/haberman/upb
- Last commit 8 Jul 2015
- Right now the Lua bindings support:
- Building schema objects manually (eg. you can essentially write .proto files natively in Lua).
- creating message objects.
- parsing Protocol Buffers into message objects.
Neopalium
https://github.com/Neopallium/lua-pb
- Latest commit 18 Feb 2016
- Own LUA parser for .proto files (no need to compile)
Library API
-- ------------------------------------------- -- @return string with message dump local function dumpRawProtoc (binMsg) local p = io.popen ("protoc --decode_raw", "w") local stdout if p then p:write(binMsg) local stdout = p:read("*a") p:close() end return stdout end
Neopalium
local pb = require 'pb' -- loading 'pb' module will replace 'require' function -- now .proto files are automatically loaded by require. local proto = require 'protos.messages' -- load 'protos/messages.proto' -- Two methods of message creation -- 1) direct assigne local protoMsg = proto.Message() protoMsg.deviceId = 0x1234 -- simple type value protoMsg.deviceType = 'MODEM' -- enum value protoMsg.repeatedSubMessage = { { id = 'TEMPERATURE', value = 34 }, { id = 'WIND', value = 2 } } protoMsg.subMessage.serialNumber = '123456-1234' protoMsg.subMessage.version = '2' -- 2) Initialize from structure local luaData = { deviceId = 0x1234, deviceType = 'MODEM', repeatedSubMessage = { { id = 'TEMPERATURE', value = 34 }, { id = 'WIND', value = 2 } }, subMessage = { serialNumber = '123456-1234', version = '2' } } local protoMsg = proto.Message(luaData) -- initialize from luaData -- 3) Still possible to directly modify, add values into message protoMsg.deviceId = 0xdeadbeef local binProtoMsg = protoMsg:Serialize() -- binProtoMsg is string print (binProtoMsh:len()) local dumpProtoMsg = protoMsg:SerializePartial('text') local receivedProtoMsg = proto.Message() if receivedProtoMsh == nil then error ("Malformed message") end receivedProtoMsg:Parse(binProtoMsg) print (receivedProtoMsg:SerializePartial('text'))
Raw dump of protobuf message (.proto files not needed)
local function dump_fields(unknown) local o = "" for i, v in ipairs(unknown) do o = o..string.format("#%02d Tag=[%2d] Wire=[%2d] %q\n", i, v.tag, v.wire, tostring(v.value)) if type (v.value) == 'table' then o = o..dump_fields(v.value) end end return o end -- ------------------------------------------- -- @return string with message dump function PD:pbDumpRawPB(binMsg) local msg, off = pb.decode_raw(binMsg) return dump_fields(msg.unknown_fields) end