Quick+PROTOC-GEN-LUA 发现的名字冲突

来源:互联网 发布:风电场风速数据下载 编辑:程序博客网 时间:2024/04/28 20:08

    Q.   当.PROTO文件内的变量名全部写成大写的话, 将会导至出现 attempt to call field 'MSG_XXX_XXX' (a nil value)

           在查证后发现, XXX.PROTO 文件与XXX_PB.LUA 里边的消息名是重复的,所以会造成这个问题,

           原因是在生成LUA文件的时候中间变量名用的是全大写同名名字, 如果自定的消息名字为小写,则没有此问题

           例如: XXX.proto 文件里边有这样一个消息:  message MSG_C2L_LOGIN_SYN {...}

               在生成XXX_pb.lua中将出现如下内容:

              local MSG_C2L_LOGIN_SYN = protobuf.Descriptor();

              MSG_C2L_LOGIN_SYN.name = "MSG_C2L_LOGIN"
              MSG_C2L_LOGIN_SYN.full_name = ".net.MSG_C2L_LOGIN"
              MSG_C2L_LOGIN_SYN.nested_types = {}
              MSG_C2L_LOGIN_SYN.enum_types = {}
              MSG_C2L_LOGIN_SYN.fields = {}
              MSG_C2L_LOGIN_SYN.is_extendable = false
              MSG_C2L_LOGIN_SYN.extensions = {}

              MSG_C2L_LOGIN_SYN = protobuf.Message(MSG_C2L_LOGIN_SYN)

 

              如上: 大家看出来了吧, 这个文件本来是想导出一个全局变量来的, 结果这个变量已经被局部占有了!

      A:    解决方法:

                      需要修改生成LUA脚本的代码!

                      打开protoc-gen-lua文件, 找到336行

                     将原内容:

                     def 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('.', '_') 
                         env.descriptor.append(
                             "local %s = protobuf.Descriptor();\n"% obj_name
                         )

                     修改成:

                     def 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('.', '_') + '_DEC'
                         env.descriptor.append(
                             "local %s = protobuf.Descriptor();\n"% obj_name
                         )

                     重新编译后, 便解决这个问题了!
0 0
原创粉丝点击