lua 支持int64[亲测有效]

lua 支持int64[亲测有效]luanumber&protobufint32int64repeatedissue:Valueoutofrange1.encoder.luafunction_VarintSize(value)ifvalue<=0x7fthenreturn1endifvalue<=0x3fffthenreturn2endifv…

大家好,欢迎来到IT知识分享网。

lua number & protobuf int32 int64 repeated issue: Value out of range

1.encoder.lua

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end

function _SignedVarintSize(value)
if value < 0 then return 10 end
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end

改成

function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end

function _SignedVarintSize(value)
if value < 0 then return 10 end
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end

2.ware_format里 (非必要?)

local function _VarUInt64ByteSizeNoTag(uint64)
if uint64 <= 0x7f then return 1 end
if uint64 <= 0x3fff then return 2 end
if uint64 <= 0x1fffff then return 3 end
if uint64 <= 0xfffffff then return 4 end
return 5
end

改成

local function _VarUInt64ByteSizeNoTag(uint64)
if uint64 <= 0x7f then return 1 end
if uint64 <= 0x3fff then return 2 end
if uint64 <= 0x1fffff then return 3 end
if uint64 <= 0xfffffff then return 4 end
if uint64 <= 0x7ffffffff then return 5 end
if uint64 <= 0x3ffffffffff then return 6 end
if uint64 <= 0x1ffffffffffff then return 7 end
if uint64 <= 0xffffffffffffff then return 8 end
if uint64 <= 0x7fffffffffffffff then return 9 end
return 10
end

type_checkers.lua里 (这个范围是我们自己根据一些情况 设定了一个范围)
增加
function Int64ValueChecker()
local _MIN = -562949953421312
local _MAX = 562949953421312
return function(proposed_value)
if type(proposed_value) ~= 'number' then
error(string.format('%s has type %s, but expected one of: number',
proposed_value, type(proposed_value)))
end
if _MIN > proposed_value or proposed_value > _MAX then
error('Value out of range: ' .. proposed_value)
end
end
end

function Uint64ValueChecker(IntValueChecker)
local _MIN = 0
local _MAX = 1125899906842624

return function(proposed_value)
    if type(proposed_value) ~= 'number' then
        error(string.format('%s has type %s, but expected one of: number',
            proposed_value, type(proposed_value)))
    end
    if _MIN > proposed_value or proposed_value > _MAX then
        error('Value out of range: ' .. proposed_value)
    end
end
end

4.protobuf.lua
里

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int32ValueChecker(),

[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint32ValueChecker(),
改成

[FieldDescriptor.CPPTYPE_INT64] = type_checkers.Int64ValueChecker(),

[FieldDescriptor.CPPTYPE_UINT64] = type_checkers.Uint64ValueChecker(),
仅供参考

转自:https://github.com/sean-lin/protoc-gen-lua/issues/23

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25147.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信