hpb: Improve ExtensionRegistry ergonomics

We introduce AddExtension, which'll take an ExtensionId. From there, our helper extracts the mini_table_ext() ptr and handles the upb_ExtensionRegistry behind the scenes.

The ctor for an ExtensionRegistry has been updated to just take an arena.

Just need to supply an extension, and let us handle the rest!

PiperOrigin-RevId: 696136849
pull/19247/head
Hong Shin 2 weeks ago committed by Copybara-Service
parent f3ede44f0d
commit 2727412a97
  1. 17
      csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs
  2. 20
      hpb/extension.h
  3. 26
      hpb/internal/message_lock_test.cc
  4. 12
      hpb_generator/tests/test_generated.cc

@ -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";
}

@ -127,18 +127,16 @@ upb_ExtensionRegistry* GetUpbExtensions(
class ExtensionRegistry {
public:
ExtensionRegistry(
const std::vector<const upb_MiniTableExtension*>& extensions,
const upb::Arena& arena)
: registry_(upb_ExtensionRegistry_New(arena.ptr())) {
explicit ExtensionRegistry(const upb::Arena& arena)
: registry_(upb_ExtensionRegistry_New(arena.ptr())) {}
template <typename ExtensionIdentifier>
void AddExtension(const ExtensionIdentifier& id) {
if (registry_) {
for (const auto extension : extensions) {
const auto* ext = extension;
bool success = upb_ExtensionRegistry_AddArray(registry_, &ext, 1);
if (!success) {
registry_ = nullptr;
break;
}
auto* extension = id.mini_table_ext();
bool success = upb_ExtensionRegistry_AddArray(registry_, &extension, 1);
if (!success) {
registry_ = nullptr;
}
}
}

@ -18,6 +18,7 @@
#include "absl/hash/hash.h"
#include "absl/log/absl_check.h"
#include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
#include "google/protobuf/hpb/extension.h"
#include "google/protobuf/hpb/hpb.h"
#include "upb/mem/arena.hpp"
#include "upb/mini_table/extension.h"
@ -105,24 +106,27 @@ void TestConcurrentExtensionAccess(::hpb::ExtensionRegistry registry) {
}
TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceBothLazy) {
::upb::Arena arena;
TestConcurrentExtensionAccess({{}, arena});
upb::Arena arena;
hpb::ExtensionRegistry registry(arena);
TestConcurrentExtensionAccess(registry);
}
TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceOneLazyOneEager) {
::upb::Arena arena;
std::vector<const upb_MiniTableExtension*> e1{theme.mini_table_ext()};
TestConcurrentExtensionAccess({e1, arena});
std::vector<const upb_MiniTableExtension*> e2{
ThemeExtension::theme_extension.mini_table_ext()};
TestConcurrentExtensionAccess({e2, arena});
upb::Arena arena;
hpb::ExtensionRegistry r1(arena);
r1.AddExtension(theme);
TestConcurrentExtensionAccess(r1);
hpb::ExtensionRegistry r2(arena);
r2.AddExtension(ThemeExtension::theme_extension);
TestConcurrentExtensionAccess(r2);
}
TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceBothEager) {
::upb::Arena arena;
std::vector<const upb_MiniTableExtension*> exts{
theme.mini_table_ext(), ThemeExtension::theme_extension.mini_table_ext()};
TestConcurrentExtensionAccess({exts, arena});
hpb::ExtensionRegistry registry(arena);
registry.AddExtension(theme);
registry.AddExtension(ThemeExtension::theme_extension);
TestConcurrentExtensionAccess(registry);
}
} // namespace

@ -1019,11 +1019,11 @@ TEST(CppGeneratedCode, ParseWithExtensionRegistry) {
::upb::Arena arena;
auto bytes = ::hpb::Serialize(&model, arena);
EXPECT_EQ(true, bytes.ok());
std::vector<const upb_MiniTableExtension*> exts{
theme.mini_table_ext(), other_ext.mini_table_ext(),
ThemeExtension::theme_extension.mini_table_ext()};
::hpb::ExtensionRegistry extensions(exts, arena);
hpb::ExtensionRegistry extensions(arena);
extensions.AddExtension(theme);
extensions.AddExtension(other_ext);
extensions.AddExtension(ThemeExtension::theme_extension);
TestModel parsed_model =
::hpb::Parse<TestModel>(bytes.value(), extensions).value();
EXPECT_EQ("Test123", parsed_model.str1());
@ -1287,8 +1287,8 @@ TEST(CppGeneratedCode, HasExtensionAndRegistry) {
std::string data = std::string(::hpb::Serialize(&source, arena).value());
// Test with ExtensionRegistry
std::vector<const upb_MiniTableExtension*> exts{theme.mini_table_ext()};
hpb::ExtensionRegistry extensions(exts, arena);
hpb::ExtensionRegistry extensions(arena);
extensions.AddExtension(theme);
TestModel parsed_model = ::hpb::Parse<TestModel>(data, extensions).value();
EXPECT_TRUE(::hpb::HasExtension(&parsed_model, theme));
}

Loading…
Cancel
Save