Skip to content

Commit

Permalink
pw_protobuf: Allow unset oneof callbacks
Browse files Browse the repository at this point in the history
Instead of failing to decode a message when a oneof callback is unset,
just ignore the oneof fields. This prevents users from having to set a
callback for every possible field in a message when they are only
interested in a few.

Change-Id: I62c5158afd5b6b77af8cf69988f63328ec4b9359
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/246692
Commit-Queue: Auto-Submit <[email protected]>
Pigweed-Auto-Submit: Alexei Frolov <[email protected]>
Lint: Lint 🤖 <[email protected]>
Docs-Not-Needed: Alexei Frolov <[email protected]>
Reviewed-by: Wyatt Hepler <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
  • Loading branch information
frolv authored and CQ Bot Account committed Nov 4, 2024
1 parent 1dcac6a commit 6a16fab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions pw_protobuf/codegen_message_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2147,11 +2147,11 @@ TEST(CodegenMessage, OneOf_Encode_MultipleTimes) {
0);
}

TEST(CodegenMessage, OneOf_Encode_UnsetEncoderFails) {
TEST(CodegenMessage, OneOf_Encode_UnsetEncoderIsIgnored) {
OneOfTest::Message message;
std::array<std::byte, 8> buffer;
OneOfTest::MemoryEncoder oneof_test(buffer);
EXPECT_EQ(oneof_test.Write(message), Status::DataLoss());
EXPECT_EQ(oneof_test.Write(message), OkStatus());
}

TEST(CodegenMessage, OneOf_Decode) {
Expand Down Expand Up @@ -2208,7 +2208,7 @@ TEST(CodegenMessage, OneOf_Decode_MultipleOneOfFieldsFails) {
EXPECT_EQ(stream_decoder.Read(message), Status::DataLoss());
}

TEST(CodegenMessage, OneOf_Decode_UnsetDecoderFails) {
TEST(CodegenMessage, OneOf_Decode_UnsetDecoderIsIgnored) {
// clang-format off
constexpr uint8_t proto_data[] = {
// type.an_int
Expand All @@ -2219,7 +2219,7 @@ TEST(CodegenMessage, OneOf_Decode_UnsetDecoderFails) {
stream::MemoryReader reader(as_bytes(span(proto_data)));
OneOfTest::StreamDecoder stream_decoder(reader);
OneOfTest::Message message;
EXPECT_EQ(stream_decoder.Read(message), Status::DataLoss());
EXPECT_EQ(stream_decoder.Read(message), OkStatus());
}

} // namespace
Expand Down
4 changes: 2 additions & 2 deletions pw_protobuf/public/pw_protobuf/internal/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ struct OneOf {
invoked_ = true;
return encode_(encoder);
}
return Status::DataLoss();
return OkStatus();
}

Status Decode(Fields field, StreamDecoder& decoder) const {
Expand All @@ -313,7 +313,7 @@ struct OneOf {
invoked_ = true;
return decode_(field, decoder);
}
return Status::DataLoss();
return OkStatus();
}

mutable bool invoked_;
Expand Down

0 comments on commit 6a16fab

Please sign in to comment.