| Did you know ... | Search Documentation: |
| Pack logtalk -- logtalk-3.98.0/library/protobuf/NOTES.md |
This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2026 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
protobuf
The protobuf library implements predicates for reading (parsing) and writing
(generating) data in the Google Protocol Buffers binary format.
This implementation is based on the Protocol Buffers Language Guide (proto3) and the Protocol Buffers Encoding specification:
Open the [../../apis/library_index.html#avro](../../apis/library_index.html#avro) link in a web browser.
To load all entities in this library, load the loader.lgt file:
| ?- logtalk_load(avro(loader)).
To test this library predicates, load the tester.lgt file:
| ?- logtalk_load(avro(tester)).
Protocol Buffers (protobuf) is a language-neutral, platform-neutral extensible mechanism for serializing structured data. It is widely used for data storage, communication protocols, and more.
In this library, Protocol Buffers schemas (normally defined in `.proto` files) are represented as Logtalk terms. The schema representation uses a curly-bracketed syntax similar to JSON representation.
A message schema is represented as:
{message-MessageName, fields-FieldList}
Where FieldList is a list of field definitions, each with:
{number-FieldNumber, name-FieldName, type-FieldType}
int32 - Uses variable-length encoding. Inefficient for negative numbers (use sint32 instead).int64 - Uses variable-length encoding. Inefficient for negative numbers (use sint64 instead).uint32 - Uses variable-length encoding.uint64 - Uses variable-length encoding.sint32 - Uses variable-length encoding with ZigZag encoding. More efficient for negative numbers.sint64 - Uses variable-length encoding with ZigZag encoding. More efficient for negative numbers.bool - Boolean value: true or false.fixed32 - Always four bytes. More efficient than uint32 if values are often greater than 2^28.fixed64 - Always eight bytes. More efficient than uint64 if values are often greater than 2^56.sfixed32 - Always four bytes. Signed fixed-width integer.sfixed64 - Always eight bytes. Signed fixed-width integer.float - 32-bit IEEE 754 floating point.double - 64-bit IEEE 754 floating point.string - UTF-8 encoded text.bytes - Arbitrary byte sequence.{message-Name, fields-Fields} - Embedded message (nested structure).Simple primitive type schemas:
{message-'Person', fields-[
{number-1, name-name, type-string},
{number-2, name-id, type-int32},
{number-3, name-email, type-string}
]}
Nested message schema:
{message-'AddressBook', fields-[
{number-1, name-people, type-{message-'Person', fields-[
{number-1, name-name, type-string},
{number-2, name-id, type-int32}
]}}
]}
Data to be serialized is represented as a list of field name-value pairs using
the - operator. For example:
[name-'John Doe', id-42, email-'john@example.com']
For primitive types, data is represented directly as Logtalk values:
42, -17true, false3.14, -273.15'Hello World', hello[0x48, 0x65, 0x6c, 0x6c, 0x6f]Encoding is accomplished using the generate/3 or generate/4 predicates.
For example, encoding an integer using the int32 schema:
| ?- protobuf::generate(bytes(Bytes), int32, 150).
Bytes = [150, 1]
yes
Encoding a string:
| ?- protobuf::generate(bytes(Bytes), string, 'testing').
Bytes = [116, 101, 115, 116, 105, 110, 103]
yes
To encode a complete message:
| ?- Schema = {message-'Person', fields-[
| {number-1, name-name, type-string},
| {number-2, name-id, type-int32}
| ]},
| Data = [name-'John', id-42],
| protobuf::generate(bytes(Bytes), Schema, Data).
Schema = {...}, Data = [name-'John', id-42], Bytes = [10, 4, 74, 111, 104, 110, 16, 84] yes
To include the schema in the output (as a custom wrapper message), use the
generate/4 predicate with the second argument set to true:
| ?- protobuf::generate(file('output.pb'), true, Schema, Data).
yes
This generates a wrapper message with:
Decoding is accomplished using the parse/2 or parse/3 predicates.
When parsing a file that includes a schema (wrapper message format), use
parse/2 which returns a Schema-Data pair:
| ?- protobuf::parse(file('input.pb'), Schema-Data).
When the schema is not present in the file, Schema is unified with false.
When the schema is known and not embedded in the file, use parse/3:
| ?- Schema = {message-'Person', fields-[
| {number-1, name-name, type-string},
| {number-2, name-id, type-int32}
| ]},
| protobuf::parse(file('person.pb'), Schema, Data).
Schema = {...}, Data = [name-'Alice', id-123] yes
The library supports three types of sources:
bytes(List) - Parse from a list of bytesstream(Stream) - Parse from an open binary streamfile(Path) - Parse from a fileProtocol Buffers uses an efficient binary wire format with the following wire types:
int32, int64, uint32, uint64, sint32, sint64, boolfixed64, sfixed64, doublestring, bytes, embedded messagesfixed32, sfixed32, float
Each field is encoded with a tag (field number and wire type) followed by the value.The binary format produced by this library is compatible with:
oneof, map, enum, and repeated fields are not yet supported in this
initial version.
The test_files subdirectory contains example schemas and data files that
demonstrate the library's capabilities:
A simple Person message schema. This is a classic Protocol Buffers example demonstrating basic field types:
An AddressBook schema demonstrating nested messages. Shows how to represent complex hierarchical data structures:
Protocol Buffers binary format is designed for:
sint32/sint64 for negative numbersfixed32/fixed64 for large numbers