~bigbes/tarantool

tarantool-protobuf

ref: 956bf2a2a4baf60a231a5e0377b418860a47cd32 tarantool-protobuf/examples/expected/full/quickstart/quickstart_pb.lua -rw-r--r-- 5.8 KiB
956bf2a2 — Eugene Blikh c-accel: fix strdup on glibc with -std=c99 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
-- 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 band = bit.band
local rshift = bit.rshift

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)
    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] = wire.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] = string.char(_len)
        else
            n = n + 1; out[n] = wire.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] = wire.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] = string.char(_len)
            else
                n = n + 1; out[n] = wire.encode_varint(_len)
            end
            n = n + 1; out[n] = _b
        end
    end
    local _exts = t._extensions
    if _exts ~= nil then
        local _elist = M.User_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 quickstart.User
function M.User_decode(buf)
    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 val
            val, pos = wire.decode_string(buf, pos)
            result.name = val
        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 val
            val, pos = wire.decode_string(buf, pos)
            _n_emails = _n_emails + 1; list[_n_emails] = val
        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