Skip to content

Commit

Permalink
Make the minimal descriptor pool public
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 691074913
  • Loading branch information
jcking authored and copybara-github committed Oct 29, 2024
1 parent a546afa commit 883d727
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
21 changes: 21 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -867,3 +867,24 @@ cc_test(
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "minimal_descriptor_pool",
srcs = ["minimal_descriptor_pool.cc"],
hdrs = ["minimal_descriptor_pool.h"],
deps = [
"//internal:minimal_descriptor_pool",
"@com_google_absl//absl/base:nullability",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "minimal_descriptor_pool_test",
srcs = ["minimal_descriptor_pool_test.cc"],
deps = [
":minimal_descriptor_pool",
"//internal:testing",
"@com_google_protobuf//:protobuf",
],
)
27 changes: 27 additions & 0 deletions common/minimal_descriptor_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/minimal_descriptor_pool.h"

#include "absl/base/nullability.h"
#include "internal/minimal_descriptor_pool.h"
#include "google/protobuf/descriptor.h"

namespace cel {

absl::Nonnull<const google::protobuf::DescriptorPool*> GetMinimalDescriptorPool() {
return internal::GetMinimalDescriptorPool();
}

} // namespace cel
31 changes: 31 additions & 0 deletions common/minimal_descriptor_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_COMMON_MINIMAL_DESCRIPTOR_POOL_H_
#define THIRD_PARTY_CEL_CPP_COMMON_MINIMAL_DESCRIPTOR_POOL_H_

#include "absl/base/nullability.h"
#include "google/protobuf/descriptor.h"

namespace cel {

// GetMinimalDescriptorPool returns a pointer to a `google::protobuf::DescriptorPool`
// which includes has the minimally necessary descriptors required by the Common
// Expression Language. The returned `google::protobuf::DescriptorPool` is valid for the
// lifetime of the process and should not be deleted.
absl::Nonnull<const google::protobuf::DescriptorPool*> GetMinimalDescriptorPool();

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_MINIMAL_DESCRIPTOR_POOL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -12,138 +12,138 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "internal/minimal_descriptor_pool.h"
#include "common/minimal_descriptor_pool.h"

#include "internal/testing.h"
#include "google/protobuf/descriptor.h"

namespace cel::internal {
namespace cel {
namespace {

using ::testing::NotNull;

TEST(MinimalDescriptorPool, NullValue) {
TEST(GetMinimalDescriptorPool, NullValue) {
ASSERT_THAT(GetMinimalDescriptorPool()->FindEnumTypeByName(
"google.protobuf.NullValue"),
NotNull());
}

TEST(MinimalDescriptorPool, BoolValue) {
TEST(GetMinimalDescriptorPool, BoolValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.BoolValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_BOOLVALUE);
}

TEST(MinimalDescriptorPool, Int32Value) {
TEST(GetMinimalDescriptorPool, Int32Value) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Int32Value");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_INT32VALUE);
}

TEST(MinimalDescriptorPool, Int64Value) {
TEST(GetMinimalDescriptorPool, Int64Value) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Int64Value");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_INT64VALUE);
}

TEST(MinimalDescriptorPool, UInt32Value) {
TEST(GetMinimalDescriptorPool, UInt32Value) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.UInt32Value");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_UINT32VALUE);
}

TEST(MinimalDescriptorPool, UInt64Value) {
TEST(GetMinimalDescriptorPool, UInt64Value) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.UInt64Value");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_UINT64VALUE);
}

TEST(MinimalDescriptorPool, FloatValue) {
TEST(GetMinimalDescriptorPool, FloatValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.FloatValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_FLOATVALUE);
}

TEST(MinimalDescriptorPool, DoubleValue) {
TEST(GetMinimalDescriptorPool, DoubleValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.DoubleValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_DOUBLEVALUE);
}

TEST(MinimalDescriptorPool, BytesValue) {
TEST(GetMinimalDescriptorPool, BytesValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.BytesValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_BYTESVALUE);
}

TEST(MinimalDescriptorPool, StringValue) {
TEST(GetMinimalDescriptorPool, StringValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.StringValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_STRINGVALUE);
}

TEST(MinimalDescriptorPool, Any) {
TEST(GetMinimalDescriptorPool, Any) {
const auto* desc =
GetMinimalDescriptorPool()->FindMessageTypeByName("google.protobuf.Any");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(), google::protobuf::Descriptor::WELLKNOWNTYPE_ANY);
}

TEST(MinimalDescriptorPool, Duration) {
TEST(GetMinimalDescriptorPool, Duration) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Duration");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_DURATION);
}

TEST(MinimalDescriptorPool, Timestamp) {
TEST(GetMinimalDescriptorPool, Timestamp) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Timestamp");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_TIMESTAMP);
}

TEST(MinimalDescriptorPool, Value) {
TEST(GetMinimalDescriptorPool, Value) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Value");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(), google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
}

TEST(MinimalDescriptorPool, ListValue) {
TEST(GetMinimalDescriptorPool, ListValue) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.ListValue");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_LISTVALUE);
}

TEST(MinimalDescriptorPool, Struct) {
TEST(GetMinimalDescriptorPool, Struct) {
const auto* desc = GetMinimalDescriptorPool()->FindMessageTypeByName(
"google.protobuf.Struct");
ASSERT_THAT(desc, NotNull());
EXPECT_EQ(desc->well_known_type(), google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
}

} // namespace
} // namespace cel::internal
} // namespace cel
10 changes: 0 additions & 10 deletions internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,6 @@ cc_library(
],
)

cc_test(
name = "minimal_descriptor_pool_test",
srcs = ["minimal_descriptor_pool_test.cc"],
deps = [
":minimal_descriptor_pool",
":testing",
"@com_google_protobuf//:protobuf",
],
)

cel_proto_transitive_descriptor_set(
name = "testing_descriptor_set",
testonly = True,
Expand Down

0 comments on commit 883d727

Please sign in to comment.