protobuf 在lua中使用(3)

来源:互联网 发布:高中数学辅导书 知乎 编辑:程序博客网 时间:2024/05/27 20:42

proto文件导出lua文件工具参考

http://download.csdn.net/detail/c471961491/9414388


通过前两篇的介绍已经能搭建一个lua的probuf使用结构了

前两篇博文地址

http://blog.csdn.net/daydayup_chf/article/details/49904785

http://blog.csdn.net/daydayup_chf/article/details/49994023


然而在使用过程中,碰到了以下情况


TestA.proto

package Google;message TestA{required uint32 id   = 1;optional uint32 name   = 2;required string year    = 3;optional uint32 nettype   = 4;}



TestB.proto

package Google;message TestB{required uint32 aa   = 1;optional uint32 bb   = 2;required string cc    = 3;}



TestC.proto

import "TestA.proto";import "TestB.proto";package Google;message TestC{required TestAtesta  = 1;required TestB testb = 2; }message TestD{required uint32dddd  = 1;}message TestE{required TestD testd  = 1;}


当使用 message TestC 时出现错误

具体原因是报message_type是个空值


改正这个的处理方法是对导出的lua文件做修改


TestA.lua

-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module('TestA_pb')TESTA = protobuf.Descriptor(); --此处去掉locallocal TESTA_ID_FIELD = protobuf.FieldDescriptor();local TESTA_NAME_FIELD = protobuf.FieldDescriptor();local TESTA_YEAR_FIELD = protobuf.FieldDescriptor();local TESTA_NETTYPE_FIELD = protobuf.FieldDescriptor();TESTA_ID_FIELD.name = "id"TESTA_ID_FIELD.full_name = ".Google.TestA.id"TESTA_ID_FIELD.number = 1TESTA_ID_FIELD.index = 0TESTA_ID_FIELD.label = 2TESTA_ID_FIELD.has_default_value = falseTESTA_ID_FIELD.default_value = 0TESTA_ID_FIELD.type = 13TESTA_ID_FIELD.cpp_type = 3TESTA_NAME_FIELD.name = "name"TESTA_NAME_FIELD.full_name = ".Google.TestA.name"TESTA_NAME_FIELD.number = 2TESTA_NAME_FIELD.index = 1TESTA_NAME_FIELD.label = 1TESTA_NAME_FIELD.has_default_value = falseTESTA_NAME_FIELD.default_value = 0TESTA_NAME_FIELD.type = 13TESTA_NAME_FIELD.cpp_type = 3TESTA_YEAR_FIELD.name = "year"TESTA_YEAR_FIELD.full_name = ".Google.TestA.year"TESTA_YEAR_FIELD.number = 3TESTA_YEAR_FIELD.index = 2TESTA_YEAR_FIELD.label = 2TESTA_YEAR_FIELD.has_default_value = falseTESTA_YEAR_FIELD.default_value = ""TESTA_YEAR_FIELD.type = 9TESTA_YEAR_FIELD.cpp_type = 9TESTA_NETTYPE_FIELD.name = "nettype"TESTA_NETTYPE_FIELD.full_name = ".Google.TestA.nettype"TESTA_NETTYPE_FIELD.number = 4TESTA_NETTYPE_FIELD.index = 3TESTA_NETTYPE_FIELD.label = 1TESTA_NETTYPE_FIELD.has_default_value = falseTESTA_NETTYPE_FIELD.default_value = 0TESTA_NETTYPE_FIELD.type = 13TESTA_NETTYPE_FIELD.cpp_type = 3TESTA.name = "TestA"TESTA.full_name = ".Google.TestA"TESTA.nested_types = {}TESTA.enum_types = {}TESTA.fields = {TESTA_ID_FIELD, TESTA_NAME_FIELD, TESTA_YEAR_FIELD, TESTA_NETTYPE_FIELD}TESTA.is_extendable = falseTESTA.extensions = {}TestA = protobuf.Message(TESTA)



TestB.lua

