加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

protoc-gen-lua中使用 Repeated 标签

发布时间:2020-12-14 21:57:59 所属栏目:大数据 来源:网络整理
导读:比如我这里创建一个 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 的

比如我这里创建一个 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 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读