• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jinq0123/LuaPbIntf: Binding Protobuf 3 to Lua 5.3

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

jinq0123/LuaPbIntf

开源软件地址(OpenSource Url):

https://github.com/jinq0123/LuaPbIntf

开源编程语言(OpenSource Language):

C++ 64.5%

开源软件介绍(OpenSource Introduction):

LuaPbIntf

Lua Protobuf interface.

Binding Protobuf to Lua with lua-intf, supporting lua53 and proto3.

LuaPbIntf is inspired by luapb, but has been rewritten to take advantage of lua-intf, which makes the binding easier to use and easier to understand.

LuaPbIntf will dynamic load Protocol Buffer message definition files .proto without code generation.

LuaPbIntf use lua table to present Message, encode() to encode lua message table to string, and decode() to decode string to lua message table.

Differ from luapb

  • Support proto3
  • Support service
  • Able to set the proto path
  • luapb skips some types such as: TYPE_SFIXED32. See issue.

Build

Build with conan

  1. Install conan.
  2. Add conan repositories
    • conan remote add remote_bintray_bincrafters https://api.bintray.com/conan/bincrafters/public-conan
    • conan remote add remote_bintray_jinq0123 https://api.bintray.com/conan/jinq0123/test
  3. conan create . user/channel --build missing
    • The result luapbintf.dll/luapbintf.so is in ~/.conan/data/luapbintf/0.1/user/channel/package/...
    • Add build settings like: conan create user/channel --build missing -s arch=x86

Without conan (depricated)

Init third_party dir first, see third_party/README.md. Then use premake5 to generate VS2015 sln or Makefile to build. See premake/premake5.bat

On Windows, it is recommended by google that you use static protobuf only. On Linux, you may need to recompile libprotobuf.a with -fPIC, or link libprotobuf.so.

Link error

error LNK2019: unresolved external symbol "void __cdecl lua_settop(struct lua_State *,int)" (?lua_settop@@YAXPAUlua_State@@H@Z)

Please see issue #2.

By default, it expects the Lua library to build under C++. If you really want to use Lua library compiled under C, you can define LUAINTF_LINK_LUA_COMPILED_IN_CXX to 0.

Run test

See test/README.md

Example

Encode and Decode

local pb = require("luapbintf")

pb.import_proto_file("test.proto")

local msg = { uid = 12345 }
local sz = pb.encode("test.TestMsg", msg)

local msg2 = pb.decode("test.TestMsg", sz)
assert(msg2.uid == 12345)

Rpc

    assert(pb.get_rpc_input_name("test.Test", "Foo") == "test.TestMsg")
    assert(pb.get_rpc_output_name("test.Test", "Foo") == "test.CommonMsg")

Map field

local msgs = {}
msgs["k1"] = {}
msgs["k2"] = {}
pb.encode("test.TestMsg", { msgs = msgs })

Oneof field

    local msg = { msg2 = {} }
    local s = pb.encode("test.TestMsg", msg)
    local result = pb.decode("test.TestMsg", s)
    assert(result.name == "")
    assert(result.name2 == nil)
    assert(result.msg2)

Type Convertion

Number will be converted to string if necessary.

    local msg = { name = 12345 }
    local s = pb.encode("test.TestMsg", msg)
    assert(pb.decode("test.TestMsg", s).name == "12345")

Integer will be converted from one type to another. Such as from int64 4294967296 to int32 0:

    local msg = { n32 = 4294967296 + 123 }
    assert(msg.n32 == 4294967296 + 123)
    local s = pb.encode("test.TestMsg", msg)
    local msg2 = pb.decode("test.TestMsg", s)
    assert(msg2.n32 == 123)

String can be converted to integer or float:

    local msg = { d = "12345e-67" }
    local s = pb.encode("test.TestMsg", msg)
    assert(pb.decode("test.TestMsg", s).d == 12345e-67)

Can not convert float to integer.

> msg = { n32 = 1.1 }
> pb.encode("test.TestMsg", msg)
stdin:1: bad argument #-1 to 'encode' (number has no integer representation)

Enum is integer, but input string enum will be converted to integer.

    local msg = { cmd = "CMD_TYPE_CHECK" }
    local s = pb.encode("test.TestMsg", msg)
    assert(pb.decode("test.TestMsg", s).cmd == 2)

Repeated field is a lua array table and it is read from index 1 to len, ignoring others.

    local msg = { names = {"n1", "n2", "n3", [0] = "n1", [100] = "n100"} }
    local s = pb.encode("test.TestMsg", msg)
    local msg2 = pb.decode("test.TestMsg", s)
    assert(#msg2.names == 3)

Proto2 Extension

Proto2 extension is not supported, which is replaced by Any type in proto3.

Reference

See doc/reference.md.

Known Bugs




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap