~bigbes/tarantool

tarantool-protobuf

ref: cef42bf3d8349e953917d9a0ba1522edadf36bd6 tarantool-protobuf/examples/expected/runtime/hello/hello_pb.lua -rw-r--r-- 17.2 KiB
cef42bf3 — Eugene Blikh docs: proto2 is supported for the core surface 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
-- Code generated by protoc-gen-tarantool. DO NOT EDIT.
-- source: hello.proto
-- syntax: proto3
-- package: hello

local pb = require("pb")
local wire = pb.wire

local M = {}

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

-- Enum: hello.Status
M.Status_descriptor = pb.enum("hello.Status", {
    UNKNOWN = 0,
    OK = 1,
    ERROR = 2,
})
M.Status = M.Status_descriptor.by_name

-- Pre-declare message descriptors so cross-references resolve.
M.Result_descriptor = {name = "hello.Result"}
M.HelloRequest_descriptor = {name = "hello.HelloRequest"}
M.HelloReply_descriptor = {name = "hello.HelloReply"}
M.Event_descriptor = {name = "hello.Event"}
M.Address_descriptor = {name = "hello.Address"}
M.Person_descriptor = {name = "hello.Person"}

-- Message: hello.Result
M.Result_descriptor.fields = {
    {name="id", id=1, kind='scalar', proto_type="int32"},
    {name="text", id=2, kind='scalar', proto_type="string", oneof="outcome"},
    {name="code", id=3, kind='scalar', proto_type="int32", oneof="outcome"},
    {name="details", id=4, kind='message', message=M.Address_descriptor, oneof="outcome"},
}
M.Result_descriptor.oneofs = {
    outcome = {"text", "code", "details"},
}
pb.finalize_message(M.Result_descriptor)
M.Result_fields = pb.field_names({
    id = "id",
    text = "text",
    code = "code",
    details = "details",
})
M.Result_oneofs = pb.field_names({
    outcome = "outcome",
})

-- Message: hello.HelloRequest
M.HelloRequest_descriptor.fields = {
    {name="name", id=1, kind='scalar', proto_type="string"},
}
pb.finalize_message(M.HelloRequest_descriptor)
M.HelloRequest_fields = pb.field_names({
    name = "name",
})

-- Message: hello.HelloReply
M.HelloReply_descriptor.fields = {
    {name="greeting", id=1, kind='scalar', proto_type="string"},
}
pb.finalize_message(M.HelloReply_descriptor)
M.HelloReply_fields = pb.field_names({
    greeting = "greeting",
})

-- Message: hello.Event
M.Event_descriptor.fields = {
    {name="title", id=1, kind='scalar', proto_type="string"},
    {name="created_at", id=2, kind='message', message=pb.wkt.Timestamp_descriptor},
    {name="duration", id=3, kind='message', message=pb.wkt.Duration_descriptor},
    {name="ack", id=4, kind='message', message=pb.wkt.Empty_descriptor},
    {name="retry_count", id=5, kind='message', message=pb.wkt.Int32Value_descriptor},
    {name="note", id=6, kind='message', message=pb.wkt.StringValue_descriptor},
    {name="is_admin", id=7, kind='message', message=pb.wkt.BoolValue_descriptor},
    {name="payload", id=8, kind='message', message=pb.wkt.Struct_descriptor},
    {name="attribute", id=9, kind='message', message=pb.wkt.Value_descriptor},
    {name="tags", id=10, kind='message', message=pb.wkt.ListValue_descriptor},
    {name="extension", id=11, kind='message', message=pb.wkt.Any_descriptor},
    {name="update_mask", id=12, kind='message', message=pb.wkt.FieldMask_descriptor},
}
pb.finalize_message(M.Event_descriptor)
M.Event_fields = pb.field_names({
    title = "title",
    created_at = "created_at",
    duration = "duration",
    ack = "ack",
    retry_count = "retry_count",
    note = "note",
    is_admin = "is_admin",
    payload = "payload",
    attribute = "attribute",
    tags = "tags",
    extension = "extension",
    update_mask = "update_mask",
})

