-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathep_gpb_tests.erl
1304 lines (1213 loc) · 52.1 KB
/
ep_gpb_tests.erl
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
%%% Copyright (C) 2010-2011 Tomas Abrahamsson
%%%
%%% Author: Tomas Abrahamsson <[email protected]>
%%%
%%% This library is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU Lesser General Public
%%% License as published by the Free Software Foundation; either
%%% version 2.1 of the License, or (at your option) any later version.
%%%
%%% This library is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% Lesser General Public License for more details.
%%%
%%% You should have received a copy of the GNU Lesser General Public
%%% License along with this library; if not, write to the Free Software
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
%%% MA 02110-1301 USA
%% origin: https://github.com/tomas-abrahamsson/gpb/blob/master/test/gpb_tests.erl
-module(ep_gpb_tests).
-include_lib("eunit/include/eunit.hrl").
-include_lib("gpb/include/gpb.hrl").
-record(m1, {a}).
-record(m2, {b}).
-record(m4, {x, y}).
decode_msg(Bin, Name, Defs) ->
decode_msg(Bin, Name, Defs, [{string_as_list, true}, {with_utf8, true}]).
decode_msg(Bin, Name, Defs, Opts) ->
enif_protobuf:set_opts(Opts),
enif_protobuf:load_cache(Defs),
enif_protobuf:decode(Bin, Name).
encode_msg(Msg, Defs) ->
encode_msg(Msg, Defs, [{string_as_list, true}, {with_utf8, true}]).
encode_msg(Msg, Defs, Opts) ->
enif_protobuf:set_opts(Opts),
enif_protobuf:load_cache(Defs),
enif_protobuf:encode(Msg).
skipping_unknown_varint_field_test() ->
#m1{a = undefined} =
decode_msg(<<32, 150, 1>>, %% field number 4 (not known), wire type = 0
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]).
skipping_unknown_length_delimited_field_test() ->
#m1{a = undefined} =
decode_msg(<<34, 1, 1>>, %% field number 4 (not known), wire type = 2
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]).
skipping_unknown_64bit_field_test() ->
#m1{a = undefined} =
decode_msg(<<33, 0, 0, 0, 0, 0, 0, 0, 0>>, %% field number 4, wire type = 1
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]).
skipping_unknown_32bit_field_test() ->
#m1{a = undefined} =
decode_msg(<<37, 0, 0, 0, 0>>, %% field number 4, wire type = 5
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]).
skipping_with_oneof_test() ->
Defs = [{{msg, m1}, [#gpb_oneof{
name = a, rnum = #m1.a,
fields = [#?gpb_field{name = a1, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []},
#?gpb_field{name = a2, fnum = 2, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]}],
#m1{a = undefined} = decode_msg(<<32, 150, 1>>, m1, Defs).
skipping_groups() ->
DefsO = [{{msg, x1}, [Field1 = #?gpb_field{name = f1, fnum = 1, rnum = 2,
type = uint32, occurrence = required,
opts = []}]}],
%% message with a group in a group
DefsN = [{{msg, x1}, [Field1,
#?gpb_field{name = g, fnum = 2, rnum = 3,
type = {group, 'x1.g1'}, occurrence = optional,
opts = []}]},
{{group, 'x1.g1'}, [#?gpb_field{
name = g1f, fnum = 3, rnum = 2,
type = {group, 'x1.g2'}, occurrence = required,
opts = []}]},
{{group, 'x1.g2'}, [#?gpb_field{
name = g2f, fnum = 4, rnum = 2,
type = uint32, occurrence = required,
opts = []}]}],
X1 = encode_msg({x1, 38, {'x1.g1', {'x1.g2', 17}}}, DefsN),
{x1, 38} = decode_msg(X1, x1, DefsO).
decode_msg_simple_occurrence_test() ->
#m1{a = undefined} =
decode_msg(<<>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]),
#m1{a = 150} =
decode_msg(<<8, 150, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = required,
opts = []}]}]),
#m1{a = [150, 151]} =
decode_msg(<<8, 150, 1, 8, 151, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = []}]}]),
ok.
decode_msg_with_enum_field_test() ->
#m1{a = v2} =
decode_msg(<<8, 150, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v1, 100},
{v2, 150}]}]).
decode_msg_with_negative_enum_value_test() ->
#m1{a = v2} =
decode_msg(<<8, 254, 255, 255, 255, 15>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v1, 100},
{v2, -2}]}]),
#m1{a = v2} =
decode_msg(<<8, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v1, 100},
{v2, -2}]}]).
decode_msg_with_enum_aliases_test() ->
#m1{a = v1} =
decode_msg(<<8, 100>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{option, allow_alias, true},
{v1, 100},
{v2, 100}]}]).
decode_unknown_enum_test() ->
#m1{a = 4711} =
decode_msg(<<8, 231, 36>>, m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v0, 0}]}]),
#m1{a = [4711]} =
decode_msg(<<8, 231, 36>>, m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = repeated,
opts = []}]},
{{enum, e}, [{v0, 0}]}]),
#m1{a = [4711]} =
decode_msg(<<10, 2, 231, 36>>, m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = repeated,
opts = [packed]}]},
{{enum, e}, [{v0, 0}]}]).
decode_msg_with_bool_field_test() ->
#m1{a = true} =
decode_msg(<<8, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]),
#m1{a = false} =
decode_msg(<<8, 0>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]).
decoding_float_test() ->
%% Stole idea from the python test in google-protobuf:
%% 1.125 is perfectly representable as a float (no rounding error).
#m1{a = 1.125} =
decode_msg(<<13, 0, 0, 144, 63>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = float, occurrence = required,
opts = []}]}]).
decoding_double_test() ->
#m1{a = 1.125} =
decode_msg(<<9, 0, 0, 0, 0, 0, 0, 242, 63>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = double, occurrence = required,
opts = []}]}]).
decode_msg_with_string_field_test() ->
#m1{a = "abc\345\344\366" ++ [1022]} =
decode_msg(<<10, 11,
$a, $b, $c, $\303, $\245, $\303, $\244, $\303, $\266, $\317, $\276>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = string, occurrence = required,
opts = []}]}]).
decode_msg_with_bytes_field_test() ->
#m1{a = <<0, 0, 0, 0>>} =
decode_msg(<<10, 4, 0, 0, 0, 0>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bytes, occurrence = required,
opts = []}]}]).
decode_msg_with_sub_msg_field_test() ->
#m1{a = #m2{b = 150}} =
decode_msg(<<10, 3, 8, 150, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {msg, m2}, occurrence = required,
opts = []}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = uint32, occurrence = required,
opts = []}]}]).
decode_msg_with_optional_nonpresent_sub_msg_field_test() ->
#m1{a = undefined} =
decode_msg(<<>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {msg, m2}, occurrence = optional,
opts = []}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = uint32, occurrence = required,
opts = []}]}]).
decoding_zero_instances_of_packed_varints_test() ->
%% "A packed repeated field containing zero elements does not
%% appear in the encoded message."
%% -- http://code.google.com/apis/protocolbuffers/docs/encoding.html
#m1{a = []} =
decode_msg(<<>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = [packed]}]}]).
decoding_one_packed_chunk_of_varints_test() ->
#m1{a = [3, 270, 86942]} =
decode_msg(<<16#22, % tag (field number 4, wire type 2)
16#06, % payload size (6 bytes)
16#03, % first element (varint 3)
16#8E, 16#02, % second element (varint 270)
16#9E, 16#a7, 16#05>>, % third element (varint 86942)
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 4, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = [packed]}]}]).
decoding_two_packed_chunks_of_varints_test() ->
%% "Note that although there's usually no reason to encode more
%% than one key-value pair for a packed repeated field, encoders
%% must be prepared to accept multiple key-value pairs. In this
%% case, the payloads should be concatenated. Each pair must
%% contain a whole number of elements."
%% -- http://code.google.com/apis/protocolbuffers/docs/encoding.html
#m1{a = [3, 270, 86942, 4, 271, 86943]} =
decode_msg(<<16#22, 16#06, 16#03, 16#8E, 16#02, 16#9E, 16#a7, 16#05,
16#22, 16#06, 16#04, 16#8F, 16#02, 16#9F, 16#a7, 16#05>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 4, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = [packed]}]}]),
ok.
decode_skips_nonpacked_fields_if_wiretype_mismatches_test() ->
#m1{a = undefined} =
decode_msg(<<9, %% 9 means wiretype=bits64 instead of expected varint
0:64>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = optional,
opts = []}]}]).
decode_skips_packed_fields_if_wiretype_mismatches_test() ->
#m1{a = []} =
decode_msg(<<9, %% 9 means wiretype=bits64 instead of expected varint
0:64>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = repeated,
opts = [packed]}]}]).
decode_of_field_fails_for_invalid_varints_test() ->
ViMax64 = gpb:encode_varint(16#ffffFFFFffffFFFF),
FDef = #?gpb_field{name = a, fnum = 1, rnum = #m1.a, type = bool,
occurrence = required, opts = []},
%% Verify fail on invalid field number + type
?assertError(_, decode_msg(<<255, ViMax64/binary, 1>>,
m1,
[{{msg, m1}, [FDef]}])),
%% Verify fail on invalid field bools
?assertError(_, decode_msg(<<8, %% field num = 1, wire type = varint
255, ViMax64/binary %% too many bits
>>,
m1,
[{{msg, m1}, [FDef]}])),
%% Verify fail on invalid field enums (enums are 32 bits signed,
%% but might be sent over the wire as 64 bits signed, but are
%% to be truncated to 32 bits before interpretation.
?assertError(_, decode_msg(<<8, %% field num = 1, wire type = varint
255, ViMax64/binary %% too many bits
>>,
m1,
[{{msg, m1}, [FDef#?gpb_field{type = {enum, e}}]},
{{enum, e}, [{a, 1}]}])),
%% Verify fail on invalid length, for length delimited field types
[?assertError(_, decode_msg(<<10, %% field num = 1, wire type = len-delim
255, ViMax64/binary, %% too many length bits
1, 2, 3
>>,
m1,
[{{msg, m1}, [FDef#?gpb_field{type = T}]},
{{msg, m2}, [FDef]}]))
|| T <- [string, bytes, {msg, m2}]],
%% Verify fail on invalid 32-bit varint field types
[?assertError(_, decode_msg(<<8, %% field num = 1, wire type = varint
255, ViMax64/binary %% too many bits
>>,
m1,
[{{msg, m1}, [FDef#?gpb_field{type = T}]}]))
|| T <- [sint32, int32, uint32]],
%% Verify fail on invalid 64-bit varint field types
[?assertError(_, decode_msg(<<8, %% field num = 1, wire type = varint
255, ViMax64/binary %% too many bits
>>,
m1,
[{{msg, m1}, [FDef#?gpb_field{type = T}]}]))
|| T <- [sint64, int64, uint64]],
%% Verify fail on invalid length for packed repeated field
?assertError(_, decode_msg(<<8, %% field num = 1, wire type = varint
255, ViMax64/binary, %% too many length bits
1, 1, 1
>>,
m1,
[{{msg, m1}, [FDef#?gpb_field{occurrence = repeated,
opts = [packed]}]}])),
ok.
decoding_oneof_test() ->
Defs = [{{msg, m1}, [#gpb_oneof{
name = a, rnum = #m1.a,
fields = [#?gpb_field{name = a1, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []},
#?gpb_field{name = a2, fnum = 2, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]}],
#m1{a = undefined} = decode_msg(<<>>, m1, Defs),
#m1{a = {a1, 150}} = decode_msg(<<8, 150, 1>>, m1, Defs),
#m1{a = {a2, 150}} = decode_msg(<<16, 150, 1>>, m1, Defs).
decoding_oneof_with_merge_test() ->
Defs = [{{msg, m1}, [#gpb_oneof{
name = a, rnum = #m1.a,
fields = [#?gpb_field{name = x, fnum = 1, rnum = #m1.a,
type = {msg, m2}, occurrence = optional,
opts = []},
#?gpb_field{name = y, fnum = 2, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = fixed32, occurrence = repeated}]}],
B1 = encode_msg(#m1{a = {x, #m2{b = [1]}}}, Defs),
B2 = encode_msg(#m1{a = {x, #m2{b = [2]}}}, Defs),
B3 = encode_msg(#m1{a = {y, 150}}, Defs),
%% Will get b=[1,2] since messages are merged
#m1{a = {x, #m2{b = [1, 2]}}} = decode_msg(<<B1/binary, B2/binary>>, m1, Defs),
%% Different oneof fields ==> no merge
#m1{a = {y, 150}} = decode_msg(<<B1/binary, B3/binary>>, m1, Defs).
decoding_map_test() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, string, fixed32},
occurrence = repeated, opts = []}]}],
#m1{a = Map} = decode_msg(<<
%% first item: "x" => 17
10, %% map is a msg type item
8, %% sub msg len
10, %%% key-field (string)
1, "x", %%% len + key
21, %%% value-field (fixed32)
17:32/little, %%% value
%% second item: "y" => 18
10, 8,
10, 1, "y", %% key
21, 18:32/little %% value
>>,
m1,
Defs),
[{"x", 17}, {"y", 18}] = lists:sort(Map).
decoding_map_with_duplicate_keys_test() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, string, fixed32},
occurrence = repeated, opts = []}]}],
#m1{a = Map} = decode_msg(
%% A map with "x" => 16, (not to be included)
%% "x" => 17 (overrides "x" => 16)
%% and "y" => 18
<<10, 8, 10, 1, "x", 21, 16:32/little,
10, 8, 10, 1, "x", 21, 17:32/little,
10, 8, 10, 1, "y", 21, 18:32/little>>,
m1,
Defs),
[{"x", 17}, {"y", 18}] = lists:sort(Map).
decoding_map_with_keys_values_missing_should_get_type_defaults_test() ->
%% map keys and values are optional, and when decoded, should
%% get type-default values if missing.
%% This is the case also with syntax="proto2".
%% (a map with all values omitted could perhaps be regarded as a set,
%% but we can't express that right now)
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 3, rnum = #m1.a,
type = {map, string, uint32},
occurrence = repeated, opts = []}]},
{{msg, m1m}, % map<string,uint32> equivalent using bwd-compat msgs
[#?gpb_field{name = a, fnum = 3, rnum = #m1.a,
type = {msg, map_equiv},
occurrence = repeated, opts = []}]},
{{msg, map_equiv},
[#?gpb_field{name = k,
fnum = 1, rnum = 2,
type = string, occurrence = optional, opts = []},
#?gpb_field{name = v,
fnum = 2, rnum = 3,
type = uint32, occurrence = optional, opts = []}]}],
B1m = encode_msg({m1m, [{map_equiv, "abc", 17},
{map_equiv, undefined, 18},
{map_equiv, "def", undefined},
{map_equiv, undefined, undefined}]}, Defs),
{m1, Map} = decode_msg(B1m, m1, Defs),
[{"", _}, {"abc", 17}, {"def", 0}] = lists:sort(Map).
error_for_mapfield_with_missing_msgvalue_test() ->
%% It is an error if a value is missing for a map<_,_> type field,
%% and the type of the value is a message.
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 3, rnum = #m1.a,
type = {map, string, {msg, m2}},
occurrence = repeated, opts = []}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 4, rnum = #m2.b, type = uint32,
occurrence = required, opts = []}]},
{{msg, m1m}, % map<string,uint32> equivalent using bwd-compat msgs
[#?gpb_field{name = a, fnum = 3, rnum = #m1.a,
type = {msg, map_equiv},
occurrence = repeated, opts = []}]},
{{msg, map_equiv},
[#?gpb_field{name = k,
fnum = 1, rnum = 2,
type = string, occurrence = optional, opts = []},
#?gpb_field{name = v,
fnum = 2, rnum = 3,
type = {msg, m2}, occurrence = optional, opts = []}]}],
B1m = encode_msg({m1m, [{map_equiv, "abc", {m2, 17}},
{map_equiv, "def", undefined}]}, Defs),
?assertError(_, decode_msg(B1m, m1, Defs)).
decode_packed_repeated_with_without_packed_test() ->
{timeout, 10, fun decode_packed_repeated_with_without_packed_test_aux/0}.
decode_packed_repeated_with_without_packed_test_aux() ->
%% "Protocol buffer parsers must be able to parse repeated fields
%% that were compiled as packed as if they were not packed, and vice
%% versa. [...]"
%% -- https://developers.google.com/protocol-buffers/docs/encoding#packed
%%
%% Also: "Only repeated fields of primitive numeric types (types
%% which use the varint, 32-bit, or 64-bit wire types) can be
%% declared 'packed'."
TypesWithValues = [{uint32, [3, 270, 86942]},
{fixed32, [3, 270, 86942]},
{float, [32.0, 0.0, 1.125]},
{fixed64, [3, 270, 86942]}],
[begin
UF = #?gpb_field{name = a, fnum = 1, rnum = #m1.a, type = Type,
occurrence = repeated, opts = []},
PF = UF#?gpb_field{opts = [packed]},
UDefs = [{{msg, m1}, [UF]}],
PDefs = [{{msg, m1}, [PF]}],
BU = encode_msg({m1, Values}, UDefs),
BP = encode_msg({m1, Values}, PDefs),
{m1, Values} = decode_msg(BU, m1, PDefs), % dec unpacked with packed
{m1, Values} = decode_msg(BP, m1, UDefs) % ... and vice versa
end
|| {Type, Values} <- TypesWithValues],
ok.
decoding_should_mask_to_bitlength_test() ->
%% This is the result of decoding too large values
%% with protobuf's generated Python and C++ output.
%% gpb works like the C++ decoder.
%%
%% Protobuf Encoded Python/C++
%% Type value decoded
%% on wire value
%% ----------------------------------------------------------------
%% uint32 2^32 0
%% uint32 2^33-1 4294967295
%% uint32 2^33 0
%% uint32 2^34-1 4294967295
%% uint64 2^64 0
%% uint64 2^65-1 18446744073709551615
%% uint64 2^65 0
%% uint64 2^66-1 18446744073709551615
%%
%% int32 2^32 0
%% int32 2^33-1 -1
%% int32 2^33 0
%% int32 2^34-1 -1
%% int64 2^64 0
%% int64 2^65-1 -1
%% int64 2^65 0
%% int64 2^66-1 -1
%%
%% sint32 2^32 0
%% sint32 2^33-1 -2147483648
%% sint32 2^33 0
%% sint32 2^34-1 -2147483648
%% sint64 2^64 0
%% sint64 2^65-1 -9223372036854775808
%% sint64 2^65 0
%% sint64 2^66-1 -9223372036854775808
%%
%% Here are the various versions involved:
%% libprotoc 3.15.3
%% Python 3.9.2
%% gcc (Debian 10.2.1-6) 10.2.1 20210110
%%
[begin
FNum = 1,
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = FNum, rnum = #m1.a,
type = Type, occurrence = required,
opts = []}]}],
WType = gpb:encode_wiretype(Type),
Encoded = iolist_to_binary(
[gpb:encode_varint((FNum bsl 3) + WType),
gpb:encode_varint(ValueToEncode)]),
Env = {Type, ValueToEncode, Encoded, Defs},
?assertMatch({#m1{a = ExpectedDecoded}, _},
{decode_msg(Encoded, m1, Defs), Env})
end
|| {ExpectedDecoded, ValueToEncode, Type}
<- [%% uint32,uint64
{0, pow2(32), uint32},
{max_uint(32), pow2(33) - 1, uint32},
{0, pow2(64), uint64},
{max_uint(64), pow2(65) - 1, uint64},
%% int32,int64
{0, pow2(32), int32},
{-1, pow2(33) - 1, int32},
{0, pow2(64), int64},
{-1, pow2(65) - 1, int64},
%% sint32,sint64
{0, pow2(32), sint32},
{min_sint(32), pow2(33) - 1, sint32},
{0, pow2(64), sint64},
{min_sint(64), pow2(65) - 1, sint64}]],
ok.
max_uint(NumBits) -> pow2(NumBits) - 1.
min_sint(NumBits) -> -pow2(NumBits - 1).
pow2(NumBits) -> (1 bsl NumBits).
%% -------------------------------------------------------------
encode_required_varint_field_test() ->
<<8, 150, 1>> =
encode_msg(#m1{a = 150},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = required,
opts = []}]}]).
encode_optional_varint_field_test() ->
<<>> =
encode_msg(#m1{a = undefined},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]).
encode_repeated_empty_field_test() ->
<<>> =
encode_msg(#m1{a = []},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = [packed]}]}]),
<<>> =
encode_msg(#m1{a = []},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = []}]}]).
encode_repeated_nonempty_field_test() ->
<<10, 4, 150, 1, 151, 1>> =
encode_msg(#m1{a = [150, 151]},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = [packed]}]}]),
<<8, 150, 1, 8, 151, 1>> =
encode_msg(#m1{a = [150, 151]},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = int32, occurrence = repeated,
opts = []}]}]).
encode_msg_with_sub_msg_field_test() ->
<<10, 3, 8, 150, 1>> =
encode_msg(#m1{a = #m2{b = 150}},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {msg, m2},
occurrence = required, opts = []}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = uint32, occurrence = required,
opts = []}]}]).
encode_msg_with_enum_field_test() ->
<<8, 150, 1>> =
encode_msg(#m1{a = v2},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v1, 100},
{v2, 150}]}]).
negative_int32_types_test() ->
%% Interop with other protobuf implementations:
%%
%% A value of -1 for an int32 can be encoded as:
%% <<8,255,255,255,255,15>>, but the Google protobuf (C++) encodes
%% it as <<8,255,255,255,255,255,255,255,255,255,1>>, ie using 64
%% bits.
%%
%% Google protobuf (C++) decodes both octet sequences to -1, but
%% there are other protobuf implementations that decode to
%% different values. So for interop, encode using 64 bits,
%% on decoding, accept both 32 and 64 bits.
%%
%% Enums are encoded as int32.
%%
%% The sint32 type is different.
%%
Defs = [{{msg, m4}, [#?gpb_field{name = x, fnum = 1, rnum = #m4.x,
type = int32, occurrence = optional,
opts = []},
#?gpb_field{name = y, fnum = 2, rnum = #m4.y,
type = {enum, e}, occurrence = optional,
opts = []}]},
{{enum, e}, [{z, 0},
{n2, -2},
{p2, 2}]}],
Minus2As64Bits = <<254, 255, 255, 255, 255, 255, 255, 255, 255, 1>>,
Minus2As32Bits = <<254, 255, 255, 255, 15>>,
X_Minus2As64Bits = <<8, Minus2As64Bits/binary>>,
X_Minus2As32Bits = <<8, Minus2As32Bits/binary>>,
Y_Minus2As64Bits = <<16, Minus2As64Bits/binary>>,
Y_Minus2As32Bits = <<16, Minus2As32Bits/binary>>,
%% Check int32
X_Minus2As64Bits = encode_msg(#m4{x = -2}, Defs),
#m4{x = -2} = decode_msg(X_Minus2As64Bits, m4, Defs),
#m4{x = -2} = decode_msg(X_Minus2As32Bits, m4, Defs),
%% Check enums
Y_Minus2As64Bits = encode_msg(#m4{y = n2}, Defs),
#m4{y = n2} = decode_msg(Y_Minus2As64Bits, m4, Defs),
#m4{y = n2} = decode_msg(Y_Minus2As32Bits, m4, Defs),
ok.
encode_msg_with_enum_aliases_test() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{option, allow_alias, true},
{v1, 100},
{v2, 100}]}],
<<8, 100>> = encode_msg(#m1{a = v1}, Defs),
<<8, 100>> = encode_msg(#m1{a = v2}, Defs).
encode_unknown_enum_test() ->
<<8, 231, 36>> =
encode_msg(#m1{a = 4711},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = required,
opts = []}]},
{{enum, e}, [{v0, 0}]}]),
<<8, 231, 36>> =
encode_msg(#m1{a = [4711]},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = repeated,
opts = []}]},
{{enum, e}, [{v0, 0}]}]),
<<10, 2, 231, 36>> =
encode_msg(#m1{a = [4711]},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {enum, e}, occurrence = repeated,
opts = [packed]}]},
{{enum, e}, [{v0, 0}]}]).
encode_msg_with_bool_field_test() ->
<<8, 1>> =
encode_msg(#m1{a = true},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]),
<<8, 1>> =
encode_msg(#m1{a = 1},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]),
<<8, 0>> =
encode_msg(#m1{a = false},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]),
<<8, 0>> =
encode_msg(#m1{a = 0},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = bool, occurrence = required,
opts = []}]}]).
encode_float_test() ->
%% Stole idea from the python test in google-protobuf:
%% 1.125 is perfectly representable as a float (no rounding error).
<<13, 0, 0, 144, 63>> =
encode_msg(#m1{a = 1.125},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = float, occurrence = required,
opts = []}]}]).
encode_packed_repeated_bools_test() ->
<<16#22, 1, 1>> =
encode_msg(#m1{a = [true]},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 4, rnum = #m1.a,
type = bool, occurrence = repeated,
opts = [packed]}]}]).
decode_packed_repeated_bools_test() ->
#m1{a = [true]} =
decode_msg(<<16#22, 1, 1>>,
m1,
[{{msg, m1}, [#?gpb_field{name = a, fnum = 4, rnum = #m1.a,
type = bool, occurrence = repeated,
opts = [packed]}]}]).
encode_double_test() ->
<<9, 0, 0, 0, 0, 0, 0, 242, 63>> =
encode_msg(#m1{a = 1.125},
[{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = double, occurrence = required,
opts = []}]}]).
encode_numbers_as_floats_doubles_test() ->
%% 16 is an integer which is also perfectly representable as an IEEE float
%% (no rounding error)
DefsF = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = float, occurrence = required,
opts = []}]}],
DefsD = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = double, occurrence = required,
opts = []}]}],
DefsFP = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = float, occurrence = repeated,
opts = [packed]}]}],
DefsDP = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = double, occurrence = repeated,
opts = [packed]}]}],
<<13, 0:16, 128, 65>> = encode_msg(#m1{a = 16}, DefsF),
<<13, 0:16, 128, 65>> = encode_msg(#m1{a = 16.0}, DefsF),
<<9, 0:48, 48, 64>> = encode_msg(#m1{a = 16}, DefsD),
<<9, 0:48, 48, 64>> = encode_msg(#m1{a = 16.0}, DefsD),
<<10, 4, 0:16, 128, 65>> = encode_msg(#m1{a = [16]}, DefsFP),
<<10, 4, 0:16, 128, 65>> = encode_msg(#m1{a = [16.0]}, DefsFP),
<<10, 8, 0:48, 48, 64>> = encode_msg(#m1{a = [16]}, DefsDP),
<<10, 8, 0:48, 48, 64>> = encode_msg(#m1{a = [16.0]}, DefsDP),
ok.
'+inf,-inf,nan_double_test'() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = double, occurrence = repeated,
opts = [packed]}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = double, occurrence = required,
opts = []}]}],
Msg = #m1{a = [infinity, '-infinity', nan]},
<<10, 24,
0:48, 16#f0, 16#7f,
0:48, 16#f0, 16#ff,
0:48, 16#f8, 16#7f>> = B = encode_msg(Msg, Defs),
Msg = decode_msg(B, m1, Defs),
Msg2 = #m2{b = infinity},
<<9, 0:48, 16#f0, 16#7f>> = B2 = encode_msg(Msg2, Defs),
Msg2 = decode_msg(B2, m2, Defs),
%% check that decode also recognizes other forms of NaN:
%% Both sets of fraction bits /= 0, and also sign bit /= 0:
B3 = <<10, 8, 4711:48, 16#ff, 16#ff>>,
#m1{a = [nan]} = decode_msg(B3, m1, Defs),
B4 = <<9, 4711:48, 16#ff, 16#ff>>,
#m2{b = nan} = decode_msg(B4, m2, Defs).
'+inf,-inf,nan_float_test'() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = float, occurrence = repeated,
opts = [packed]}]},
{{msg, m2}, [#?gpb_field{name = b, fnum = 1, rnum = #m2.b,
type = float, occurrence = required,
opts = []}]}],
Msg = #m1{a = [infinity, '-infinity', nan]},
<<10, 12,
0:16, 16#80, 16#7f,
0:16, 16#80, 16#ff,
0:16, 16#c0, 16#7f>> = B = encode_msg(Msg, Defs),
Msg = decode_msg(B, m1, Defs),
Msg2 = #m2{b = infinity},
<<13, 0:16, 16#80, 16#7f>> = B2 = encode_msg(Msg2, Defs),
Msg2 = decode_msg(B2, m2, Defs),
%% check that decode also recognizes other forms of NaN:
%% Both sets of fraction bits /= 0, and also sign bit /= 0:
B3 = <<10, 4, 4711:16, 16#ff, 16#ff>>,
#m1{a = [nan]} = decode_msg(B3, m1, Defs),
B4 = <<13, 4711:16, 16#ff, 16#ff>>,
#m2{b = nan} = decode_msg(B4, m2, Defs).
encode_oneof_test() ->
Defs = [{{msg, m1}, [#gpb_oneof{
name = a, rnum = #m1.a,
fields = [#?gpb_field{name = a1, fnum = 1, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []},
#?gpb_field{name = a2, fnum = 2, rnum = #m1.a,
type = int32, occurrence = optional,
opts = []}]}]}],
<<>> = encode_msg(#m1{a = undefined}, Defs),
<<8, 150, 1>> = encode_msg(#m1{a = {a1, 150}}, Defs),
<<16, 150, 1>> = encode_msg(#m1{a = {a2, 150}}, Defs).
encode_map_test() ->
Defs = [{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, string, fixed32},
occurrence = repeated, opts = []}]}],
<<10, 8,
10, 1, "x", %% key
21, 17:32/little, %% value
10, 8,
10, 1, "y", %% key
21, 18:32/little %% value
>> = encode_msg(#m1{a = [{"x", 17}, {"y", 18}]}, Defs),
%% Map items are present even if they have type-defaults
%% even if syntax="proto3"
P3Defs = [{syntax, "proto3"},
{proto3_msgs, [m1]},
{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, string, uint32},
occurrence = repeated, opts = []}]}],
<<10, 4, 10, 0, 16, 0>> = encode_msg(#m1{a = [{"", 0}]}, P3Defs),
%% ^^^^ ^^^^
%% Key Value
%% Map fields are not packed, even if proto3
%% (the spec says it is equivalent to repeated MapFieldItem x = 17;
%% and in proto3, repeated primitive fields are packed by default.
%% Now this isn't a packed primitive field, but check just in case.
<<10, 5, 10, 1, 97, 16, 1, % "a" => 1
10, 5, 10, 1, 98, 16, 2>> = % "b" => 2
encode_msg(#m1{a = [{"a", 1}, {"b", 2}]}, P3Defs).
encode_proto3_unicode_strings_test() ->
P3Defs = [{syntax, "proto3"},
{proto3_msgs, [m1, m2]},
{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, string, string},
occurrence = repeated, opts = []}]},
{{msg, m2}, [#?gpb_field{name = a, fnum = 1, rnum = #m2.b,
type = string,
occurrence = defaulty, opts = []}]}],
Smiley1 = [16#1f631],
Smiley1B = <<240, 159, 152, 177>> = unicode:characters_to_binary(Smiley1),
Smiley2 = [16#1f628],
Smiley2B = <<240, 159, 152, 168>> = unicode:characters_to_binary(Smiley2),
<<10, 12,
10, 4, Smiley1B:4/binary,
18, 4, Smiley2B:4/binary>> =
encode_msg(#m1{a = [{Smiley1, Smiley2}]}, P3Defs, [{string_as_list, false}]),
<<10, 4, Smiley1B:4/binary>> = encode_msg(#m2{b = [Smiley1]}, P3Defs, [{string_as_list, false}]).
encode_proto3_various_empty_string_test() ->
%% With iolists for encoding, there are many ways to specify an empty
%% string. In proto3, they should encode to nothing.
P3Defs = [{syntax, "proto3"},
{proto3_msgs, [m1, m2]},
{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, uint32, string},
occurrence = repeated, opts = []}]},
{{msg, m2}, [#?gpb_field{name = a, fnum = 1, rnum = #m2.b,
type = string,
occurrence = defaulty, opts = []}]}],
SuperEmpty = [<<>>, [[[<<>>, <<>>]]]],
<<10, 4, 8, 1, 18, 0,
10, 4, 8, 2, 18, 0,
10, 6, 8, 3, 18, 2, 97, 98,
10, 5, 8, 4, 18, 1, 65>> = encode_msg(#m1{a = [{1, <<>>}, {2, SuperEmpty},
{3, [97, [<<98>>]]}, {4, <<65>>}]},
P3Defs, [{string_as_list, false}]),
<<>> = encode_msg(#m2{b = ""}, P3Defs, [{string_as_list, false}]),
<<>> = encode_msg(#m2{b = <<>>}, P3Defs, [{string_as_list, false}]),
<<>> = encode_msg(#m2{b = SuperEmpty}, P3Defs, [{string_as_list, false}]),
<<10, 2, 97, 98>> = encode_msg(#m2{b = [97, [<<98>>]]}, P3Defs, [{string_as_list, false}]),
<<10, 1, 65>> = encode_msg(#m2{b = <<65>>}, P3Defs, [{string_as_list, false}]).
encode_proto3_various_empty_sequence_of_bytes_test() ->
%% With iolists for encoding, there are many ways to specify an empty
%% binary. In proto3, they should encode to nothing.
P3Defs = [{syntax, "proto3"},
{proto3_msgs, [m1, m2]},
{{msg, m1}, [#?gpb_field{name = a, fnum = 1, rnum = #m1.a,
type = {map, uint32, bytes},
occurrence = repeated, opts = []}]},
{{msg, m2}, [#?gpb_field{name = a, fnum = 1, rnum = #m2.b,
type = bytes,
occurrence = defaulty, opts = []}]}],
SuperEmpty = [<<>>, [[[<<>>, <<>>]]]],
<<10, 4, 8, 1, 18, 0,
10, 4, 8, 2, 18, 0,
10, 6, 8, 3, 18, 2, 97, 98,
10, 5, 8, 4, 18, 1, 65>> = encode_msg(#m1{a = [{1, <<>>}, {2, SuperEmpty},
{3, [97, [<<98>>]]}, {4, <<65>>}]},
P3Defs),
<<>> = encode_msg(#m2{b = <<>>}, P3Defs),
<<>> = encode_msg(#m2{b = ""}, P3Defs),
<<>> = encode_msg(#m2{b = SuperEmpty}, P3Defs),
<<10, 2, 97, 98>> = encode_msg(#m2{b = [97, [<<98>>]]}, P3Defs),
<<10, 1, 65>> = encode_msg(#m2{b = <<65>>}, P3Defs).
proto3_type_default_values_never_serialized_test() ->
%% "... if a scalar message field is set to its default, the value
%% will not be serialized on the wire."
%% -- https://developers.google.com/protocol-buffers/docs/proto3#default
%%
%% Assuming for oneof it means default = undefined (but I couldn't
%% find any mention of this in the official protobuf docs)
Defs = [{syntax, "proto3"},
{proto3_msgs, [m, s]},
{{enum, e}, [{e0, 0}, {e1, 1}]},
{{msg, s}, % submsg
[#?gpb_field{name = a, fnum = 1, rnum = 2, type = string,
occurrence = defaulty, opts = []}]},
{{msg, m},
[#?gpb_field{name = a, fnum = 1, rnum = 2, type = string,
occurrence = defaulty, opts = []},
#?gpb_field{name = b, fnum = 2, rnum = 3, type = bytes,
occurrence = defaulty, opts = []},
#?gpb_field{name = c, fnum = 3, rnum = 4, type = bool,
occurrence = defaulty, opts = []},
#?gpb_field{name = d, fnum = 4, rnum = 5, type = uint32,
occurrence = defaulty, opts = []},
#?gpb_field{name = e, fnum = 5, rnum = 6, type = float,
occurrence = defaulty, opts = []},
#?gpb_field{name = f, fnum = 6, rnum = 7, type = double,
occurrence = defaulty, opts = []},
#?gpb_field{name = g, fnum = 7, rnum = 8, type = {msg, s},
occurrence = defaulty, opts = []},
#gpb_oneof{
name = h, rnum = 9,
fields = [#?gpb_field{name = ha, fnum = 8, rnum = 9, type = uint32,