Book Image

Protocol Buffers Handbook

By : Clément Jean
Book Image

Protocol Buffers Handbook

By: Clément Jean

Overview of this book

Explore how Protocol Buffers (Protobuf) serialize structured data and provides a language-neutral, platform-neutral, and extensible solution. With this guide to mastering Protobuf, you'll build your skills to effectively serialize, transmit, and manage data across diverse platforms and languages. This book will help you enter the world of Protocol Buffers by unraveling the intricate nuances of Protobuf syntax and showing you how to define complex data structures. As you progress, you’ll learn schema evolution, ensuring seamless compatibility as your projects evolve. The book also covers advanced topics such as custom options and plugins, allowing you to tailor validation processes to your specific requirements. You’ll understand how to automate project builds using cutting-edge tools such as Buf and Bazel, streamlining your development workflow. With hands-on projects in Go and Python programming, you’ll learn how to practically apply Protobuf concepts. Later chapters will show you how to integrate data interchange capabilities across different programming languages, enabling efficient collaboration and system interoperability. By the end of this book, you’ll have a solid understanding of Protobuf internals, enabling you to discern when and how to use and redefine your approach to data serialization.
Table of Contents (13 chapters)

Disabling tags – reserved tags

Protobuf has a concept called reserved tags. This means that these tags are made unusable by developers when they are updating a message. This looks like this:

message Id {
  reserved 1;
}

In this case, tag 1 isn’t reusable. This might not be directly clear how, but this helps with the problems that we saw in the previous section. Let’s see how.

If you recall the problem of integer overflow, we had a value of 4,294,967,297 encoded and Protobuf, after decoding, returned a value equal to 1. This problem came from the fact that we changed the type of the value field from uint32 to uint64 and we are now trying to encode an uint64 in an uint32. While this did not crash and the value field was populated with data, in most cases, we won’t want the overflow behavior. This might lead to getting the wrong data for a user, overwriting data that is not from the user, and more.

To prevent this, instead of directly changing...