From 63c55e048888397a3b163762e919256e4fe1f2da Mon Sep 17 00:00:00 2001 From: Richard Peters Date: Tue, 9 Jul 2024 09:47:55 +0200 Subject: [PATCH] fix(echo): support serializing nested messages (#670) * fix(echo): support serializing nested messages * Apply suggestions from code review Co-authored-by: Daan Timmer <8293597+daantimmer@users.noreply.github.com> * Update protobuf/echo/ProtoMessageSender.hpp Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: Daan Timmer <8293597+daantimmer@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- infra/util/BoundedString.hpp | 2 +- protobuf/echo/ProtoMessageSender.hpp | 3 ++- protobuf/echo/test/TestMessages.proto | 5 +++++ protobuf/echo/test/TestProtoMessageSender.cpp | 8 ++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/infra/util/BoundedString.hpp b/infra/util/BoundedString.hpp index 9263d49c9..fd1d6417a 100644 --- a/infra/util/BoundedString.hpp +++ b/infra/util/BoundedString.hpp @@ -1423,7 +1423,7 @@ namespace infra for (; additional != 0; --additional) range[index++] = *first++; - return &range[index]; + return std::next(range.begin(), index); } template diff --git a/protobuf/echo/ProtoMessageSender.hpp b/protobuf/echo/ProtoMessageSender.hpp index 2297fc31c..7d68bc0b0 100644 --- a/protobuf/echo/ProtoMessageSender.hpp +++ b/protobuf/echo/ProtoMessageSender.hpp @@ -117,7 +117,8 @@ namespace services template bool ProtoMessageSenderBase::SerializeField(ProtoMessage, infra::ProtoFormatter& formatter, const Message& value, uint32_t fieldNumber, bool& retry) const { - infra::DataOutputStream::WithWriter countingStream; + std::array saveStateStorage; // For writing length fields + infra::DataOutputStream::WithWriter countingStream{ saveStateStorage }; infra::ProtoFormatter countingFormatter{ countingStream }; value.Serialize(countingFormatter); formatter.PutLengthDelimitedSize(countingStream.Writer().Processed(), fieldNumber); diff --git a/protobuf/echo/test/TestMessages.proto b/protobuf/echo/test/TestMessages.proto index 57f243d02..a9e213914 100644 --- a/protobuf/echo/test/TestMessages.proto +++ b/protobuf/echo/test/TestMessages.proto @@ -163,6 +163,11 @@ message TestMoreNestedMessage NestedMessage2 message2 = 2; } +message TestDeepNestedMessage +{ + TestNestedMessage message = 1; +} + message TestNestedRepeatedMessage { message NestedMessage diff --git a/protobuf/echo/test/TestProtoMessageSender.cpp b/protobuf/echo/test/TestProtoMessageSender.cpp index 46a51a08a..8d0eb8e03 100644 --- a/protobuf/echo/test/TestProtoMessageSender.cpp +++ b/protobuf/echo/test/TestProtoMessageSender.cpp @@ -176,6 +176,14 @@ TEST_F(ProtoMessageSenderTest, format_more_nested_message) ExpectFill({ (1 << 3) | 2, 2, 1 << 3, 5, (2 << 3) | 2, 2, 2 << 3, 10 }, sender); } +TEST_F(ProtoMessageSenderTest, format_deep_nested_message) +{ + test_messages::TestDeepNestedMessage message{ { 5 } }; + services::ProtoMessageSender sender{ message }; + + ExpectFill({ 1 << 3 | 2, 4, 1 << 3 | 2, 2, 1 << 3, 5 }, sender); +} + TEST_F(ProtoMessageSenderTest, dont_format_on_buffer_full) { test_messages::TestBoolWithBytes message;