您的位置:首页 > 编程语言 > Lua

protoc-gen-lua extensions正确的使用方式

2015-12-12 02:14 627 查看
我们项目中的Protobuf 是这样的格式--Student.protomessage Student{required int32 id = 1;extensions 10 to max;}message Phone{required string phonenum=1;}extend Student{repeated Phone Studentphones = 10;}用protoc-gen-lua生成之后如下--Student_pb.lua-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module('Student_pb')local STUDENT = protobuf.Descriptor();local STUDENT_ID_FIELD = protobuf.FieldDescriptor();local PHONE = protobuf.Descriptor();local PHONE_PHONENUM_FIELD = protobuf.FieldDescriptor();STUDENT_ID_FIELD.name = "id"STUDENT_ID_FIELD.full_name = ".Student.id"STUDENT_ID_FIELD.number = 1STUDENT_ID_FIELD.index = 0STUDENT_ID_FIELD.label = 2STUDENT_ID_FIELD.has_default_value = falseSTUDENT_ID_FIELD.default_value = 0STUDENT_ID_FIELD.type = 5STUDENT_ID_FIELD.cpp_type = 1STUDENT.name = "Student"STUDENT.full_name = ".Student"STUDENT.nested_types = {}STUDENT.enum_types = {}STUDENT.fields = {STUDENT_ID_FIELD}STUDENT.is_extendable = trueSTUDENT.extensions = {}PHONE_PHONENUM_FIELD.name = "phonenum"PHONE_PHONENUM_FIELD.full_name = ".Phone.phonenum"PHONE_PHONENUM_FIELD.number = 1PHONE_PHONENUM_FIELD.index = 0PHONE_PHONENUM_FIELD.label = 2PHONE_PHONENUM_FIELD.has_default_value = falsePHONE_PHONENUM_FIELD.default_value = ""PHONE_PHONENUM_FIELD.type = 9PHONE_PHONENUM_FIELD.cpp_type = 9PHONE.name = "Phone"PHONE.full_name = ".Phone"PHONE.nested_types = {}PHONE.enum_types = {}PHONE.fields = {PHONE_PHONENUM_FIELD}PHONE.is_extendable = falsePHONE.extensions = {}Phone = protobuf.Message(PHONE)Student = protobuf.Message(STUDENT)完全没有 extensions 被注册。所以这种格式是不被支持的。还是要按照 example 中的写
--Person.proto

message Person
{
required int32 id = 1;

extensions 10 to max;
}

message Phone
{
extend Person
{
repeated Phone phones = 10;
}

required string phonenum=1;
}

