Skip to content

Commit

Permalink
pw_protobuf: Initialize variables in docs example
Browse files Browse the repository at this point in the history
Updates the wire decoder example to initialize variables to their
default values in case they don't appear in the serialized message.

Fixes: 337272675
Change-Id: I2c122c63d7cd646c0648ac4b25befae470fd40df
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/257112
Reviewed-by: Dave Roth <[email protected]>
Lint: Lint 🤖 <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Pigweed-Auto-Submit: Alexei Frolov <[email protected]>
  • Loading branch information
frolv authored and CQ Bot Account committed Jan 2, 2025
1 parent 6582c5e commit babdb2a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pw_protobuf/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ in the examples:
message Customer {
enum Status {
UNKNOWN = 0;
NEW = 1;
ACTIVE = 2;
INACTIVE = 3;
Expand Down Expand Up @@ -79,10 +80,12 @@ This results in the following generated structure:
.. code-block:: c++

enum class Customer::Status : uint32_t {
UNKNOWN = 0,
NEW = 1,
ACTIVE = 2,
INACTIVE = 3,

kUnknown = UNKNOWN,
kNew = NEW,
kActive = ACTIVE,
kInactive = INACTIVE,
Expand Down Expand Up @@ -467,9 +470,11 @@ complex than encoding or using the message structure.
.. code-block:: c++

pw::Status DecodeCustomer(Customer::StreamDecoder& decoder) {
uint32_t age;
char name[32];
Customer::Status status;
// Initialize variables to their default values as they will not be written
// if they don't appear in the serialized message.
uint32_t age = 0;
char name[32] = "";
Customer::Status status = Customer::Status::UNKNOWN;

while ((status = decoder.Next()).ok()) {
switch (decoder.Field().value()) {
Expand Down

0 comments on commit babdb2a

Please sign in to comment.