-- Message: hello.Address
M.Address_descriptor.fields = {
    {name="street", id=1, kind='scalar', proto_type="string"},
    {name="city", id=2, kind='scalar', proto_type="string"},
    {name="zip", id=3, kind='scalar', proto_type="int32"},
    {name="apartment", id=4, kind='scalar', proto_type="string", optional=true},
}
pb.finalize_message(M.Address_descriptor)
M.Address_fields = pb.field_names({
    street = "street",
    city = "city",
    zip = "zip",
    apartment = "apartment",
})

-- Message: hello.Person
M.Person_descriptor.fields = {
    {name="name", id=1, kind='scalar', proto_type="string"},
    {name="age", id=2, kind='scalar', proto_type="int32"},
    {name="emails", id=3, kind='scalar', proto_type="string", repeated=true},
    {name="status", id=4, kind='enum', enum=M.Status_descriptor},
    {name="address", id=5, kind='message', message=M.Address_descriptor},
    {name="friends", id=6, kind='message', message=M.Person_descriptor, repeated=true},
    {name="lucky_numbers", id=7, kind='scalar', proto_type="int32", repeated=true, packed=true},
    {name="avatar", id=8, kind='scalar', proto_type="bytes"},
    {name="user_id", id=9, kind='scalar', proto_type="fixed64"},
    {name="balance", id=10, kind='scalar', proto_type="sint32"},
    {name="weight_kg", id=11, kind='scalar', proto_type="double"},
    {name="ages_by_nickname", id=13, kind='map', key={kind='scalar', proto_type="string"}, value={kind='scalar', proto_type="int32"}},
    {name="nickname_by_age", id=14, kind='map', key={kind='scalar', proto_type="int32"}, value={kind='scalar', proto_type="string"}},
    {name="addresses_by_label", id=15, kind='map', key={kind='scalar', proto_type="string"}, value={kind='message', message=M.Address_descriptor}},
}
pb.finalize_message(M.Person_descriptor)
M.Person_fields = pb.field_names({
    name = "name",
    age = "age",
    emails = "emails",
    status = "status",
    address = "address",
    friends = "friends",
    lucky_numbers = "lucky_numbers",
    avatar = "avatar",
    user_id = "user_id",
    balance = "balance",
    weight_kg = "weight_kg",
    ages_by_nickname = "ages_by_nickname",
    nickname_by_age = "nickname_by_age",
    addresses_by_label = "addresses_by_label",
})

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

--- Demo message for oneof handling.
---@class hello.Result
---@field id integer
---@field text? string
---@field code? integer
---@field details? hello.Address

--- gRPC service demo. Covers unary + all three streaming flavors so the
--- loopback transport exercises every codegen branch.
---@class hello.HelloRequest
---@field name string

---@class hello.HelloReply
---@field greeting string

--- Demo message exercising well-known types (M3).
---@class hello.Event
---@field title string
---@field created_at google.protobuf.Timestamp
---@field duration google.protobuf.Duration
---@field ack google.protobuf.Empty
---@field retry_count google.protobuf.Int32Value
---@field note google.protobuf.StringValue
---@field is_admin google.protobuf.BoolValue
---@field payload google.protobuf.Struct
---@field attribute google.protobuf.Value
---@field tags google.protobuf.ListValue
---@field extension google.protobuf.Any
---@field update_mask google.protobuf.FieldMask

---@class hello.Address
---@field street string
---@field city string
---@field zip integer
---@field apartment? string @ Explicit-optional: presence is meaningful (distinct from default).

---@class hello.Person
---@field name string
---@field age integer
---@field emails string[]
---@field status hello.Status
---@field address hello.Address
---@field friends hello.Person[]
---@field lucky_numbers integer[]
---@field avatar string
---@field user_id integer
---@field balance integer
---@field weight_kg number
---@field ages_by_nickname table<string, integer> @ Map fields (M2)
---@field nickname_by_age table<integer, string>
---@field addresses_by_label table<string, hello.Address>

