~bigbes/tarantool

tarantool-protobuf

ref: 79efcdc964e67b13cd7c9471b3dc341928979a57 tarantool-protobuf/examples/expected/full/hello/hello_pb.lua -rw-r--r-- 39.8 KiB
79efcdc9 — Eugene Blikh docs: 100% proto2 + proto3 conformance 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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
-- 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)
    if type(t) ~= 'table' then
        error("expected table for hello.Result, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    local _of_outcome
    if t.text ~= nil then _of_outcome = "text" end
    if t.code ~= nil then _of_outcome = "code" end
    if t.details ~= nil then _of_outcome = "details" end
    -- 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: text
    v = t.text
    if _of_outcome == "text" 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: code
    v = t.code
    if _of_outcome == "code" then
        n = n + 1; out[n] = "\x18"
        n = n + 1; out[n] = wire.encode_int32(v)
    end
    -- field 4: details
    v = t.details
    if _of_outcome == "details" then
        local _b = M.Address_encode(v)
        n = n + 1; out[n] = "\x22"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    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 hello.Result
function M.Result_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.Result 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.text = val
            result.code = nil
            result.details = nil
        elseif id == 3 then
            local val
            val, pos = wire.decode_int32(buf, pos)
            result.code = val
            result.text = nil
            result.details = nil
        elseif id == 4 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            local prev = result.details
            if prev == nil then
                result.details = M.Address_decode(payload)
            else
                pb.codec.merge_message(M.Address_descriptor, prev, M.Address_decode(payload))
            end
            result.text = nil
            result.code = nil
        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.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)
    if type(t) ~= 'table' then
        error("expected table for hello.HelloRequest, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: name
    v = t.name
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x0a"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    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 hello.HelloRequest
function M.HelloRequest_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.HelloRequest 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_string(buf, pos)
            result.name = 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.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)
    if type(t) ~= 'table' then
        error("expected table for hello.HelloReply, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: greeting
    v = t.greeting
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x0a"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    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 hello.HelloReply
function M.HelloReply_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.HelloReply 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_string(buf, pos)
            result.greeting = 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.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)
    if type(t) ~= 'table' then
        error("expected table for hello.Event, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: title
    v = t.title
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x0a"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    end
    -- field 2: created_at
    v = t.created_at
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Timestamp_encode(v)
        n = n + 1; out[n] = "\x12"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 3: duration
    v = t.duration
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Duration_encode(v)
        n = n + 1; out[n] = "\x1a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 4: ack
    v = t.ack
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Empty_encode(v)
        n = n + 1; out[n] = "\x22"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 5: retry_count
    v = t.retry_count
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Int32Value_encode(v)
        n = n + 1; out[n] = "\x2a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 6: note
    v = t.note
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.StringValue_encode(v)
        n = n + 1; out[n] = "\x32"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 7: is_admin
    v = t.is_admin
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.BoolValue_encode(v)
        n = n + 1; out[n] = "\x3a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 8: payload
    v = t.payload
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Struct_encode(v)
        n = n + 1; out[n] = "\x42"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 9: attribute
    v = t.attribute
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Value_encode(v)
        n = n + 1; out[n] = "\x4a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 10: tags
    v = t.tags
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.ListValue_encode(v)
        n = n + 1; out[n] = "\x52"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 11: extension
    v = t.extension
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.Any_encode(v)
        n = n + 1; out[n] = "\x5a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 12: update_mask
    v = t.update_mask
    if v ~= nil or type(v) == 'cdata' then
        local _b = pb.wkt.FieldMask_encode(v)
        n = n + 1; out[n] = "\x62"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    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 hello.Event
function M.Event_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.Event 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_string(buf, pos)
            result.title = val
        elseif id == 2 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.created_at = pb.wkt.Timestamp_decode(payload)
        elseif id == 3 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.duration = pb.wkt.Duration_decode(payload)
        elseif id == 4 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.ack = pb.wkt.Empty_decode(payload)
        elseif id == 5 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.retry_count = pb.wkt.Int32Value_decode(payload)
        elseif id == 6 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.note = pb.wkt.StringValue_decode(payload)
        elseif id == 7 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.is_admin = pb.wkt.BoolValue_decode(payload)
        elseif id == 8 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.payload = pb.wkt.Struct_decode(payload)
        elseif id == 9 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.attribute = pb.wkt.Value_decode(payload)
        elseif id == 10 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.tags = pb.wkt.ListValue_decode(payload)
        elseif id == 11 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.extension = pb.wkt.Any_decode(payload)
        elseif id == 12 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            result.update_mask = pb.wkt.FieldMask_decode(payload)
        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.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)
    if type(t) ~= 'table' then
        error("expected table for hello.Address, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: street
    v = t.street
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x0a"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    end
    -- field 2: city
    v = t.city
    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: zip
    v = t.zip
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x18"
        n = n + 1; out[n] = wire.encode_int32(v)
    end
    -- field 4: apartment
    v = t.apartment
    if v ~= nil then
        n = n + 1; out[n] = "\x22"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    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 hello.Address
function M.Address_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.Address 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_string(buf, pos)
            result.street = val
        elseif id == 2 then
            local val
            val, pos = wire.decode_string(buf, pos)
            result.city = val
        elseif id == 3 then
            local val
            val, pos = wire.decode_int32(buf, pos)
            result.zip = val
        elseif id == 4 then
            local val
            val, pos = wire.decode_string(buf, pos)
            result.apartment = 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.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)
    if type(t) ~= 'table' then
        error("expected table for hello.Person, got " .. type(t), 0)
    end
    local out, n = {}, 0
    local v
    -- field 1: name
    v = t.name
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x0a"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    end
    -- field 2: age
    v = t.age
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x10"
        n = n + 1; out[n] = wire.encode_int32(v)
    end
    -- field 3: emails
    v = t.emails
    if v ~= nil and #v > 0 then
        local _tag = "\x1a"
        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
    -- field 4: status
    v = t.status
    if v ~= nil then
        local nv = v
        if type(v) == 'string' then
            nv = M.Status[v]
            if nv == nil then error("unknown enum value '" .. v .. "' for hello.Status", 0) end
        end
        if nv ~= 0 then
            n = n + 1; out[n] = "\x20"
            n = n + 1; out[n] = wire.encode_int32(nv)
        end
    end
    -- field 5: address
    v = t.address
    if v ~= nil or type(v) == 'cdata' then
        local _b = M.Address_encode(v)
        n = n + 1; out[n] = "\x2a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 6: friends
    v = t.friends
    if v ~= nil and #v > 0 then
        local _tag = "\x32"
        for _i = 1, #v do
            local _b = M.Person_encode(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
    -- field 7: lucky_numbers
    v = t.lucky_numbers
    if v ~= nil and #v > 0 then
        local parts, m = {}, 0
        for _i = 1, #v do
            m = m + 1; parts[m] = wire.encode_int32(v[_i])
        end
        local _b = table.concat(parts)
        n = n + 1; out[n] = "\x3a"
        n = n + 1; out[n] = wire.encode_varint(#_b)
        n = n + 1; out[n] = _b
    end
    -- field 8: avatar
    v = t.avatar
    if v ~= nil and v ~= '' then
        n = n + 1; out[n] = "\x42"
        n = n + 1; out[n] = wire.encode_varint(#v)
        n = n + 1; out[n] = v
    end
    -- field 9: user_id
    v = t.user_id
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x49"
        n = n + 1; out[n] = wire.encode_fixed64(v)
    end
    -- field 10: balance
    v = t.balance
    if v ~= nil and v ~= 0 then
        n = n + 1; out[n] = "\x50"
        n = n + 1; out[n] = wire.encode_sint32(v)
    end
    -- field 11: weight_kg
    v = t.weight_kg
    if v ~= nil and (v ~= 0 or 1/v == -math.huge) then
        n = n + 1; out[n] = "\x59"
        n = n + 1; out[n] = wire.encode_double(v)
    end
    -- field 13: ages_by_nickname
    v = t.ages_by_nickname
    if v ~= nil and next(v) ~= nil then
        local _tag, _ktag, _vtag = "\x6a", "\x0a", "\x10"
        for _k, _val in pairs(v) do
            local entry, _m = {}, 0
            if _k ~= '' then
                _m = _m + 1; entry[_m] = _ktag
                _m = _m + 1; entry[_m] = wire.encode_string(_k)
            end
            if _val ~= 0 then
                _m = _m + 1; entry[_m] = _vtag
                _m = _m + 1; entry[_m] = wire.encode_int32(_val)
            end
            n = n + 1; out[n] = _tag
            n = n + 1; out[n] = wire.encode_len(table.concat(entry))
        end
    end
    -- field 14: nickname_by_age
    v = t.nickname_by_age
    if v ~= nil and next(v) ~= nil then
        local _tag, _ktag, _vtag = "\x72", "\x08", "\x12"
        for _k, _val in pairs(v) do
            local entry, _m = {}, 0
            if _k ~= 0 then
                _m = _m + 1; entry[_m] = _ktag
                _m = _m + 1; entry[_m] = wire.encode_int32(_k)
            end
            if _val ~= '' then
                _m = _m + 1; entry[_m] = _vtag
                _m = _m + 1; entry[_m] = wire.encode_string(_val)
            end
            n = n + 1; out[n] = _tag
            n = n + 1; out[n] = wire.encode_len(table.concat(entry))
        end
    end
    -- field 15: addresses_by_label
    v = t.addresses_by_label
    if v ~= nil and next(v) ~= nil then
        local _tag, _ktag, _vtag = "\x7a", "\x0a", "\x12"
        for _k, _val in pairs(v) do
            local entry, _m = {}, 0
            if _k ~= '' then
                _m = _m + 1; entry[_m] = _ktag
                _m = _m + 1; entry[_m] = wire.encode_string(_k)
            end
            if _val ~= nil then
                _m = _m + 1; entry[_m] = _vtag
                _m = _m + 1; entry[_m] = wire.encode_len(M.Address_encode(_val))
            end
            n = n + 1; out[n] = _tag
            n = n + 1; out[n] = wire.encode_len(table.concat(entry))
        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 hello.Person
function M.Person_decode(buf)
    if type(buf) ~= 'string' then
        error("expected string for hello.Person 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_string(buf, pos)
            result.name = val
        elseif id == 2 then
            local val
            val, pos = wire.decode_int32(buf, pos)
            result.age = val
        elseif id == 3 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
        elseif id == 4 then
            local u
            u, pos = wire.decode_varint(buf, pos)
            result.status = wire.varint_to_int32(u)
        elseif id == 5 then
            local payload
            payload, pos = wire.decode_len(buf, pos)
            local prev = result.address
            if prev == nil then
                result.address = M.Address_decode(payload)
            else
                pb.codec.merge_message(M.Address_descriptor, prev, M.Address_decode(payload))
            end
        elseif id == 6 then
            local list = result.friends
            if list == nil then list = {}; result.friends = list end
            local payload
            payload, pos = wire.decode_len(buf, pos)
            list[#list + 1] = M.Person_decode(payload)
        elseif id == 7 then
            local list = result.lucky_numbers
            if list == nil then list = {}; result.lucky_numbers = list end
            if wt == 2 then
                local payload
                payload, pos = wire.decode_len(buf, pos)
                local p2, lim = 1, #payload
                while p2 <= lim do
                    local val
                    val, p2 = wire.decode_int32(payload, p2)
                    list[#list + 1] = val
                end
            else
                local val
                val, pos = wire.decode_int32(buf, pos)
                list[#list + 1] = val
            end
        elseif id == 8 then
            local val
            val, pos = wire.decode_bytes(buf, pos)
            result.avatar = val
        elseif id == 9 then
            local val
            val, pos = wire.decode_fixed64(buf, pos)
            result.user_id = val
        elseif id == 10 then
            local val
            val, pos = wire.decode_sint32(buf, pos)
            result.balance = val
        elseif id == 11 then
            local val
            val, pos = wire.decode_double(buf, pos)
            result.weight_kg = val
        elseif id == 13 then
            local map = result.ages_by_nickname
            if map == nil then map = {}; result.ages_by_nickname = map end
            local payload
            payload, pos = wire.decode_len(buf, pos)
            local _ep, _elim = 1, #payload
            local _key, _val = '', 0
            while _ep <= _elim do
                local eid, ewt
                eid, ewt, _ep = wire.decode_tag(payload, _ep)
                if eid == 1 then
                    _key, _ep = wire.decode_string(payload, _ep)
                elseif eid == 2 then
                    _val, _ep = wire.decode_int32(payload, _ep)
                else
                    _ep = wire.skip_field(payload, _ep, ewt, eid)
                end
            end
            map[_key] = _val
        elseif id == 14 then
            local map = result.nickname_by_age
            if map == nil then map = {}; result.nickname_by_age = map end
            local payload
            payload, pos = wire.decode_len(buf, pos)
            local _ep, _elim = 1, #payload
            local _key, _val = 0, ''
            while _ep <= _elim do
                local eid, ewt
                eid, ewt, _ep = wire.decode_tag(payload, _ep)
                if eid == 1 then
                    _key, _ep = wire.decode_int32(payload, _ep)
                elseif eid == 2 then
                    _val, _ep = wire.decode_string(payload, _ep)
                else
                    _ep = wire.skip_field(payload, _ep, ewt, eid)
                end
            end
            map[_key] = _val
        elseif id == 15 then
            local map = result.addresses_by_label
            if map == nil then map = {}; result.addresses_by_label = map end
            local payload
            payload, pos = wire.decode_len(buf, pos)
            local _ep, _elim = 1, #payload
            local _key, _val = '', {}
            while _ep <= _elim do
                local eid, ewt
                eid, ewt, _ep = wire.decode_tag(payload, _ep)
                if eid == 1 then
                    _key, _ep = wire.decode_string(payload, _ep)
                elseif eid == 2 then
                    local _payload
                    _payload, _ep = wire.decode_len(payload, _ep)
                    _val = M.Address_decode(_payload)
                else
                    _ep = wire.skip_field(payload, _ep, ewt, eid)
                end
            end
            map[_key] = _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.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