~bigbes/tarantool

tarantool-protobuf

ref: 0fb62135cea8c822c59c17cd6676697ab5304dd7 tarantool-protobuf/examples/expected/full/c_int64/c_int64_pb.lua -rw-r--r-- 5.6 KiB
0fb62135 — Eugene Blikh codegen: localize wire.* upvalues per generated message function 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-- Code generated by protoc-gen-tarantool. DO NOT EDIT.
-- source: c_int64.proto
-- syntax: proto3
-- package: c_int64

local pb = require("pb")
local wire = pb.wire
local string_byte = string.byte
local utf8_len = require('utf8').len
local band = bit.band
local rshift = bit.rshift

local M = {}

M.options = {go_package = "tarantoolpb_synthetic/c_int64"}

-- Pre-declare message descriptors so cross-references resolve.
M.Wide_descriptor = {name = "c_int64.Wide"}

-- Message: c_int64.Wide
M.Wide_descriptor.fields = {
    {name="a_int64", id=1, kind='scalar', proto_type="int64"},
    {name="a_uint64", id=2, kind='scalar', proto_type="uint64"},
    {name="a_sint64", id=3, kind='scalar', proto_type="sint64"},
    {name="a_fixed64", id=4, kind='scalar', proto_type="fixed64"},
    {name="a_sfixed64", id=5, kind='scalar', proto_type="sfixed64"},
}
pb.finalize_message(M.Wide_descriptor)
M.Wide_fields = pb.field_names({
    a_int64 = "a_int64",
    a_uint64 = "a_uint64",
    a_sint64 = "a_sint64",
    a_fixed64 = "a_fixed64",
    a_sfixed64 = "a_sfixed64",
})

-- EmmyLua / lua-language-server type annotations.
-- These are comments — no runtime effect. They give editors
-- autocomplete and type-checking for the generated wrappers.
---@class c_int64.Wide
---@field a_int64 integer
---@field a_uint64 integer
---@field a_sint64 integer
---@field a_fixed64 integer
---@field a_sfixed64 integer

---@param t? c_int64.Wide
---@return c_int64.Wide
function M.Wide_new(t) return t or {} end

---@param t c_int64.Wide
---@return string
function M.Wide_encode(t)
    local _d = M.Wide_descriptor
    if pb.c_runtime ~= nil then
        local _p = _d.c_plan or pb.c_runtime.compile_plan(_d)
        return pb.c_runtime.encode(_p, t)
    end
    if type(t) ~= 'table' then
        error("expected table for c_int64.Wide, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: a_int64
    v = t.a_int64
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x08"
        n = n + 1; out[n] = wire.encode_int64(v)
    end
    -- field 2: a_uint64
    v = t.a_uint64
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x10"
        n = n + 1; out[n] = wire.encode_uint64(v)
    end
    -- field 3: a_sint64
    v = t.a_sint64
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x18"
        n = n + 1; out[n] = wire.encode_sint64(v)
    end
    -- field 4: a_fixed64
    v = t.a_fixed64
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x21"
        n = n + 1; out[n] = wire.encode_fixed64(v)
    end
    -- field 5: a_sfixed64
    v = t.a_sfixed64
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x29"
        n = n + 1; out[n] = wire.encode_sfixed64(v)
    end
    local _exts = t._extensions
    if _exts ~= nil then
        local _elist = M.Wide_descriptor.extensions_list
        if _elist ~= nil then
            for _i = 1, #_elist do
                local _ext = _elist[_i]
                local _ev = _exts[_ext.full_name]
                if _ev ~= nil then
                    pb.codec.encode_field(_ext, _ev, out, true)
                end
            end
            n = #out
        end
    end
    local _uf = t._unknown_fields
    if _uf ~= nil and _uf ~= '' then n = n + 1; out[n] = _uf end
    return table.concat(out)
end

---@param b string
---@return c_int64.Wide
function M.Wide_decode(buf)
    local _d = M.Wide_descriptor
    if pb.c_runtime ~= nil then
        local _p = _d.c_plan or pb.c_runtime.compile_plan(_d)
        return pb.c_runtime.decode(_p, buf)
    end
    if type(buf) ~= 'string' then
        error("expected string for c_int64.Wide decode, got " .. type(buf), 0)
    end
    local result = {}
    local pos, len = 1, #buf
    local _uf
    while pos <= len do
        local _tag_start = pos
        local id, wt
        local _b = string_byte(buf, pos)
        if _b ~= nil and _b < 0x80 then
            wt = band(_b, 7)
            if wt >= 6 then error("illegal wire type " .. wt, 0) end
            id = rshift(_b, 3)
            if id == 0 then error("illegal field number 0", 0) end
            pos = pos + 1
        else
            id, wt, pos = wire.decode_tag(buf, pos)
        end
        if id == 1 then
            local val
            val, pos = wire.decode_int64(buf, pos)
            result.a_int64 = val
        elseif id == 2 then
            local val
            val, pos = wire.decode_uint64(buf, pos)
            result.a_uint64 = val
        elseif id == 3 then
            local val
            val, pos = wire.decode_sint64(buf, pos)
            result.a_sint64 = val
        elseif id == 4 then
            local val
            val, pos = wire.decode_fixed64(buf, pos)
            result.a_fixed64 = val
        elseif id == 5 then
            local val
            val, pos = wire.decode_sfixed64(buf, pos)
            result.a_sfixed64 = val
        else
            local _ebid = M.Wide_descriptor.extensions_by_id
            local _ext = _ebid and _ebid[id] or nil
            if _ext ~= nil then
                pos = pb.codec.decode_extension(_ext, buf, pos, wt, result)
            else
                pos = wire.skip_field(buf, pos, wt, id)
                if _uf == nil then _uf = {} end
                _uf[#_uf + 1] = buf:sub(_tag_start, pos - 1)
            end
        end
    end
    if _uf ~= nil then result._unknown_fields = table.concat(_uf) end
    return result
end

---@param b string
---@return pb.MessageView
function M.Wide_decode_lazy(b) return pb.decode_lazy(M.Wide_descriptor, b) end
---@param t c_int64.Wide
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.Wide_text(t, opts) return pb.text.encode(M.Wide_descriptor, t, opts) end

return M