forked from SWI-Prolog/contrib-protobufs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobufs.pl
1585 lines (1472 loc) · 70.5 KB
/
protobufs.pl
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
/* Part of SWI-Prolog
Author: Jeffrey Rosenwald, extended by Peter Ludemann
E-mail: [email protected], [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2010-2013, Jeffrey Rosenwald; 2021-2024, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(protobufs,
[ protobuf_message/2, % ?Template ?Codes
protobuf_message/3, % ?Template ?Codes ?Rest
protobuf_parse_from_codes/3, % +WireCodes, +MessageType, -Term
protobuf_serialize_to_codes/3, % +Term, +MessageType, -WireCodes
protobuf_field_is_map/2, % +MessageType, +FieldName
protobuf_map_pairs/3 % ?ProtobufTermList, ?DictTag, ?Pairs
% TODO: Restore the following to the public interface, if
% someone needs them. For now, the tests directly specify
% them using, e.g. protobufs:uint32_codes(..., ...).
%
% protobuf_segment_message/2, % ?Segments ?Codes
% protobuf_segment_convert/2, % +Form1 ?Form2
% uint32_codes/2,
% int32_codes/2,
% float32_codes/2,
% uint64_codes/2,
% int64_codes/2,
% float64_codes/2,
% int64_zigzag/2,
% uint32_int32/2,
% uint64_int64/2,
% uint32_codes_when/2,
% int32_codes_when/2, % TODO: unused
% float32_codes_when/2,
% uint64_codes_when/2,
% int64_codes_when/2, % TODO: unused
% float64_codes_when/2,
% int64_zigzag_when/2,
% uint32_int32_when/2,
% uint64_int64_when/2,
% int64_float64_when/2,
% int32_float32_when/2,
% protobuf_var_int//1,
% protobuf_tag_type//2
]).
:- use_module(library(apply_macros)). % autoload(library(apply), [maplist/3, foldl/4]).
:- autoload(library(error), [must_be/2, domain_error/2, existence_error/2]).
:- autoload(library(lists), [append/3]).
:- autoload(library(utf8), [utf8_codes//1]).
:- autoload(library(dif), [dif/2]).
:- autoload(library(dcg/high_order), [sequence//2]).
:- autoload(library(when), [when/2]).
:- use_module(library(debug), [assertion/1]). % TODO: remove
:- set_prolog_flag(optimise, true). % For arithmetic using is/2.
/** <module> Google's Protocol Buffers ("protobufs")
Protocol buffers are Google's language-neutral, platform-neutral,
extensible mechanism for serializing structured data -- think XML, but
smaller, faster, and simpler. You define how you want your data to be
structured once. This takes the form of a template that describes the
data structure. You use this template to encode and decode your data
structure into wire-streams that may be sent-to or read-from your peers.
The underlying wire stream is platform independent, lossless, and may be
used to interwork with a variety of languages and systems regardless of
word size or endianness. Techniques exist to safely extend your data
structure without breaking deployed programs that are compiled against
the "old" format.
The idea behind Google's Protocol Buffers is that you define your
structured messages using a domain-specific language and tool
set. Further documentation on this is at
[https://developers.google.com/protocol-buffers](https://developers.google.com/protocol-buffers).
There are two ways you can use protobufs in Prolog:
* with a compiled =|.proto|= file: protobuf_parse_from_codes/3 and
protobuf_serialize_to_codes/3.
* with a lower-level interface protobuf_message/2, which allows you
to define your own domain-specific language for parsing and
serializing protobufs.
The protobuf_parse_from_codes/3 and protobuf_serialize_to_codes/3
interface translates between a "wire stream" and a Prolog term. This
interface takes advantage of SWI-Prolog's
[dict](</pldoc/man?section=bidicts>).
The =protoc= plugin (=protoc-gen-swipl=) generates a
Prolog file of meta-information that captures the =|.proto|= file's
definition in the =protobufs= module, with the following facts:
* =|proto_meta_normalize(Unnormalized, Normalized)|=
* =|proto_meta_package(Package, FileName, Options)|=
* =|proto_meta_message_type(Fqn, Package, Name)|=
* =|proto_meta_message_type_map_entry(Fqn)|=
* =|proto_meta_field_name(Fqn, FieldNumber, FieldName, FqnName)|=
* =|proto_meta_field_json_name(FqnName, JsonName)|=
* =|proto_meta_field_label(FqnName, LabelRepeatOptional) % 'LABEL_OPTIONAL', 'LABEL_REQUIRED', 'LABEL_REPEATED'|=
* =|proto_meta_field_type(FqnName, Type) % 'TYPE_INT32', 'TYPE_MESSAGE', etc|=
* =|proto_meta_field_type_name(FqnName, TypeName)|=
* =|proto_meta_field_default_value(FqnName, DefaultValue)|=
* =|proto_meta_field_option_packed(FqnName)|=
* =|proto_meta_enum_type(FqnName, Fqn, Name)|=
* =|proto_meta_enum_value(FqnName, Name, Number)|=
* =|proto_meta_field_oneof_index(FqnName, Index)|=
* =|proto_meta_oneof(FqnName, Index, Name)|=
The protobuf_message/2 interface allows you to define your message
template as a list of predefined
Prolog terms that correspond to production rules in the Definite Clause
Grammar (DCG) that realizes the interpreter. Each production rule has an
equivalent rule in the protobuf grammar. The process is not unlike
specifiying the format of a regular expression. To encode a template to
a wire-stream, you pass a grounded template, =X=, and variable, =Y=, to
protobuf_message/2. To decode a wire-stream, =Y=, you pass an ungrounded
template, =X=, along with a grounded wire-stream, =Y=, to
protobuf_message/2. The interpreter will unify the unbound variables in
the template with values decoded from the wire-stream.
For an overview and tutorial with examples, see
[library(protobufs): Google's Protocol Buffers](#protobufs-main)
Examples of usage may also be found by inspecting
[[test_protobufs.pl][https://github.com/SWI-Prolog/contrib-protobufs/blob/master/test_protobufs.pl]]
and the
[[demo][https://github.com/SWI-Prolog/contrib-protobufs/tree/master/demo]]
directory, or by looking at the "addressbook" example that is typically
installed at
/usr/lib/swi-prolog/doc/packages/examples/protobufs/interop/addressbook.pl
@see https://developers.google.com/protocol-buffers
@see https://developers.google.com/protocol-buffers/docs/encoding
@author Jeffrey Rosenwald ([email protected])
@author Peter Ludemann ([email protected])
@compat SWI-Prolog
*/
:- use_foreign_library(foreign(protobufs)).
%! protobuf_parse_from_codes(+WireCodes:list(int), +MessageType:atom, -Term) is semidet.
% Process bytes (list of int) that is the serialized form of a message (designated
% by =MessageType=), creating a Prolog term.
%
% =Protoc= must have been run (with the =|--swipl_out=|= option and the resulting
% top-level _pb.pl file loaded. For more details, see the "protoc" section of the
% overview documentation.
%
% Fails if the message can't be parsed or if the appropriate meta-data from =protoc=
% hasn't been loaded.
%
% All fields that are omitted from the =WireCodes= are set to their
% default values (typically the empty string or 0, depending on the
% type; or =|[]|= for repeated groups). There is no way of testing
% whether a value was specified in =WireCodes= or given its default
% value (that is, there is no equivalent of the Python
% implementation's =HasField`). Optional embedded messages and groups
% do not have any default value -- you must check their existence by
% using get_dict/3 or similar. If a field is part of a "oneof" set,
% then none of the other fields is set. You can determine which field
% had a value by using get_dict/3.
%
% @tbd document the generated terms (see library(http/json) and json_read_dict/3)
% @tbd add options such as =true= and =value_string_as= (similar to json_read_dict/3)
% @tbd add option for form of the [dict](</pldoc/man?section=bidicts>) tags (fully qualified or not)
% @tbd add option for outputting fields in the C++/Python/Java order
% (by field number rather than by field name).
%
% @bug Ignores =|.proto|= [extensions](https://developers.google.com/protocol-buffers/docs/proto#extensions).
% @bug =map= fields don't get special treatment (but see protobuf_map_pairs/3).
% @bug Generates fields in a different order from the C++, Python,
% Java implementations, which use the field number to determine
% field order whereas currently this implementation uses field
% name. (This isn't stricly speaking a bug, because it's allowed
% by the specification; but it might cause some surprise.)
%
% @param WireCodes Wire format of the message from e.g., read_stream_to_codes/2.
% (The stream should have options `encoding(octet)` and `type(binary)`,
% either as options to read_file_to_codes/3 or by calling set_stream/2
% on the stream to read_stream_to_codes/2.)
% @param MessageType Fully qualified message name (from the =|.proto|= file's =package= and =message=).
% For example, if the =package= is =google.protobuf= and the
% message is =FileDescriptorSet=, then you would use
% =|'.google.protobuf.FileDescriptorSet'|= or =|'google.protobuf.FileDescriptorSet'|=.
% If there's no package name, use e.g.: =|'MyMessage|= or =|'.MyMessage'|=.
% You can see the packages by looking at
% =|protobufs:proto_meta_package(Pkg,File,_)|=
% and the message names and fields by
% =|protobufs:proto_meta_field_name('.google.protobuf.FileDescriptorSet',
% FieldNumber, FieldName, FqnName)|= (the initial '.' is not optional for these facts,
% only for the top-level name given to protobuf_serialize_to_codes/3).
% @param Term The generated term, as nested [dict](</pldoc/man?section=bidicts>)s.
% @see [library(protobufs): Google's Protocol Buffers](#protobufs-serialize-to-codes)
% @error version_error(Module-Version) you need to recompile the =Module=
% with a newer version of =|protoc|=.
protobuf_parse_from_codes(WireCodes, MessageType0, Term) :-
verify_version,
must_be(ground, MessageType0),
( proto_meta_normalize(MessageType0, MessageType)
-> true
; existence_error(protobuf_package, MessageType0)
),
protobuf_segment_message(Segments, WireCodes),
% protobuf_segment_message/2 can leave choicepoints, backtracking
% through all the possibilities would have combinatoric explosion;
% instead use segment_to_term/3 call protobuf_segment_convert/2 to
% change segments that were guessed incorrectly.
!,
maplist(segment_to_term(MessageType), Segments, MsgFields),
!, % TODO: remove
combine_fields(MsgFields, MessageType{}, Term),
!. % TODO: remove? - but proto_meta might have left choicepoints if loaded twice
verify_version :-
( protoc_gen_swipl_version(Module, Version),
Version @< [0,9,1] % This must be sync-ed with changes to protoc-gen-swipl
-> throw(error(version_error(Module-Version), _))
; true
).
%! protobuf_serialize_to_codes(+Term:dict, -MessageType:atom, -WireCodes:list(int)) is det.
% Process a Prolog term into bytes (list of int) that is the serialized form of a
% message (designated by =MessageType=).
%
% =Protoc= must have been run (with the =|--swipl_out=|= option and the resulting
% top-level _pb.pl file loaded. For more details, see the "protoc" section of the
% overview documentation.
%
% Fails if the term isn't of an appropriate form or if the appropriate
% meta-data from =protoc= hasn't been loaded, or if a field name is incorrect
% (and therefore nothing in the meta-data matches it).
%
% @bug =map= fields don't get special treatment (but see protobuf_map_pairs/3).
% @bug =oneof= is not checked for validity.
%
% @param Term The Prolog form of the data, as nested [dict](</pldoc/man?section=bidicts>)s.
% @param MessageType Fully qualified message name (from the =|.proto|= file's =package= and =message=).
% For example, if the =package= is =google.protobuf= and the
% message is =FileDescriptorSet=, then you would use
% =|'.google.protobuf.FileDescriptorSet'|= or =|'google.protobuf.FileDescriptorSet'|=.
% If there's no package name, use e.g.: =|'MyMessage|= or =|'.MyMessage'|=.
% You can see the packages by looking at
% =|protobufs:proto_meta_package(Pkg,File,_)|=
% and the message names and fields by
% =|protobufs:proto_meta_field_name('.google.protobuf.FileDescriptorSet',
% FieldNumber, FieldName, FqnName)|= (the initial '.' is not optional for these facts,
% only for the top-level name given to protobuf_serialize_to_codes/3).
% @param WireCodes Wire format of the message, which can be output using
% =|format('~s', [WireCodes])|=.
% @see [library(protobufs): Google's Protocol Buffers](#protobufs-serialize-to-codes)
% @error version_error(Module-Version) you need to recompile the =Module=
% with a newer version of =|protoc|=.
% @error existence_error if a field can't be found in the meta-data
protobuf_serialize_to_codes(Term, MessageType0, WireCodes) :-
verify_version,
must_be(ground, MessageType0),
( proto_meta_normalize(MessageType0, MessageType)
-> true
; existence_error(protobuf_package, MessageType0)
),
term_to_segments(Term, MessageType, Segments),
!, % TODO: remove
protobuf_segment_message(Segments, WireCodes),
!. % TODO: remove? - but proto_meta might have left choicepoints if loaded twice
%
% Map wire type (atom) to its encoding (an int)
%
wire_type(varint, 0). % for int32, int64, uint32, uint64, sint32, sint64, bool, enum
wire_type(fixed64, 1). % for fixed64, sfixed64, double
wire_type(length_delimited, 2). % for string, bytes, embedded messages, packed repeated fields
wire_type(start_group, 3). % for groups (deprecated)
wire_type(end_group, 4). % for groups (deprecated)
wire_type(fixed32, 5). % for fixed32, sfixed32, float
%
% basic wire-type processing handled by C-support code in DCG-form
%
fixed_uint32(X, [A0, A1, A2, A3 | Rest], Rest) :-
uint32_codes_when(X, [A0, A1, A2, A3]).
/* equivalent to:
fixed_uint32_(X) -->
[ A0,A1,A2,A3 ],
{ uint32_codes_when(X, [A0,A1,A2,A3]) }.
*/
fixed_uint64(X, [A0, A1, A2, A3, A4, A5, A6, A7 | Rest], Rest) :-
uint64_codes_when(X, [A0, A1, A2, A3, A4, A5, A6, A7]).
fixed_float64(X, [A0, A1, A2, A3, A4, A5, A6, A7 | Rest], Rest) :-
float64_codes_when(X, [A0, A1, A2, A3, A4, A5, A6, A7]).
fixed_float32(X, [A0, A1, A2, A3 | Rest], Rest) :-
float32_codes_when(X, [A0, A1, A2, A3]).
%
% Start of the DCG
%
code_string(N, Codes, Rest, Rest1) :-
length(Codes, N),
append(Codes, Rest1, Rest),
!.
/*
code_string(N, Codes) -->
{ length(Codes, N) },
Codes, !.
*/
%
% deal with Google's method of packing unsigned integers in variable
% length, modulo 128 strings.
%
% protobuf_var_int//1 and protobuf_tag_type//2 productions were rewritten in straight
% Prolog for speed's sake.
%
%! protobuf_var_int(?A:int)// is det.
% Conversion between an int A and a list of codes, using the
% "varint" encoding.
% The behvior is undefined if =A= is negative.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
% e.g. phrase(protobuf_var_int(300), S) => S = [172,2]
% phrase(protobuf_var_int(A), [172,2]) -> A = 300
protobuf_var_int(A, [A | Rest], Rest) :-
A < 128,
!.
protobuf_var_int(X, [A | Rest], Rest1) :-
nonvar(X),
X1 is X >> 7,
A is 128 + (X /\ 0x7f),
protobuf_var_int(X1, Rest, Rest1),
!.
protobuf_var_int(X, [A | Rest], Rest1) :-
protobuf_var_int(X1, Rest, Rest1),
X is (X1 << 7) + A - 128,
!.
%! protobuf_tag_type(?Tag:int, ?WireType:atom)// is det.
% Conversion between Tag (number) + WireType and wirestream codes.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
% @arg Tag The item's tag (field number)
% @arg WireType The item's wire type (see prolog_type//2 for how to
% convert this to a Prolog type)
protobuf_tag_type(Tag, WireType, Rest, Rest1) :-
nonvar(Tag), nonvar(WireType),
wire_type(WireType, WireTypeEncoding),
A is Tag << 3 \/ WireTypeEncoding,
protobuf_var_int(A, Rest, Rest1),
!.
protobuf_tag_type(Tag, WireType, Rest, Rest1) :-
protobuf_var_int(A, Rest, Rest1),
WireTypeEncoding is A /\ 0x07,
wire_type(WireType, WireTypeEncoding),
Tag is A >> 3.
%! prolog_type(?Tag:int, ?PrologType:atom)// is semidet.
% Match Tag (field number) + PrologType.
% When Type is a variable, backtracks through all the possibilities
% for a given wire encoding.
% Note that 'repeated' isn't here because it's handled by single_message//3.
% See also segment_type_tag/3.
prolog_type(Tag, double) --> protobuf_tag_type(Tag, fixed64).
prolog_type(Tag, integer64) --> protobuf_tag_type(Tag, fixed64).
prolog_type(Tag, unsigned64) --> protobuf_tag_type(Tag, fixed64).
prolog_type(Tag, float) --> protobuf_tag_type(Tag, fixed32).
prolog_type(Tag, integer32) --> protobuf_tag_type(Tag, fixed32).
prolog_type(Tag, unsigned32) --> protobuf_tag_type(Tag, fixed32).
prolog_type(Tag, integer) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, unsigned) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, signed32) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, signed64) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, boolean) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, enum) --> protobuf_tag_type(Tag, varint).
prolog_type(Tag, atom) --> protobuf_tag_type(Tag, length_delimited).
prolog_type(Tag, codes) --> protobuf_tag_type(Tag, length_delimited).
prolog_type(Tag, utf8_codes) --> protobuf_tag_type(Tag, length_delimited).
prolog_type(Tag, string) --> protobuf_tag_type(Tag, length_delimited).
prolog_type(Tag, embedded) --> protobuf_tag_type(Tag, length_delimited).
prolog_type(Tag, packed) --> protobuf_tag_type(Tag, length_delimited).
%
% The protobuf-2.1.0 grammar allows negative values in enums.
% But they are encoded as unsigned in the golden message.
% As such, they use the maximum length of a varint, so it is
% recommended that they be non-negative. However, that's controlled
% by the =|.proto|= file.
%
:- meta_predicate enumeration(1,?,?).
enumeration(Type) -->
{ call(Type, Value) },
payload(signed64, Value).
%! payload(?PrologType, ?Payload) is det.
% Process the codes into =Payload=, according to =PrologType=
% TODO: payload//2 "mode" is sometimes module-sensitive, sometimes not.
% payload(enum, A)// has A as a callable
% all other uses of payload//2, the 2nd arg is not callable.
% - This confuses check/0; it also makes defining an enumeration
% more difficult because it has to be defined in module protobufs
% (see vector_demo.pl, which defines protobufs:commands/2)
payload(enum, Payload) -->
enumeration(Payload).
payload(double, Payload) -->
fixed_float64(Payload).
payload(integer64, Payload) -->
{ uint64_int64_when(Payload0, Payload) },
fixed_uint64(Payload0).
payload(unsigned64, Payload) -->
fixed_uint64(Payload).
payload(float, Payload) -->
fixed_float32(Payload).
payload(integer32, Payload) -->
{ uint32_int32_when(Payload0, Payload) },
fixed_uint32(Payload0).
payload(unsigned32, Payload) -->
fixed_uint32(Payload).
payload(integer, Payload) -->
{ nonvar(Payload), int64_zigzag(Payload, X) }, % TODO: int64_zigzag_when/2
!,
protobuf_var_int(X).
payload(integer, Payload) -->
protobuf_var_int(X),
{ int64_zigzag(Payload, X) }. % TODO: int64_zigzag_when/2
payload(unsigned, Payload) -->
protobuf_var_int(Payload),
{ Payload >= 0 }.
payload(signed32, Payload) --> % signed32 is not defined by prolog_type//2
% for wire-stream compatibility reasons.
% signed32 ought to write 5 bytes for negative numbers, but both
% the C++ and Python implementations write 10 bytes. For
% wire-stream compatibility, we follow C++ and Python, even though
% protoc decode appears to work just fine with 5 bytes --
% presumably there are some issues with decoding 5 bytes and
% getting the sign extension correct with some 32/64-bit integer
% models. See CodedOutputStream::WriteVarint32SignExtended(int32
% value) in google/protobuf/io/coded_stream.h.
payload(signed64, Payload).
payload(signed64, Payload) -->
% protobuf_var_int//1 cannot handle negative numbers (note that
% zig-zag encoding always results in a positive number), so
% compute the 64-bit 2s complement, which is what is produced
% form C++ and Python.
{ nonvar(Payload) },
!,
{ uint64_int64(X, Payload) }, % TODO: uint64_int64_when
protobuf_var_int(X).
payload(signed64, Payload) -->
% See comment in previous clause about negative numbers.
protobuf_var_int(X),
{ uint64_int64(X, Payload) }. % TODO: uint64_int64_when
payload(codes, Payload) -->
{ nonvar(Payload),
!,
length(Payload, Len)
},
protobuf_var_int(Len),
code_string(Len, Payload).
payload(codes, Payload) -->
protobuf_var_int(Len),
code_string(Len, Payload).
payload(utf8_codes, Payload) -->
{ nonvar(Payload), % TODO: use freeze/2 or when/2
!,
phrase(utf8_codes(Payload), B)
},
payload(codes, B).
payload(utf8_codes, Payload) -->
payload(codes, B),
{ phrase(utf8_codes(Payload), B) }.
payload(atom, Payload) -->
{ nonvar(Payload),
atom_codes(Payload, Codes)
},
payload(utf8_codes, Codes),
!.
payload(atom, Payload) -->
payload(utf8_codes, Codes),
{ atom_codes(Payload, Codes) }.
payload(boolean, true) -->
payload(unsigned, 1).
payload(boolean, false) -->
payload(unsigned, 0).
payload(string, Payload) -->
{ nonvar(Payload)
-> string_codes(Payload, Codes)
; true
},
% string_codes produces a list of unicode, not bytes
payload(utf8_codes, Codes),
{ string_codes(Payload, Codes) }.
payload(embedded, protobuf(PayloadSeq)) -->
{ ground(PayloadSeq),
phrase(protobuf(PayloadSeq), Codes)
},
payload(codes, Codes),
!.
payload(embedded, protobuf(PayloadSeq)) -->
payload(codes, Codes),
{ phrase(protobuf(PayloadSeq), Codes) }.
payload(packed, TypedPayloadSeq) -->
{ TypedPayloadSeq =.. [PrologType, PayloadSeq], % TypedPayloadSeq = PrologType(PayloadSeq)
ground(PayloadSeq),
phrase(packed_payload(PrologType, PayloadSeq), Codes)
},
payload(codes, Codes),
!.
payload(packed, enum(EnumSeq)) -->
!,
% TODO: combine with next clause
% TODO: replace =.. with a predicate that gives all the possibilities - see detag/6.
{ EnumSeq =.. [ Enum, Values ] }, % EnumSeq = Enum(Values)
payload(codes, Codes),
{ phrase(packed_enum(Enum, Values), Codes) }.
payload(packed, TypedPayloadSeq) -->
payload(codes, Codes),
% TODO: replace =.. with a predicate that gives all the possibilities - see detag/6.
{ TypedPayloadSeq =.. [PrologType, PayloadSeq] }, % TypedPayloadSeq = PrologType(PayloadSeq)
{ phrase(packed_payload(PrologType, PayloadSeq), Codes) }.
packed_payload(enum, EnumSeq) -->
{ ground(EnumSeq) }, !,
{ EnumSeq =.. [EnumType, Values] }, % EnumSeq = EnumType(Values)
packed_enum(EnumType, Values).
packed_payload(PrologType, PayloadSeq) -->
sequence_payload(PrologType, PayloadSeq).
% sequence_payload//2 (because sequence//2 isn't compile-time expanded)
sequence_payload(PrologType, PayloadSeq) -->
sequence_payload_(PayloadSeq, PrologType).
sequence_payload_([], _PrologType) --> [ ].
sequence_payload_([Payload|PayloadSeq], PrologType) -->
payload(PrologType, Payload),
sequence_payload_(PayloadSeq, PrologType).
packed_enum(Enum, [ A | As ]) -->
% TODO: replace =.. with a predicate that gives all the possibilities - see detag/6.
{ E =.. [Enum, A] },
payload(enum, E),
packed_enum(Enum, As).
packed_enum(_, []) --> [ ].
start_group(Tag) --> protobuf_tag_type(Tag, start_group).
end_group(Tag) --> protobuf_tag_type(Tag, end_group).
%
%
nothing([]) --> [], !.
protobuf([Field | Fields]) -->
% TODO: don't use =.. -- move logic to single_message
( { Field = repeated_embedded(Tag, protobuf(EmbeddedFields), Items) }
-> repeated_embedded_messages(Tag, EmbeddedFields, Items)
; { Field =.. [ PrologType, Tag, Payload] }, % Field = PrologType(Tag, Payload)
single_message(PrologType, Tag, Payload),
( protobuf(Fields)
; nothing(Fields)
)
),
!.
repeated_message(repeated_enum, Tag, Type, [A | B]) -->
% TODO: replace =.. with a predicate that gives all the possibilities - see detag/6.
{ TypedPayload =.. [Type, A] }, % TypedPayload = Type(A)
single_message(enum, Tag, TypedPayload),
( repeated_message(repeated_enum, Tag, Type, B)
; nothing(B)
).
repeated_message(Type, Tag, [A | B]) -->
{ Type \= repeated_enum },
single_message(Type, Tag, A),
repeated_message(Type, Tag, B).
repeated_message(_Type, _Tag, A) -->
nothing(A).
repeated_embedded_messages(Tag, EmbeddedFields, [protobuf(A) | B]) -->
{ copy_term(EmbeddedFields, A) },
single_message(embedded, Tag, protobuf(A)), !,
repeated_embedded_messages(Tag, EmbeddedFields, B).
repeated_embedded_messages(_Tag, _EmbeddedFields, []) -->
[ ].
%! single_message(+PrologType:atom, ?Tag, ?Payload)// is det.
% Processes a single messages (e.g., one item in the list in protobuf([...]).
% The PrologType, Tag, Payload are from Field =.. [PrologType, Tag, Payload]
% in the caller
single_message(repeated, Tag, enum(EnumSeq)) -->
!,
{ EnumSeq =.. [EnumType, Values] }, % EnumSeq = EnumType(Values)
repeated_message(repeated_enum, Tag, EnumType, Values).
single_message(repeated, Tag, Payload) -->
!,
% TODO: replace =.. with a predicate that gives all the possibilities - see detag/6.
{ Payload =.. [PrologType, A] }, % Payload = PrologType(A)
{ PrologType \= enum },
repeated_message(PrologType, Tag, A).
single_message(group, Tag, A) -->
!,
start_group(Tag),
protobuf(A),
end_group(Tag).
single_message(PrologType, Tag, Payload) -->
{ PrologType \= repeated, PrologType \= group },
prolog_type(Tag, PrologType),
payload(PrologType, Payload).
%! protobuf_message(?Template, ?WireStream) is semidet.
%! protobuf_message(?Template, ?WireStream, ?Rest) is nondet.
%
% Marshals and unmarshals byte streams encoded using Google's
% Protobuf grammars. protobuf_message/2 provides a bi-directional
% parser that marshals a Prolog structure to WireStream, according
% to rules specified by Template. It can also unmarshal WireStream
% into a Prolog structure according to the same grammar.
% protobuf_message/3 provides a difference list version.
%
% @bug The protobuf specification states that the wire-stream can have
% the fields in any order and that unknown fields are to be ignored.
% This implementation assumes that the fields are in the exact order
% of the definition and match exactly. If you use
% protobuf_parse_from_codes/3, you can avoid this problem.o
%
% @param Template is a protobuf grammar specification. On decode,
% unbound variables in the Template are unified with their respective
% values in the WireStream. On encode, Template must be ground.
%
% @param WireStream is a code list that was generated by a protobuf
% encoder using an equivalent template.
protobuf_message(protobuf(TemplateList), WireStream) :-
must_be(list, TemplateList),
phrase(protobuf(TemplateList), WireStream),
!.
protobuf_message(protobuf(TemplateList), WireStream, Residue) :-
must_be(list, TemplateList),
phrase(protobuf(TemplateList), WireStream, Residue).
%! protobuf_segment_message(+Segments:list, -WireStream:list(int)) is det.
%! protobuf_segment_message(-Segments:list, +WireStream:list(int)) is det.
%
% Low level marshalling and unmarshalling of byte streams. The
% processing is independent of the =|.proto|= description, similar to
% the processing done by =|protoc --decode_raw|=. This means that
% field names aren't shown, only field numbers.
%
% For unmarshalling, a simple heuristic is used on length-delimited
% segments: first interpret it as a message; if that fails, try to
% interpret as a UTF8 string; otherwise, leave it as a "blob" (if the
% heuristic was wrong, you can convert to a string or a blob by using
% protobuf_segment_convert/2). 32-bit and 64-bit numbers are left as
% codes because they could be either integers or floating point (use
% int32_codes_when/2, float32_codes_when/2, int64_codes_when/2,
% uint32_codes_when/2, uint64_codes_when/2, float64_codes_when/2 as
% appropriate); variable-length numbers ("varint" in the [[Protocol
% Buffers encoding
% documentation][https://developers.google.com/protocol-buffers/docs/encoding#varints]]),
% might require "zigzag" conversion, int64_zigzag_when/2.
%
% For marshalling, use the predicates int32_codes_when/2,
% float32_codes_when/2, int64_codes_when/2, uint32_codes_when/2,
% uint64_codes_when/2, float64_codes_when/2, int64_zigzag_when/2 to
% put integer and floating point values into the appropriate form.
%
% @bug This predicate is preliminary and may change as additional
% functionality is added.
%
% @param Segments a list containing terms of the following form (=Tag= is
% the field number; =Codes= is a list of integers):
% * varint(Tag,Varint) - =Varint= may need int64_zigzag_when/2
% * fixed64(Tag,Int) - =Int= signed, derived from the 8 codes
% * fixed32(Tag,Codes) - =Int= is signed, derived from the 4 codes
% * message(Tag,Segments)
% * group(Tag,Segments)
% * string(Tag,String) - =String= is a SWI-Prolog string
% * packed(Tag,Type(Scalars)) - =Type= is one of
% =varint=, =fixed64=, =fixed32=; =Scalars=
% is a list of =Varint= or =Codes=, which should
% be interpreted as described under those items.
% Note that the protobuf specification does not
% allow packed repeated string.
% * length_delimited(Tag,Codes)
% * repeated(List) - =List= of segments
% Of these, =group= is deprecated in the protobuf documentation and
% shouldn't appear in modern code, having been superseded by nested
% message types.
%
% For deciding how to interpret a length-delimited item (when
% =Segments= is a variable), an attempt is made to parse the item in
% the following order (although code should not rely on this order):
% * message
% * string (it must be in the form of a UTF string)
% * packed (which can backtrack through the various =Type=s)
% * length_delimited - which always is possible.
%
% The interpretation of length-delimited items can sometimes guess
% wrong; the interpretation can be undone by either backtracking or
% by using protobuf_segment_convert/2 to convert the incorrect
% segment to a string or a list of codes. Backtracking through all
% the possibilities is not recommended, because of combinatoric
% explosion (there is an example in the unit tests); instead, it is
% suggested that you take the first result and iterate through its
% items, calling protobuf_segment_convert/2 as needed to reinterpret
% incorrectly guessed segments.
%
% @param WireStream a code list that was generated by a protobuf
% endoder.
%
% @see https://developers.google.com/protocol-buffers/docs/encoding
protobuf_segment_message(Segments, WireStream) :-
phrase(segment_message(Segments), WireStream).
segment_message(Segments) -->
sequence_segment(Segments).
% sequence_segment//1 (because sequence//2 isn't compile-time expanded)
sequence_segment([]) --> [ ].
sequence_segment([Segment|Segments]) -->
segment(Segment),
sequence_segment(Segments).
segment(Segment) -->
{ nonvar(Segment) },
!,
% repeated(List) can be created by field_segment_scalar_or_repeated/7
( { Segment = repeated(Segments) }
-> sequence_segment(Segments)
; { segment_type_tag(Segment, Type, Tag) },
protobuf_tag_type(Tag, Type),
segment(Type, Tag, Segment)
).
segment(Segment) -->
% { var(Segment) },
protobuf_tag_type(Tag, Type),
segment(Type, Tag, Segment).
segment(varint, Tag, varint(Tag,Value)) -->
protobuf_var_int(Value).
segment(fixed64, Tag, fixed64(Tag, Int64)) -->
payload(integer64, Int64).
segment(fixed32, Tag, fixed32(Tag, Int32)) -->
payload(integer32, Int32).
segment(start_group, Tag, group(Tag, Segments)) -->
segment_message(Segments),
protobuf_tag_type(Tag, end_group).
segment(length_delimited, Tag, Result) -->
segment_length_delimited(Tag, Result).
segment_length_delimited(Tag, Result) -->
{ nonvar(Result) },
!,
{ length_delimited_segment(Result, Tag, Codes) },
{ length(Codes, CodesLen) },
protobuf_var_int(CodesLen),
code_string(CodesLen, Codes).
segment_length_delimited(Tag, Result) -->
% { var(Result) },
protobuf_var_int(CodesLen),
code_string(CodesLen, Codes),
{ length_delimited_segment(Result, Tag, Codes) }.
length_delimited_segment(message(Tag,Segments), Tag, Codes) :-
protobuf_segment_message(Segments, Codes).
length_delimited_segment(group(Tag,Segments), Tag, Codes) :-
phrase(segment_group(Tag, Segments), Codes).
length_delimited_segment(string(Tag,String), Tag, Codes) :-
( nonvar(String)
-> string_codes(String, StringCodes),
phrase(utf8_codes(StringCodes), Codes)
; phrase(utf8_codes(StringCodes), Codes),
string_codes(String, StringCodes)
).
length_delimited_segment(packed(Tag,Payload), Tag, Codes) :-
% We don't know the type of the fields, so we try the 3
% possibilities. This has a problem: an even number of fixed32
% items can't be distinguished from half the number of fixed64
% items; but it's all we can do. The good news is that usually
% varint (possibly with zig-zag encoding) is more common because
% it's more compact (I don't know whether 32-bit or 64-bit is more
% common for floating point).
packed_option(Type, Items, Payload),
phrase(sequence_payload(Type, Items), Codes).
length_delimited_segment(length_delimited(Tag,Codes), Tag, Codes).
segment_group(Tag, Segments) -->
start_group(Tag),
segment_message(Segments),
end_group(Tag).
% See also prolog_type//2. Note that this doesn't handle repeated(List),
% which is used internally (see field_segment_scalar_or_repeated/7).
segment_type_tag(varint(Tag,_Value), varint, Tag).
segment_type_tag(fixed64(Tag,_Value), fixed64, Tag).
segment_type_tag(group(Tag,_Segments), start_group, Tag).
segment_type_tag(fixed32(Tag,_Value), fixed32, Tag).
segment_type_tag(length_delimited(Tag,_Codes), length_delimited, Tag).
segment_type_tag(message(Tag,_Segments), length_delimited, Tag).
segment_type_tag(packed(Tag,_Payload), length_delimited, Tag).
segment_type_tag(string(Tag,_String), length_delimited, Tag).
%! detag(+Compound, -Name, -Tag, -Value, List, -CompoundWithList) is semidet.
% Deconstruct =Compound= or the form =|Name(Tag,Value)|= and create a
% new =CompoundWithList= that replaces =Value= with =List=. This is
% used by packed_list/2 to transform =|[varint(1,0),varint(1,1)]|= to
% =|varint(1,[0,1])|=.
%
% Some of =Compound= items are impossible for =packed= with the
% current protobuf spec, but they don't do any harm.
detag(varint(Tag,Value), varint, Tag, Value, List, varint(List)).
detag(fixed64(Tag,Value), fixed64, Tag, Value, List, fixed64(List)).
detag(fixed32(Tag,Value), fixed32, Tag, Value, List, fixed32(List)).
detag(length_delimited(Tag,Codes), length_delimited, Tag, Codes, List, length_delimited(List)).
detag(message(Tag,Segments), message, Tag, Segments, List, message(List)).
detag(packed(Tag,Payload), packed, Tag, Payload, List, packed(List)). % TODO: delete?
detag(string(Tag,String), string, Tag, String, List, string(List)).
% See also prolog_type//2, but pick only one for each wirestream type
% For varint(Items), use one that doesn't do zigzag
packed_option(integer64, Items, fixed64(Items)).
packed_option(integer32, Items, fixed32(Items)).
packed_option(unsigned, Items, varint(Items)).
% packed_option(integer, Items, varint(Items)).
% packed_option(double, Items, fixed64(Items)).
% packed_option(float, Items, fixed32(Items)).
% packed_option(signed64, Items, varint(Items)).
% packed_option(boolean, Items, varint(Items)).
% packed_option(enum, Items, varint(Items)).
%! protobuf_segment_convert(+Form1, ?Form2) is multi.
% A convenience predicate for dealing with the situation where
% protobuf_segment_message/2 interprets a segment of the wire stream
% as a form that you don't want (e.g., as a message but it should have
% been a UTF8 string).
%
% =Form1= is converted back to the original wire stream, then the
% predicate non-deterimisticly attempts to convert the wire stream to
% a =|string|= or =|length_delimited|= term (or both: the lattter
% always succeeds).
%
% The possible conversions are:
% message(Tag,Segments) => string(Tag,String)
% message(Tag,Segments) => length_delimited(Tag,Codes)
% string(Tag,String) => length_delimited(Tag,Codes)
% length_delimited(Tag,Codes) => length_delimited(Tag,Codes)
%
% Note that for fixed32, fixed64, only the signed integer forms are
% given; if you want the floating point forms, then you need to do use
% int64_float64_when/2 and int32_float32_when/2.
%
% For example:
% ~~~{.pl}
% ?- protobuf_segment_convert(
% message(10,[fixed64(13,7309475598860382318)]),
% string(10,"inputType")).
% ?- protobuf_segment_convert(
% message(10,[fixed64(13,7309475598860382318)]),
% length_delimited(10,[105,110,112,117,116,84,121,112,101])).
% ?- protobuf_segment_convert(
% string(10, "inputType"),
% length_delimited(10,[105,110,112,117,116,84,121,112,101])).
% ?- forall(protobuf_segment_convert(string(1999,"\x1\\x0\\x0\\x0\\x2\\x0\\x0\\x0\"),Z), writeln(Z)).
% string(1999, )
% packed(1999,fixed64([8589934593]))
% packed(1999,fixed32([1,2]))
% packed(1999,varint([1,0,0,0,2,0,0,0]))
% length_delimited(1999,[1,0,0,0,2,0,0,0])
% ~~~
% These come from:
% ~~~{.pl}
% Codes = [82,9,105,110,112,117,116,84,121,112,101],
% protobuf_message(protobuf([embedded(T1, protobuf([integer64(T2, I)]))]), Codes),
% protobuf_message(protobuf([string(T,S)]), Codes).
% T = 10, T1 = 10, T2 = 13,
% I = 7309475598860382318,
% S = "inputType".
% ~~~
%
% @bug This predicate is preliminary and may change as additional
% functionality is added.
% @bug This predicate will sometimes generate unexpected choice points,
% Such as from =|protobuf_segment_convert(message(10,...), string(10,...))|=
%
% @param Form1 =|message(Tag,Pieces)|=, =|string(Tag,String)|=, =|length_delimited(Tag,Codes)|=,
% =|varint(Tag,Value)|=, =|fixed64(Tag,Value)|=, =|fixed32(Tag,Value)|=.
% @param Form2 similar to =Form1=.
protobuf_segment_convert(Form, Form). % for efficiency, don't generate codes
protobuf_segment_convert(Form1, Form2) :-
dif(Form1, Form2), % Form1=Form2 handled by first clause
protobuf_segment_message([Form1], WireCodes),
phrase(tag_and_codes(Tag, Codes), WireCodes),
length_delimited_segment(Form2, Tag, Codes).
tag_and_codes(Tag, Codes) -->
protobuf_tag_type(Tag, length_delimited),
payload(codes, Codes).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Documention of the foreign predicates, which are wrapped and exported.
%! uint32_codes_when(?Uint32, ?Codes) is det.
% Convert between a 32-bit unsigned integer value and its wirestream codes.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
%
% This predicate delays until either =Uint32= or =Codes= is
% sufficiently instantiated.
%
% There is also a non-delayed protobufs:uint32_codes/2
%
% SWI-Prolog doesn't have a 32-bit integer type, so 32-bit integer
% is simulated by doing a range check.
%
% @param Uint32 an unsigned integer that's in the 32-bit range
% @param Codes a list of 4 integers (codes)
%
% @error Type,Domain if =Value= or =Codes= are of the wrong
% type or out of range.
uint32_codes_when(Uint32, Codes) :-
when((nonvar(Uint32) ; nonvar(Codes)), uint32_codes(Uint32, Codes)).
%! int32_codes_when(?Int32, ?Codes) is det.
% Convert between a 32-bit signed integer value and its wirestream codes.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
%
% This predicate delays until either =Int32= or =Codes= is
% sufficiently instantiated.
%
% There is also a non-delayed protobufs:int32_codes/2
%
% SWI-Prolog doesn't have a 32-bit integer type, so 32-bit integer
% is simulated by doing a range check.
%
% @param Int32 an unsigned integer that's in the 32-bit range
% @param Codes a list of 4 integers (codes)
%
% @error Type,Domain if =Value= or =Codes= are of the wrong
% type or out of range.
int32_codes_when(Int32, Codes) :- % TODO: unused
when((nonvar(Int32) ; nonvar(Codes)), int32_codes(Int32, Codes)).
%! float32_codes_when(?Value, ?Codes) is det.
% Convert between a 32-bit floating point value and its wirestream codes.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
%
% This predicate delays until either =Value= or =Codes= is
% sufficiently instantiated.
%
% There is also a non-delayed protobufs:float32_codes/2
%
% @param Value a floating point number
% @param Codes a list of 4 integers (codes)
float32_codes_when(Value, Codes) :-
when((nonvar(Value) ; nonvar(Codes)), float32_codes(Value, Codes)).
%! uint64_codes_when(?Uint64, ?Codes) is det.
% Convert between a 64-bit unsigned integer value and its wirestream codes.
% This is a low-level predicate; normally, you should use
% template_message/2 and the appropriate template term.
%
% SWI-Prolog allows integer values greater than 64 bits, so
% a range check is done.
%
% This predicate delays until either =Uint64= or =Codes= is
% sufficiently instantiated.
%
% There is also a non-delayed protobufs:uint64_codes/2
%
% @param Uint64 an unsigned integer
% @param Codes a list of 8 integers (codes)