protobuf-gen-lua 跨文件引用message 出现 attempt to index upvalue ‘message_type‘ (a nil value) 错误解决

protobuf-gen-lua 跨文件引用message 出现 attempt to index upvalue 'message_type' (a nil value) 错误解决

 

版权声明:本文为博主原创文章,转载请注明CSDN博客源地址!共同学习,一起进步~ 

https://blog.csdn.net/qq_29473881/article/details/107975413

proto文件转成lua文件的时候,解析返回的数据出现'message_type' (a nil value) 错误 原因是跨文件引用了message ,如下:

在接收这个releInfo数据时候就会出现空数据异常,解析不出来数据(其实lua本身好像是不支持传文件引用的,有待研究)

解决方法:修改 protoc-gen-lua 文件多嵌套引用

1、修改 field_desc.HasField('type_name'): 方法(左边是修改前,右边是修改后)

# type_name = env.get_ref_name(field_desc.type_name).upper().replace('.', '_')
        if field_desc.type == FDP.TYPE_MESSAGE:
            # 修改 引用其他文件夹下的message 时,把左边的文件名截取出来,再把右边的字段名大写处理 如: TestInfo_pb.TESTINFOPROTO
            typeNameL = env.get_ref_name(field_desc.type_name).split(".")[0]
            typeNameR = env.get_ref_name(field_desc.type_name).split(".")[1]

            typeNameLL = typeNameL.split("/")
            typeNameL = typeNameLL[len(typeNameLL) - 1]
            type_name = typeNameL +"."+ (typeNameR.upper())
            context('.message_type = %s\n' % type_name)
        else:
            type_name = env.get_ref_name(field_desc.type_name).upper().replace('.', '_')
            context('.enum_type = %s\n' % type_name)

2、修改 code_gen_message(message_descriptor, env, containing_type = None): 方法:

    env.enter(message_descriptor.name)
    full_name = env.get_local_name()
    obj_name = full_name.upper().replace('.', '_')
    # 修改 把local 去掉了,方便 TestInfo_pb.TESTINFOPROTO 能否直接访问到
    env.descriptor.append(
        "%s = protobuf.Descriptor();\n"% obj_name
    )
    # "local %s = protobuf.Descriptor();\n"% obj_name

3、修改 code_gen_file(proto_file, env, is_gen): 方法

lua = Writer()
        write_header(lua)
        lua('local protobuf = require "protobuf"\n')
        for i in includes:
            # 修改 将前面的local xx_pb 去掉 了,因为 这个require 返回的是 一个 true or false 
            lua('require("%s_pb")\n' % (i))
            # lua('local %s_pb = require("%s_pb")\n' % (i, i))
        # 修改 修改文件名,将文件名中的路径去掉
        typeNameLL = env.filename.split("/")
        typeNameL = typeNameLL[len(typeNameLL) - 1]
        # 修改 增加了",package.seeall" 多传入一个package.seeall的参数,相当于 setmetatable(M, {__index = _G}) .
        lua("module('%s_pb',package.seeall)\n" % typeNameL)

通过以上步骤就修改完毕,重新执行proto 转lua的脚本即可看到区别:

(ps:如果想知道proto文件如何转成lua文件,请查看我另外一篇文章:https://blog.csdn.net/qq_29473881/article/details/107958275

以上整个步骤完成,如有问题请留言评论!

(参考文章:https://www.jianshu.com/p/af657613c20a

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值