~bigbes/tarantool

tarantool-protobuf

ref: e3f067416da55e4390d1a9b3e1f410ad7a48d22f tarantool-protobuf/examples/expected/full/quickstart/quickstart_pb.lua -rw-r--r-- 9.8 KiB
e3f06741 — Eugene Blikh runtime: pb.decode_unsafe + M.<Name>_decode_unsafe in runtime mode (58u) 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
-- Code generated by protoc-gen-tarantool. DO NOT EDIT.
-- source: quickstart.proto
-- syntax: proto3
-- package: quickstart

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 CHARS = wire.CHARS
local table_new = require('table.new')

local M = {}

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

-- Enum: quickstart.Role
M.Role_descriptor = pb.enum("quickstart.Role", {
    ROLE_UNSPECIFIED = 0,
    USER = 1,
    ADMIN = 2,
})
M.Role = M.Role_descriptor.by_name

-- Pre-declare message descriptors so cross-references resolve.
M.User_descriptor = {name = "quickstart.User"}

-- Message: quickstart.User
M.User_descriptor.fields = {
    {name="id", id=1, kind='scalar', proto_type="int32"},
    {name="name", id=2, kind='scalar', proto_type="string"},
    {name="role", id=3, kind='enum', enum=M.Role_descriptor},
    {name="emails", id=4, kind='scalar', proto_type="string", repeated=true},
}
pb.finalize_message(M.User_descriptor)
M.User_fields = pb.field_names({
    id = "id",
    name = "name",
    role = "role",
    emails = "emails",
})

-- EmmyLua / lua-language-server type annotations.
-- These are comments — no runtime effect. They give editors
-- autocomplete and type-checking for the generated wrappers.
---@alias quickstart.Role integer

---@class quickstart.User
---@field id integer
---@field name string
---@field role quickstart.Role
---@field emails string[]

---@param t? quickstart.User
---@return quickstart.User
function M.User_new(t) return t or {} end

---@param t quickstart.User
---@return string
function M.User_encode(t)
    local encode_int32 = wire.encode_int32
    local encode_varint = wire.encode_varint
    local _d = M.User_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 quickstart.User, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: id
    v = t.id
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x08"
        n = n + 1; out[n] = encode_int32(v)
    end
    -- field 2: name
    v = t.name
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x12"
        local _len = #v
        if _len < 128 then
            n = n + 1; out[n] = CHARS[_len]
        else
            n = n + 1; out[n] = encode_varint(_len)
        end
        n = n + 1; out[n] = v
    end
    -- field 3: role
    v = t.role
    if v ~= nil then
        local nv = v
        if type(v) == 'string' then
            nv = M.Role[v]
            if nv == nil then error("unknown enum value '" .. v .. "' for quickstart.Role", 0) end
        end
        if nv ~= 0 then
            n = n + 1; out[n] = "\x18"
            n = n + 1; out[n] = encode_int32(nv)
        end
    end
    -- field 4: emails
    v = t.emails
    if v ~= nil and #v > 0 then
        local _tag = "\x22"
        for _i = 1, #v do
            local _b = v[_i]
            n = n + 1; out[n] = _tag
            local _len = #_b
            if _len < 128 then
                n = n + 1; out[n] = CHARS[_len]
            else
                n = n + 1; out[n] = encode_varint(_len)
            end
            n = n + 1; out[n] = _b
        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 quickstart.User
function M.User_decode(buf)
    local decode_string = wire.decode_string
    local _d = M.User_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 quickstart.User decode, got " .. type(buf), 0)
    end
    local result = {}
    local pos, len = 1, #buf
    local _uf
    local _n_emails = 0
    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_int32(buf, pos)
            result.id = val
        elseif id == 2 then
            local _lb = string_byte(buf, pos)
            if _lb ~= nil and _lb < 0x80 then
                local _np = pos + 1
                local _epos = _np + _lb
                if _epos - 1 > len then error("truncated LEN at offset " .. pos, 0) end
                local _s = buf:sub(_np, _epos - 1)
                if utf8_len(_s) == nil then error("invalid UTF-8 in string field at offset " .. pos, 0) end
                result.name = _s
                pos = _epos
            else
                local val
                val, pos = decode_string(buf, pos)
                result.name = val
            end
        elseif id == 3 then
            local u
            u, pos = wire.decode_varint(buf, pos)
            result.role = wire.varint_to_int32(u)
        elseif id == 4 then
            local list = result.emails
            if list == nil then list = {}; result.emails = list end
            local _lb = string_byte(buf, pos)
            if _lb ~= nil and _lb < 0x80 then
                local _np = pos + 1
                local _epos = _np + _lb
                if _epos - 1 > len then error("truncated LEN at offset " .. pos, 0) end
                local _s = buf:sub(_np, _epos - 1)
                if utf8_len(_s) == nil then error("invalid UTF-8 in string field at offset " .. pos, 0) end
                _n_emails = _n_emails + 1; list[_n_emails] = _s
                pos = _epos
            else
                local val
                val, pos = decode_string(buf, pos)
                _n_emails = _n_emails + 1; list[_n_emails] = val
            end
        else
            local _ebid = M.User_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 quickstart.User
function M.User_decode_unsafe(buf)
    local decode_bytes = wire.decode_bytes
    local _d = M.User_descriptor
    if type(buf) ~= 'string' then
        error("expected string for quickstart.User decode, got " .. type(buf), 0)
    end
    local result = {}
    local pos, len = 1, #buf
    local _uf
    local _n_emails = 0
    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_int32(buf, pos)
            result.id = val
        elseif id == 2 then
            local _lb = string_byte(buf, pos)
            if _lb ~= nil and _lb < 0x80 then
                local _np = pos + 1
                local _epos = _np + _lb
                if _epos - 1 > len then error("truncated LEN at offset " .. pos, 0) end
                local _s = buf:sub(_np, _epos - 1)
                result.name = _s
                pos = _epos
            else
                local val
                val, pos = decode_bytes(buf, pos)
                result.name = val
            end
        elseif id == 3 then
            local u
            u, pos = wire.decode_varint(buf, pos)
            result.role = wire.varint_to_int32(u)
        elseif id == 4 then
            local list = result.emails
            if list == nil then list = {}; result.emails = list end
            local _lb = string_byte(buf, pos)
            if _lb ~= nil and _lb < 0x80 then
                local _np = pos + 1
                local _epos = _np + _lb
                if _epos - 1 > len then error("truncated LEN at offset " .. pos, 0) end
                local _s = buf:sub(_np, _epos - 1)
                _n_emails = _n_emails + 1; list[_n_emails] = _s
                pos = _epos
            else
                local val
                val, pos = decode_bytes(buf, pos)
                _n_emails = _n_emails + 1; list[_n_emails] = val
            end
        else
            local _ebid = M.User_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.User_decode_lazy(b) return pb.decode_lazy(M.User_descriptor, b) end
---@param t quickstart.User
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.User_text(t, opts) return pb.text.encode(M.User_descriptor, t, opts) end

return M