Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

序列化结果是乱序的 #35

Open
zhudusong opened this issue Nov 11, 2017 · 0 comments
Open

序列化结果是乱序的 #35

zhudusong opened this issue Nov 11, 2017 · 0 comments

Comments

@zhudusong
Copy link

protobuf.lua中_internal_serialize函数实现,ListFields方法遍历是无序的,会导致结果不稳定,在某些情景下无法反序列化。

local _internal_serialize = function(self, write_bytes)
    for field_descriptor, field_value in message_meta._member.ListFields(self) do
        field_descriptor._encoder(write_bytes, field_value)
    end
end

改动:根据field的key的index排序,再进行遍历。

local _internal_serialize = function(self, write_bytes)
    local field_list = {}
    for k, v in pairs(self._fields) do
        field_list[k.index + 1] = k
    end
    local n = table.maxn(field_list)
    for i = 1, n do
        local descriptor = field_list[i]
        if descriptor then
            value = self._fields[descriptor]
            if _IsPresent(descriptor, value) then
                descriptor._encoder(write_bytes, value)
            end
        end
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant