lua protobuf repeated字段赋值

2017-03-16  本文已影响1540人  e497b7005759

lua使用protobuf发送列表结构,对repeated字段进行赋值会报错,报错内容为“Assignment not allowed to repeated field xxx in protocol message object.”,protobuf不允许对repeated字段赋值。解决办法如下:

test.proto
message Class
 {
  repeated Student students = 2;
}
message Student
 {
  required int32 id = 1;
  required string name = 2;
}

local studentList = {{id = 1,name = 1},{id = 2,name = 2},{id = 3,name = 3}}
local msg = test_pb.Class()     
local students = msg.students;
for i,v in ipairs(studentList) do
    local student = test_pb.Student()
    student.id = v.id
    student.name = v.name
    table.insert(students,student)
end
local data = msg:SerializeToString()
上一篇下一篇

猜你喜欢

热点阅读