lua protobuf 使用整理(二)

最近写了一个函数, table自动编码为protobuf格式

local parse
parse = function(args, msg)
    for k, v in pairs(args) do
        if type(v) == 'table' then --table类型判断是否是数组
            if #v > 0 then
                for index, ele in ipairs(v) do --遍历数组元素
                    if type(ele) == 'table' then --数组元素是table, 递归解析内容
                        local emsg = msg[k]:add()
                        parse(ele, emsg)
                    else --数组元素是基本数据类型,直接添加
                        msg[k]:append(ele)
                    end
                end
            else --非数组,递归解析内容
                parse(v, msg[k])
            end
        else
            msg[k] = args[k]
        end
    end
end

标签: none