-- 遍历信息节点,进行字典转换
function getDict(msg_)
local _dict = {}
for _field, _value in msg_:ListFields() do
local _key = _field.name
local _dealValueFunc = function(field_value)
if _field.type == FieldDescriptor.TYPE_MESSAGE then
return getDict(field_value)
else
return field_value
end
end
if _field.label == FieldDescriptor.LABEL_REPEATED then
local _list = {}
for _, _item in ipairs(_value) do
table.insert(_list,_dealValueFunc(_item))
end
_dict[_key] = _list
else
_dict[_key] = _dealValueFunc(_value)
end
end
return _dict
end
protobuf/text_format.lua 中添加以上方法。
protobuf/protobuf.lua 中做如上修改。
这样 ParseFromString 就可以返回一个纯lua表