message Course
{
extend Person
{
repeated Course course = 11;
}

required int32 courseid=1;
}
这样生成的 pb lua 文件像下面这样--person_pb.lua-- Generated By protoc-gen-lua Do not Editlocal protobuf = require "protobuf"module('person_pb')local PERSON = protobuf.Descriptor();local PERSON_ID_FIELD = protobuf.FieldDescriptor();local PHONE = protobuf.Descriptor();local PHONE_PHONENUM_FIELD = protobuf.FieldDescriptor();local PHONE_PHONES_FIELD = protobuf.FieldDescriptor();local COURSE = protobuf.Descriptor();local COURSE_COURSEID_FIELD = protobuf.FieldDescriptor();local COURSE_COURSE_FIELD = protobuf.FieldDescriptor();PERSON_ID_FIELD.name = "id"PERSON_ID_FIELD.full_name = ".Person.id"PERSON_ID_FIELD.number = 1PERSON_ID_FIELD.index = 0PERSON_ID_FIELD.label = 2PERSON_ID_FIELD.has_default_value = falsePERSON_ID_FIELD.default_value = 0PERSON_ID_FIELD.type = 5PERSON_ID_FIELD.cpp_type = 1PERSON.name = "Person"PERSON.full_name = ".Person"PERSON.nested_types = {}PERSON.enum_types = {}PERSON.fields = {PERSON_ID_FIELD}PERSON.is_extendable = truePERSON.extensions = {}PHONE_PHONENUM_FIELD.name = "phonenum"PHONE_PHONENUM_FIELD.full_name = ".Phone.phonenum"PHONE_PHONENUM_FIELD.number = 1PHONE_PHONENUM_FIELD.index = 0PHONE_PHONENUM_FIELD.label = 2PHONE_PHONENUM_FIELD.has_default_value = falsePHONE_PHONENUM_FIELD.default_value = ""PHONE_PHONENUM_FIELD.type = 9PHONE_PHONENUM_FIELD.cpp_type = 9PHONE_PHONES_FIELD.name = "phones"PHONE_PHONES_FIELD.full_name = ".Phone.phones"PHONE_PHONES_FIELD.number = 10PHONE_PHONES_FIELD.index = 0PHONE_PHONES_FIELD.label = 3PHONE_PHONES_FIELD.has_default_value = falsePHONE_PHONES_FIELD.default_value = {}PHONE_PHONES_FIELD.message_type = PHONEPHONE_PHONES_FIELD.type = 11PHONE_PHONES_FIELD.cpp_type = 10PHONE.name = "Phone"PHONE.full_name = ".Phone"PHONE.nested_types = {}PHONE.enum_types = {}PHONE.fields = {PHONE_PHONENUM_FIELD}PHONE.is_extendable = falsePHONE.extensions = {PHONE_PHONES_FIELD}COURSE_COURSEID_FIELD.name = "courseid"COURSE_COURSEID_FIELD.full_name = ".Course.courseid"COURSE_COURSEID_FIELD.number = 1COURSE_COURSEID_FIELD.index = 0COURSE_COURSEID_FIELD.label = 2COURSE_COURSEID_FIELD.has_default_value = falseCOURSE_COURSEID_FIELD.default_value = 0COURSE_COURSEID_FIELD.type = 5COURSE_COURSEID_FIELD.cpp_type = 1COURSE_COURSE_FIELD.name = "course"COURSE_COURSE_FIELD.full_name = ".Course.course"COURSE_COURSE_FIELD.number = 11COURSE_COURSE_FIELD.index = 0COURSE_COURSE_FIELD.label = 3COURSE_COURSE_FIELD.has_default_value = falseCOURSE_COURSE_FIELD.default_value = {}COURSE_COURSE_FIELD.message_type = COURSECOURSE_COURSE_FIELD.type = 11COURSE_COURSE_FIELD.cpp_type = 10COURSE.name = "Course"COURSE.full_name = ".Course"COURSE.nested_types = {}COURSE.enum_types = {}COURSE.fields = {COURSE_COURSEID_FIELD}COURSE.is_extendable = falseCOURSE.extensions = {COURSE_COURSE_FIELD}Course = protobuf.Message(COURSE)Person = protobuf.Message(PERSON)Phone = protobuf.Message(PHONE)Person.RegisterExtension(PHONE_PHONES_FIELD)Person.RegisterExtension(COURSE_COURSE_FIELD)注册了 Extension ,这样才能在代码中去使用 extensions 。测试代码
--test.lua

package.path = package.path .. ';../protobuf/?.lua;../protobuf/luascript/?.lua'
package.cpath = package.cpath .. ';../protobuf/?.so'

require 'person_pb'

local person= person_pb.Person()
person.id = 1000

local phone = person.Extensions[person_pb.Phone.phones]:add()
phone.phonenum="1376795xxxx"

local phone1 = person.Extensions[person_pb.Phone.phones]:add()
phone1.phonenum="1376795xxxx-1"

local course = person.Extensions[person_pb.Course.course]:add()
course.courseid=12

local data = person:SerializeToString()

local msg = person_pb.Person()

msg:ParseFromString(data)
print(msg)

print(table.maxn(msg.Extensions[person_pb.Phone.phones]))
for i=1,table.maxn(msg.Extensions[person_pb.Phone.phones]),1 do
local phone=msg.Extensions[person_pb.Phone.phones][i]
print(phone.phonenum)
end
输出
id: 1000

[phones] {

phonenum: 1376795xxxx

}

[phones] {

phonenum: 1376795xxxx-1

}

[course] {

courseid: 12

}
2

1376795xxxx

1376795xxxx-1

[Finished in 0.0s]
测试工程打包下载
http://pan.baidu.com/s/1nt5v0gD
上面的是 repeate ,对应的就是 table ,单个的像下面写
    --person.protomessage Person{required int32 id = 1;extensions 10 to max;}message Phone{extend Person{required Phone phones = 10;}required string phonenum=1;}测试代码
    --test.luapackage.path = package.path .. ';../protobuf/?.lua;../protobuf/luascript/?.lua'package.cpath = package.cpath .. ';../protobuf/?.so'require 'person_pb'local person= person_pb.Person()person.id = 1000local phone = person.Extensions[person_pb.Phone.phones]phone.phonenum="1376795xxxx"local data = person:SerializeToString()-------------------------------------------local msg = person_pb.Person()msg:ParseFromString(data)print(msg)print(msg.Extensions[person_pb.Phone.phones])输出
[phones] {phonenum: 1376795xxxx}id: 1000
phonenum: 1376795xxxx
[Finished in 0.0s]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: