From b3086c3e20793620c4ad781af86d182d7f74855d Mon Sep 17 00:00:00 2001 From: Justin King Date: Fri, 14 Mar 2025 06:44:51 -0700 Subject: [PATCH] Expose minimal descriptor set as `google::protobuf::DescriptorDatabase` PiperOrigin-RevId: 736846057 --- common/BUILD | 23 ++- common/minimal_descriptor_database.cc | 27 ++++ common/minimal_descriptor_database.h | 32 ++++ common/minimal_descriptor_database_test.cc | 139 ++++++++++++++++++ internal/BUILD | 15 +- internal/minimal_descriptor_database.h | 32 ++++ ...criptor_pool.cc => minimal_descriptors.cc} | 12 +- 7 files changed, 274 insertions(+), 6 deletions(-) create mode 100644 common/minimal_descriptor_database.cc create mode 100644 common/minimal_descriptor_database.h create mode 100644 common/minimal_descriptor_database_test.cc create mode 100644 internal/minimal_descriptor_database.h rename internal/{minimal_descriptor_pool.cc => minimal_descriptors.cc} (82%) diff --git a/common/BUILD b/common/BUILD index 20409ff69..125e8e834 100644 --- a/common/BUILD +++ b/common/BUILD @@ -820,7 +820,7 @@ cc_library( srcs = ["minimal_descriptor_pool.cc"], hdrs = ["minimal_descriptor_pool.h"], deps = [ - "//internal:minimal_descriptor_pool", + "//internal:minimal_descriptors", "@com_google_absl//absl/base:nullability", "@com_google_protobuf//:protobuf", ], @@ -836,6 +836,27 @@ cc_test( ], ) +cc_library( + name = "minimal_descriptor_database", + srcs = ["minimal_descriptor_database.cc"], + hdrs = ["minimal_descriptor_database.h"], + deps = [ + "//internal:minimal_descriptors", + "@com_google_absl//absl/base:nullability", + "@com_google_protobuf//:protobuf", + ], +) + +cc_test( + name = "minimal_descriptor_database_test", + srcs = ["minimal_descriptor_database_test.cc"], + deps = [ + ":minimal_descriptor_database", + "//internal:testing", + "@com_google_protobuf//:protobuf", + ], +) + cc_library( name = "function_descriptor", srcs = [ diff --git a/common/minimal_descriptor_database.cc b/common/minimal_descriptor_database.cc new file mode 100644 index 000000000..83215f5c1 --- /dev/null +++ b/common/minimal_descriptor_database.cc @@ -0,0 +1,27 @@ +// Copyright 2025 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_database.h" + +#include "absl/base/nullability.h" +#include "internal/minimal_descriptor_database.h" +#include "google/protobuf/descriptor_database.h" + +namespace cel { + +absl::Nonnull GetMinimalDescriptorDatabase() { + return internal::GetMinimalDescriptorDatabase(); +} + +} // namespace cel diff --git a/common/minimal_descriptor_database.h b/common/minimal_descriptor_database.h new file mode 100644 index 000000000..0b7767d9f --- /dev/null +++ b/common/minimal_descriptor_database.h @@ -0,0 +1,32 @@ +// Copyright 2025 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_DATABASE_H_ +#define THIRD_PARTY_CEL_CPP_COMMON_MINIMAL_DESCRIPTOR_DATABASE_H_ + +#include "absl/base/nullability.h" +#include "google/protobuf/descriptor_database.h" + +namespace cel { + +// GetMinimalDescriptorDatabase returns a pointer to a +// `google::protobuf::DescriptorDatabase` which includes has the minimally necessary +// descriptors required by the Common Expression Language. The returned +// `google::protobuf::DescriptorDatabase` is valid for the lifetime of the process and +// should not be deleted. +absl::Nonnull GetMinimalDescriptorDatabase(); + +} // namespace cel + +#endif // THIRD_PARTY_CEL_CPP_COMMON_MINIMAL_DESCRIPTOR_DATABASE_H_ diff --git a/common/minimal_descriptor_database_test.cc b/common/minimal_descriptor_database_test.cc new file mode 100644 index 000000000..e91d73cf6 --- /dev/null +++ b/common/minimal_descriptor_database_test.cc @@ -0,0 +1,139 @@ +// Copyright 2025 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_database.h" + +#include "google/protobuf/descriptor.pb.h" +#include "internal/testing.h" +#include "google/protobuf/descriptor.h" + +namespace cel { +namespace { + +using ::testing::IsTrue; + +TEST(GetMinimalDescriptorDatabase, NullValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.NullValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, BoolValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.BoolValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Int32Value) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Int32Value", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Int64Value) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Int64Value", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, UInt32Value) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.UInt32Value", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, UInt64Value) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.UInt64Value", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, FloatValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.FloatValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, DoubleValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.DoubleValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, BytesValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.BytesValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, StringValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.StringValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Any) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Any", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Duration) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Duration", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Timestamp) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Timestamp", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Value) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Value", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, ListValue) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.ListValue", &fd), + IsTrue()); +} + +TEST(GetMinimalDescriptorDatabase, Struct) { + google::protobuf::FileDescriptorProto fd; + EXPECT_THAT(GetMinimalDescriptorDatabase()->FindFileContainingSymbol( + "google.protobuf.Struct", &fd), + IsTrue()); +} + +} // namespace +} // namespace cel diff --git a/internal/BUILD b/internal/BUILD index 84b8d52d4..62581a9a9 100644 --- a/internal/BUILD +++ b/internal/BUILD @@ -434,13 +434,22 @@ cel_cc_embed( src = ":minimal_descriptor_set", ) -cc_library( +alias( name = "minimal_descriptor_pool", - srcs = ["minimal_descriptor_pool.cc"], - hdrs = ["minimal_descriptor_pool.h"], + actual = ":minimal_descriptors", +) + +cc_library( + name = "minimal_descriptors", + srcs = ["minimal_descriptors.cc"], + hdrs = [ + "minimal_descriptor_database.h", + "minimal_descriptor_pool.h", + ], textual_hdrs = [":minimal_descriptor_set_embed"], deps = [ "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/base:no_destructor", "@com_google_absl//absl/base:nullability", "@com_google_absl//absl/log:absl_check", "@com_google_protobuf//:protobuf", diff --git a/internal/minimal_descriptor_database.h b/internal/minimal_descriptor_database.h new file mode 100644 index 000000000..0ff32ece2 --- /dev/null +++ b/internal/minimal_descriptor_database.h @@ -0,0 +1,32 @@ +// Copyright 2025 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_INTERNAL_MINIMAL_DESCRIPTOR_DATABASE_H_ +#define THIRD_PARTY_CEL_CPP_INTERNAL_MINIMAL_DESCRIPTOR_DATABASE_H_ + +#include "absl/base/nullability.h" +#include "google/protobuf/descriptor_database.h" + +namespace cel::internal { + +// GetMinimalDescriptorDatabase returns a pointer to a +// `google::protobuf::DescriptorDatabase` which includes has the minimally necessary +// descriptors required by the Common Expression Language. The returning +// `google::protobuf::DescripDescriptorDatabasetorPool` is valid for the lifetime of the +// process. +absl::Nonnull GetMinimalDescriptorDatabase(); + +} // namespace cel::internal + +#endif // THIRD_PARTY_CEL_CPP_INTERNAL_MINIMAL_DESCRIPTOR_DATABASE_H_ diff --git a/internal/minimal_descriptor_pool.cc b/internal/minimal_descriptors.cc similarity index 82% rename from internal/minimal_descriptor_pool.cc rename to internal/minimal_descriptors.cc index 9ec79df50..8e232f15d 100644 --- a/internal/minimal_descriptor_pool.cc +++ b/internal/minimal_descriptors.cc @@ -12,16 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/minimal_descriptor_pool.h" - #include #include "google/protobuf/descriptor.pb.h" #include "absl/base/attributes.h" #include "absl/base/macros.h" +#include "absl/base/no_destructor.h" #include "absl/base/nullability.h" #include "absl/log/absl_check.h" +#include "internal/minimal_descriptor_database.h" +#include "internal/minimal_descriptor_pool.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor_database.h" namespace cel::internal { @@ -47,4 +49,10 @@ absl::Nonnull GetMinimalDescriptorPool( return pool; } +absl::Nonnull GetMinimalDescriptorDatabase() { + static absl::NoDestructor database( + *GetMinimalDescriptorPool()); + return &*database; +} + } // namespace cel::internal