~bigbes/tarantool

tarantool-protobuf

ref: a0005a0576092d45f73bfcc87200ab6a721d5c3a tarantool-protobuf/examples/expected/full/quickstart/quickstart_pb.lua -rw-r--r-- 4.3 KiB
a0005a05 — Eugene Blikh docs: full reference + how-to set, migrate Makefile to Justfile 3 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
-- 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 M = {}

-- 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"
        n = n + 1; out[n] = wire.encode_varint(#v)
        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
            n = n + 1; out[n] = wire.encode_varint(#_b)
            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)
    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
    while pos <= len do
        local _tag_start = pos
        local id, wt
        id, wt, pos = wire.decode_tag(buf, pos)
        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)
            list[#list + 1] = val
        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
    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