Skip to content

Commit

Permalink
Add ::protos::Parse template for Ptr<T>.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 500743633
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 9, 2023
1 parent 204b9ee commit b5384af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions protos/protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ bool Parse(T& message, absl::string_view bytes) {
arena) == kUpb_DecodeStatus_Ok;
}

template <typename T>
bool Parse(Ptr<T>& message, absl::string_view bytes) {
_upb_Message_Clear(message->msg(), T::minitable());
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena());
return upb_Decode(bytes.data(), bytes.size(), message->msg(), T::minitable(),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}

template <typename T>
bool Parse(std::unique_ptr<T>& message, absl::string_view bytes) {
_upb_Message_Clear(message->msg(), T::minitable());
Expand Down Expand Up @@ -430,8 +439,7 @@ absl::StatusOr<absl::string_view> Serialize(std::shared_ptr<T>& message,
}

template <typename T>
absl::StatusOr<absl::string_view> Serialize(Ptr<const T> message,
upb::Arena& arena,
absl::StatusOr<absl::string_view> Serialize(Ptr<T> message, upb::Arena& arena,
int options = 0) {
return ::protos::internal::Serialize(message->msg(), T::minitable(),
arena.ptr(), options);
Expand Down
1 change: 1 addition & 0 deletions protos_generator/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,6 @@ cc_test(
":test_model_upb_proto",
"@com_google_googletest//:gtest_main",
"//:upb",
"//protos",
],
)
19 changes: 18 additions & 1 deletion protos_generator/tests/test_generated.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include <limits>

#include "gtest/gtest.h"
#include "protos/protos.h"
#include "protos_generator/tests/child_model.upb.proto.h"
#include "protos_generator/tests/no_package.upb.proto.h"
#include "protos_generator/tests/test_model.upb.proto.h"
#include "upb/string_view.h"

using ::protos_generator::test::protos::ChildModel1;
using ::protos_generator::test::protos::other_ext;
Expand Down Expand Up @@ -559,6 +559,23 @@ TEST(CppGeneratedCode, Parse) {
EXPECT_EQ(false, ::protos::GetExtension(parsed_model, theme).ok());
}

TEST(CppGeneratedCode, ParseIntoPtrToModel) {
TestModel model;
model.set_str1("Test123");
ThemeExtension extension1;
extension1.set_ext_name("Hello World");
EXPECT_EQ(true, ::protos::SetExtension(model, theme, extension1).ok());
::upb::Arena arena;
auto bytes = ::protos::Serialize(model, arena);
EXPECT_EQ(true, bytes.ok());
::protos::Ptr<TestModel> parsed_model =
::protos::CreateMessage<TestModel>(arena);
EXPECT_TRUE(::protos::Parse(parsed_model, bytes.value()));
EXPECT_EQ("Test123", parsed_model->str1());
// Should not return an extension since we did not pass ExtensionRegistry.
EXPECT_EQ(false, ::protos::GetExtension(parsed_model, theme).ok());
}

TEST(CppGeneratedCode, ParseWithExtensionRegistry) {
TestModel model;
model.set_str1("Test123");
Expand Down

0 comments on commit b5384af

Please sign in to comment.