-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module('TestB_pb')TESTB = protobuf.Descriptor(); --去掉locallocal TESTB_AA_FIELD = protobuf.FieldDescriptor();local TESTB_BB_FIELD = protobuf.FieldDescriptor();local TESTB_CC_FIELD = protobuf.FieldDescriptor();TESTB_AA_FIELD.name = "aa"TESTB_AA_FIELD.full_name = ".Google.TestB.aa"TESTB_AA_FIELD.number = 1TESTB_AA_FIELD.index = 0TESTB_AA_FIELD.label = 2TESTB_AA_FIELD.has_default_value = falseTESTB_AA_FIELD.default_value = 0TESTB_AA_FIELD.type = 13TESTB_AA_FIELD.cpp_type = 3TESTB_BB_FIELD.name = "bb"TESTB_BB_FIELD.full_name = ".Google.TestB.bb"TESTB_BB_FIELD.number = 2TESTB_BB_FIELD.index = 1TESTB_BB_FIELD.label = 1TESTB_BB_FIELD.has_default_value = falseTESTB_BB_FIELD.default_value = 0TESTB_BB_FIELD.type = 13TESTB_BB_FIELD.cpp_type = 3TESTB_CC_FIELD.name = "cc"TESTB_CC_FIELD.full_name = ".Google.TestB.cc"TESTB_CC_FIELD.number = 3TESTB_CC_FIELD.index = 2TESTB_CC_FIELD.label = 2TESTB_CC_FIELD.has_default_value = falseTESTB_CC_FIELD.default_value = ""TESTB_CC_FIELD.type = 9TESTB_CC_FIELD.cpp_type = 9TESTB.name = "TestB"TESTB.full_name = ".Google.TestB"TESTB.nested_types = {}TESTB.enum_types = {}TESTB.fields = {TESTB_AA_FIELD, TESTB_BB_FIELD, TESTB_CC_FIELD}TESTB.is_extendable = falseTESTB.extensions = {}TestB = protobuf.Message(TESTB)


TestC.lua

-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"local TestA_pb = require("TestA_pb")local TestB_pb = require("TestB_pb")module('TestC_pb')local TESTC = protobuf.Descriptor();local TESTC_TESTA_FIELD = protobuf.FieldDescriptor();local TESTC_TESTB_FIELD = protobuf.FieldDescriptor();local TESTD = protobuf.Descriptor();local TESTD_DDDD_FIELD = protobuf.FieldDescriptor();local TESTE = protobuf.Descriptor();local TESTE_TESTD_FIELD = protobuf.FieldDescriptor();TESTC_TESTA_FIELD.name = "testa"TESTC_TESTA_FIELD.full_name = ".Google.TestC.testa"TESTC_TESTA_FIELD.number = 1TESTC_TESTA_FIELD.index = 0TESTC_TESTA_FIELD.label = 2TESTC_TESTA_FIELD.has_default_value = falseTESTC_TESTA_FIELD.default_value = nilTESTC_TESTA_FIELD.message_type = TestA_pb.TESTA  --改成这样TESTC_TESTA_FIELD.type = 11TESTC_TESTA_FIELD.cpp_type = 10TESTC_TESTB_FIELD.name = "testb"TESTC_TESTB_FIELD.full_name = ".Google.TestC.testb"TESTC_TESTB_FIELD.number = 2TESTC_TESTB_FIELD.index = 1TESTC_TESTB_FIELD.label = 2TESTC_TESTB_FIELD.has_default_value = falseTESTC_TESTB_FIELD.default_value = nilTESTC_TESTB_FIELD.message_type = TestB_pb.TESTB  --改成这样TESTC_TESTB_FIELD.type = 11TESTC_TESTB_FIELD.cpp_type = 10TESTC.name = "TestC"TESTC.full_name = ".Google.TestC"TESTC.nested_types = {}TESTC.enum_types = {}TESTC.fields = {TESTC_TESTA_FIELD, TESTC_TESTB_FIELD}TESTC.is_extendable = falseTESTC.extensions = {}TESTD_DDDD_FIELD.name = "dddd"TESTD_DDDD_FIELD.full_name = ".Google.TestD.dddd"TESTD_DDDD_FIELD.number = 1TESTD_DDDD_FIELD.index = 0TESTD_DDDD_FIELD.label = 2TESTD_DDDD_FIELD.has_default_value = falseTESTD_DDDD_FIELD.default_value = 0TESTD_DDDD_FIELD.type = 13TESTD_DDDD_FIELD.cpp_type = 3TESTD.name = "TestD"TESTD.full_name = ".Google.TestD"TESTD.nested_types = {}TESTD.enum_types = {}TESTD.fields = {TESTD_DDDD_FIELD}TESTD.is_extendable = falseTESTD.extensions = {}TESTE_TESTD_FIELD.name = "testd"TESTE_TESTD_FIELD.full_name = ".Google.TestE.testd"TESTE_TESTD_FIELD.number = 1TESTE_TESTD_FIELD.index = 0TESTE_TESTD_FIELD.label = 2TESTE_TESTD_FIELD.has_default_value = falseTESTE_TESTD_FIELD.default_value = nilTESTE_TESTD_FIELD.message_type = TESTDTESTE_TESTD_FIELD.type = 11TESTE_TESTD_FIELD.cpp_type = 10TESTE.name = "TestE"TESTE.full_name = ".Google.TestE"TESTE.nested_types = {}TESTE.enum_types = {}TESTE.fields = {TESTE_TESTD_FIELD}TESTE.is_extendable = falseTESTE.extensions = {}TestC = protobuf.Message(TESTC)TestD = protobuf.Message(TESTD)TestE = protobuf.Message(TESTE)


修改完成之后保存,就能调用 message TestC 

0 0