From 8eeee0e3cab9dc9af407275dbafa1bd6322787cf Mon Sep 17 00:00:00 2001 From: Hong Shin Date: Wed, 6 Nov 2024 11:22:52 -0800 Subject: [PATCH] hpb: implement SetExtension for scalars i32 and i64 PiperOrigin-RevId: 693799584 --- .../Reflection/FeatureSetDescriptor.g.cs | 17 ------- hpb/extension.h | 44 +++++++++++++------ hpb_generator/tests/test_generated.cc | 21 ++++++++- 3 files changed, 50 insertions(+), 32 deletions(-) delete mode 100644 csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs diff --git a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs b/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs deleted file mode 100644 index 208ce1fcb6..0000000000 --- a/csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs +++ /dev/null @@ -1,17 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file or at -// https://developers.google.com/open-source/licenses/bsd -#endregion - -namespace Google.Protobuf.Reflection; - -internal sealed partial class FeatureSetDescriptor -{ - // Canonical serialized form of the edition defaults, generated by embed_edition_defaults. - private const string DefaultsBase64 = - "ChMYhAciACoMCAEQAhgCIAMoATACChMY5wciACoMCAIQARgBIAIoATABChMY6AciDAgBEAEYASACKAEwASoAIOYHKOgH"; -} diff --git a/hpb/extension.h b/hpb/extension.h index e85eb21537..57c75970bd 100644 --- a/hpb/extension.h +++ b/hpb/extension.h @@ -13,6 +13,8 @@ #include #include "absl/base/attributes.h" +#include "absl/log/absl_log.h" +#include "absl/status/status.h" #include "absl/status/statusor.h" #include "google/protobuf/hpb/backend/upb/interop.h" #include "google/protobuf/hpb/internal/message_lock.h" @@ -55,6 +57,7 @@ struct UpbExtensionTrait { using DefaultType = int32_t; using ReturnType = int32_t; static constexpr auto kGetter = upb_Message_GetExtensionInt32; + static constexpr auto kSetter = upb_Message_SetExtensionInt32; }; template <> @@ -62,6 +65,7 @@ struct UpbExtensionTrait { using DefaultType = int64_t; using ReturnType = int64_t; static constexpr auto kGetter = upb_Message_GetExtensionInt64; + static constexpr auto kSetter = upb_Message_SetExtensionInt64; }; // TODO: b/375460289 - flesh out non-promotional msg support that does @@ -184,11 +188,18 @@ absl::Status SetExtension( Ptr message, const ::hpb::internal::ExtensionIdentifier& id, const Extension& value) { - static_assert(!std::is_const_v); - auto* message_arena = hpb::interop::upb::GetArena(message); - return ::hpb::internal::SetExtension(hpb::interop::upb::GetMessage(message), - message_arena, id.mini_table_ext(), - hpb::interop::upb::GetMessage(&value)); + if constexpr (std::is_integral_v) { + bool res = hpb::internal::UpbExtensionTrait::kSetter( + hpb::interop::upb::GetMessage(message), id.mini_table_ext(), value, + hpb::interop::upb::GetArena(message)); + return res ? absl::OkStatus() : MessageAllocationError(); + } else { + static_assert(!std::is_const_v); + auto* message_arena = hpb::interop::upb::GetArena(message); + return ::hpb::internal::SetExtension(hpb::interop::upb::GetMessage(message), + message_arena, id.mini_table_ext(), + hpb::interop::upb::GetMessage(&value)); + } } template message, const ::hpb::internal::ExtensionIdentifier& id, Extension&& value) { - Extension ext = std::move(value); - static_assert(!std::is_const_v); - auto* message_arena = hpb::interop::upb::GetArena(message); - auto* extension_arena = hpb::interop::upb::GetArena(&ext); - return ::hpb::internal::MoveExtension(hpb::interop::upb::GetMessage(message), - message_arena, id.mini_table_ext(), - hpb::interop::upb::GetMessage(&ext), - extension_arena); + if constexpr (std::is_integral_v) { + bool res = hpb::internal::UpbExtensionTrait::kSetter( + hpb::interop::upb::GetMessage(message), id.mini_table_ext(), value, + hpb::interop::upb::GetArena(message)); + return res ? absl::OkStatus() : MessageAllocationError(); + } else { + Extension ext = std::forward(value); + static_assert(!std::is_const_v); + auto* message_arena = hpb::interop::upb::GetArena(message); + auto* extension_arena = hpb::interop::upb::GetArena(&ext); + return ::hpb::internal::MoveExtension( + hpb::interop::upb::GetMessage(message), message_arena, + id.mini_table_ext(), hpb::interop::upb::GetMessage(&ext), + extension_arena); + } } template #include #include #include #include #include -#include #include #include @@ -58,6 +56,7 @@ using ::hpb_unittest::protos::ThemeExtension; using ::hpb_unittest::someotherpackage::protos::int32_ext; using ::hpb_unittest::someotherpackage::protos::int64_ext; using ::testing::ElementsAre; +using ::testing::status::IsOkAndHolds; TEST(CppGeneratedCode, Constructor) { TestModel test_model; } @@ -734,6 +733,24 @@ TEST(CppGeneratedCode, ClearExtensionWithEmptyExtensionPtr) { EXPECT_EQ(false, ::hpb::HasExtension(recursive_child, theme)); } +TEST(CppGeneratedCode, SetExtensionInt32) { + TestModel model; + EXPECT_EQ(false, hpb::HasExtension(&model, int32_ext)); + int32_t val = 55; + auto x = hpb::SetExtension(&model, int32_ext, val); + EXPECT_EQ(true, hpb::HasExtension(&model, int32_ext)); + EXPECT_THAT(hpb::GetExtension(&model, int32_ext), IsOkAndHolds(val)); +} + +TEST(CppGeneratedCode, SetExtensionInt64) { + TestModel model; + EXPECT_EQ(false, hpb::HasExtension(&model, int64_ext)); + int64_t val = std::numeric_limits::max() + int64_t{1}; + auto x = hpb::SetExtension(&model, int64_ext, val); + EXPECT_EQ(true, hpb::HasExtension(&model, int64_ext)); + EXPECT_THAT(hpb::GetExtension(&model, int64_ext), IsOkAndHolds(val)); +} + TEST(CppGeneratedCode, SetExtension) { TestModel model; void* prior_message;