---@param t? hello.Result
---@return hello.Result
function M.Result_new(t) return t or {} end
---@param t hello.Result
---@return string
function M.Result_encode(t) return pb.encode(M.Result_descriptor, t) end
---@param b string
---@return hello.Result
function M.Result_decode(b) return pb.decode(M.Result_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.Result_decode_lazy(b) return pb.decode_lazy(M.Result_descriptor, b) end
---@param t hello.Result
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.Result_text(t, opts) return pb.text.encode(M.Result_descriptor, t, opts) end

---@param t? hello.HelloRequest
---@return hello.HelloRequest
function M.HelloRequest_new(t) return t or {} end
---@param t hello.HelloRequest
---@return string
function M.HelloRequest_encode(t) return pb.encode(M.HelloRequest_descriptor, t) end
---@param b string
---@return hello.HelloRequest
function M.HelloRequest_decode(b) return pb.decode(M.HelloRequest_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.HelloRequest_decode_lazy(b) return pb.decode_lazy(M.HelloRequest_descriptor, b) end
---@param t hello.HelloRequest
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.HelloRequest_text(t, opts) return pb.text.encode(M.HelloRequest_descriptor, t, opts) end

---@param t? hello.HelloReply
---@return hello.HelloReply
function M.HelloReply_new(t) return t or {} end
---@param t hello.HelloReply
---@return string
function M.HelloReply_encode(t) return pb.encode(M.HelloReply_descriptor, t) end
---@param b string
---@return hello.HelloReply
function M.HelloReply_decode(b) return pb.decode(M.HelloReply_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.HelloReply_decode_lazy(b) return pb.decode_lazy(M.HelloReply_descriptor, b) end
---@param t hello.HelloReply
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.HelloReply_text(t, opts) return pb.text.encode(M.HelloReply_descriptor, t, opts) end

---@param t? hello.Event
---@return hello.Event
function M.Event_new(t) return t or {} end
---@param t hello.Event
---@return string
function M.Event_encode(t) return pb.encode(M.Event_descriptor, t) end
---@param b string
---@return hello.Event
function M.Event_decode(b) return pb.decode(M.Event_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.Event_decode_lazy(b) return pb.decode_lazy(M.Event_descriptor, b) end
---@param t hello.Event
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.Event_text(t, opts) return pb.text.encode(M.Event_descriptor, t, opts) end

---@param t? hello.Address
---@return hello.Address
function M.Address_new(t) return t or {} end
---@param t hello.Address
---@return string
function M.Address_encode(t) return pb.encode(M.Address_descriptor, t) end
---@param b string
---@return hello.Address
function M.Address_decode(b) return pb.decode(M.Address_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.Address_decode_lazy(b) return pb.decode_lazy(M.Address_descriptor, b) end
---@param t hello.Address
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.Address_text(t, opts) return pb.text.encode(M.Address_descriptor, t, opts) end
---@param t hello.Address
---@return boolean
function M.Address_has_apartment(t) return t.apartment ~= nil end
---@param t hello.Address
function M.Address_clear_apartment(t) t.apartment = nil end

---@param t? hello.Person
---@return hello.Person
function M.Person_new(t) return t or {} end
---@param t hello.Person
---@return string
function M.Person_encode(t) return pb.encode(M.Person_descriptor, t) end
---@param b string
---@return hello.Person
function M.Person_decode(b) return pb.decode(M.Person_descriptor, b) end
---@param b string
---@return pb.MessageView
function M.Person_decode_lazy(b) return pb.decode_lazy(M.Person_descriptor, b) end
---@param t hello.Person
---@param opts? {single_line: boolean?, indent: string?}
---@return string
function M.Person_text(t, opts) return pb.text.encode(M.Person_descriptor, t, opts) end

-- Service: hello.Greeter
M.Greeter_service = {
    name = "hello.Greeter",
    full_name = "/hello.Greeter",
    methods = {
        SayHello = {
            name = "SayHello",
            full_name = "/hello.Greeter/SayHello",
            input = M.HelloRequest_descriptor,
            output = M.HelloReply_descriptor,
        },
        Echo = {
            name = "Echo",
            full_name = "/hello.Greeter/Echo",
            input = M.HelloRequest_descriptor,
            output = M.HelloRequest_descriptor,
        },
        -- Server-streaming: one request, server pushes N replies.
        StreamHellos = {
            name = "StreamHellos",
            full_name = "/hello.Greeter/StreamHellos",
            input = M.HelloRequest_descriptor,
            output = M.HelloReply_descriptor,
            server_streaming = true,
        },
        -- Client-streaming: client pushes N requests, server returns one reply.
        CollectHellos = {
            name = "CollectHellos",
            full_name = "/hello.Greeter/CollectHellos",
            input = M.HelloRequest_descriptor,
            output = M.HelloReply_descriptor,
            client_streaming = true,
        },
        -- Bidirectional: both sides push and pull independently.
        Chat = {
            name = "Chat",
            full_name = "/hello.Greeter/Chat",
            input = M.HelloRequest_descriptor,
            output = M.HelloReply_descriptor,
            client_streaming = true,
            server_streaming = true,
        },
    },
}

function M.Greeter_client(transport)
    if transport == nil then error("Greeter_client: transport is required", 0) end
    return {
        SayHello = function(req, ctx)
            local req_bytes = M.HelloRequest_encode(req)
            local resp_bytes = transport:unary("/hello.Greeter/SayHello", req_bytes, ctx)
            return M.HelloReply_decode(resp_bytes)
        end,
        Echo = function(req, ctx)
            local req_bytes = M.HelloRequest_encode(req)
            local resp_bytes = transport:unary("/hello.Greeter/Echo", req_bytes, ctx)
            return M.HelloRequest_decode(resp_bytes)
        end,
        -- Server-streaming: one request, server pushes N replies.
        StreamHellos = function(req, ctx)
            local req_bytes = M.HelloRequest_encode(req)
            local raw = transport:server_stream("/hello.Greeter/StreamHellos", req_bytes, ctx)
            return pb.grpc.wrap_server_stream(raw, M.HelloReply_decode)
        end,
        -- Client-streaming: client pushes N requests, server returns one reply.
        CollectHellos = function(ctx)
            local raw = transport:client_stream("/hello.Greeter/CollectHellos", ctx)
            return pb.grpc.wrap_call(raw, M.HelloRequest_encode, M.HelloReply_decode)
        end,
        -- Bidirectional: both sides push and pull independently.
        Chat = function(ctx)
            local raw = transport:bidi("/hello.Greeter/Chat", ctx)
            return pb.grpc.wrap_call(raw, M.HelloRequest_encode, M.HelloReply_decode)
        end,
    }
end

function M.Greeter_server(impl)
    if type(impl) ~= 'table' then error("Greeter_server: impl table is required", 0) end
    return {
        service = M.Greeter_service,
        methods = {
            ["/hello.Greeter/SayHello"] = function(req_bytes, ctx)
                local handler = impl.SayHello
                if handler == nil then error("Greeter.SayHello: handler missing", 0) end
                local req = M.HelloRequest_decode(req_bytes)
                local resp = handler(req, ctx)
                return M.HelloReply_encode(resp)
            end,
            ["/hello.Greeter/Echo"] = function(req_bytes, ctx)
                local handler = impl.Echo
                if handler == nil then error("Greeter.Echo: handler missing", 0) end
                local req = M.HelloRequest_decode(req_bytes)
                local resp = handler(req, ctx)
                return M.HelloRequest_encode(resp)
            end,
        },
        streams = {
            -- Server-streaming: one request, server pushes N replies.
            ["/hello.Greeter/StreamHellos"] = {
                kind = 'server_stream',
                handler = function(req_bytes, server_view, ctx)
                    local handler = impl.StreamHellos
                    if handler == nil then error("Greeter.StreamHellos: handler missing", 0) end
                    local req = M.HelloRequest_decode(req_bytes)
                    local wrapped = pb.grpc.wrap_server_view(server_view, nil, M.HelloReply_encode)
                    handler(req, wrapped, ctx)
                end,
            },
            -- Client-streaming: client pushes N requests, server returns one reply.
            ["/hello.Greeter/CollectHellos"] = {
                kind = 'client_stream',
                handler = function(_, server_view, ctx)
                    local handler = impl.CollectHellos
                    if handler == nil then error("Greeter.CollectHellos: handler missing", 0) end
                    local wrapped = pb.grpc.wrap_server_view(server_view, M.HelloRequest_decode, nil)
                    local resp = handler(wrapped, ctx)
                    if resp == nil then error("Greeter.CollectHellos: handler returned nil response", 0) end
                    server_view:send(M.HelloReply_encode(resp))
                end,
            },
            -- Bidirectional: both sides push and pull independently.
            ["/hello.Greeter/Chat"] = {
                kind = 'bidi',
                handler = function(_, server_view, ctx)
                    local handler = impl.Chat
                    if handler == nil then error("Greeter.Chat: handler missing", 0) end
                    local wrapped = pb.grpc.wrap_server_view(server_view, M.HelloRequest_decode, M.HelloReply_encode)
                    handler(wrapped, ctx)
                end,
            },
        },
    }
end

return M