IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    支持 proto3 的 lua 绑定库 LuaPbIntf

    金庆发表于 2017-04-25 03:43:00
    love 0
    支持 proto3 的 lua 绑定库 LuaPbIntf

    (金庆的专栏 2017.4)

    protobuf的lua绑定库之前一直用 pbc,但是该库已经不更新了。
    想要添加 proto3 的 map , 还有支持 service, 发现代码太高深,无法下手。

    pbc因为用c代码实现了 protobuf 的功能,所以代码量很大。
    而对于 c++ 来说,只需要利用 protobuf 库中动态消息就可以实现 lua 所需功能。

    luapb 就是直接用 protobuf 库封装后导出lua实现的。该库仅支持 proto2.

    在 luapb 基础上实现对 proto3 的支持,就是 LuaPbIntf 库。
    LuaPbIntf 库利用了 lua-intf 库来实现 c++ 与 lua 绑定,所以代码量比 luapb 又少了许多,
    并且可读性大大增强,更改添加新功能也就更方便了。

    与 pbc, lubpb 一样,LuaPbIntf 也是动态加载 proto 文件,不需要代码生成。

    LuaPbIntf 采用 lua table 来表示 pb 消息,不是 pbc 中的代理表,而只是普通表。

    项目地址:https://github.com/jinq0123/LuaPbIntf

    示例:

    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)

    proto3 map 示例:

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

    rpc service 支持:

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


    金庆 2017-04-25 11:43 发表评论


沪ICP备19023445号-2号
友情链接