protoc-gen-lua中使用 Repeated 标签

来源:互联网 发布:淘宝联盟5.0apk 编辑:程序博客网 时间:2024/04/28 16:12

比如我这里创建一个 Student.proto

    --Student.proto         message Info    {    required string name=1;    }         message Student    {    required int32 count=1;    repeated Info infos=2;    }

在C# 里面 Repeated 转出来是对应 List 。
在Lua 里面 Repeated 转出来是对应 Table 的。
然后像下面这样使用

编写测试脚本 StudentTest.lua

    --StudentTest.lua         package.path = package.path .. ';./protobuf/?.lua;./protobuf/luascript/?.lua'         require("Student_pb")         local msg=Student_pb.Student()    msg.count=2         local info=Student_pb.Info()    info.name="chenpeng"         table.insert(msg.infos,info)         local info1=Student_pb.Info()    info1.name="chenpeng1"    table.insert(msg.infos,info1)         print("msg.infos count=" .. table.maxn(msg.infos))         print("---------------------------")    for i=1,table.maxn(msg.infos) do    print(msg.infos[i])    end         print("---------------------------")    for key,value in pairs(msg.infos) do    print(key)    print(value.name)    end

附件中附带了继承 protoc-gen-lua 的 jit2.0.3 。直接运行脚本即可。
我这边用的 Sublime Text2
输出如下:

msg.infos count=2
---------------------------
name: chenpeng

name: chenpeng1

---------------------------
1
chenpeng
2
chenpeng1
_listener
nil
_message_descriptor
Info
[Finished in 0.1s]

测试工程下载:

http://pan.baidu.com/s/1jGnk4Rk 



0 0
原创粉丝点击