Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions eval/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ cc_library(
"//runtime/internal:issue_collector",
"//runtime/internal:runtime_env",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
Expand All @@ -150,7 +149,6 @@ cc_library(
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_absl//absl/types:span",
"@com_google_absl//absl/types:variant",
"@com_google_protobuf//:protobuf",
Expand Down
2 changes: 1 addition & 1 deletion eval/compiler/flat_expr_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,7 @@ absl::StatusOr<FlatExpression> FlatExprBuilder::CreateExpressionImpl(
const cel::TypeProvider& FlatExprBuilder::GetTypeProvider() const {
return use_legacy_type_provider_
? static_cast<const cel::TypeProvider&>(
*GetLegacyRuntimeTypeProvider(type_registry_))
GetLegacyRuntimeTypeProvider(type_registry_))
: GetRuntimeTypeProvider(type_registry_);
}

Expand Down
1 change: 1 addition & 0 deletions eval/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ cc_library(
"//eval/public/structs:legacy_type_provider",
"//eval/public/structs:protobuf_descriptor_type_provider",
"//runtime:type_registry",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
Expand Down
21 changes: 18 additions & 3 deletions eval/public/cel_type_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <utility>
#include <vector>

#include "absl/base/call_once.h"
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
Expand All @@ -28,6 +29,7 @@
#include "base/type_provider.h"
#include "eval/public/structs/legacy_type_adapter.h"
#include "eval/public/structs/legacy_type_provider.h"
#include "eval/public/structs/protobuf_descriptor_type_provider.h"
#include "runtime/type_registry.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
Expand Down Expand Up @@ -60,7 +62,9 @@ class CelTypeRegistry {

CelTypeRegistry(const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nullable message_factory)
: modern_type_registry_(descriptor_pool, message_factory) {}
: descriptor_pool_(descriptor_pool),
message_factory_(message_factory),
modern_type_registry_(descriptor_pool, message_factory) {}

~CelTypeRegistry() = default;

Expand All @@ -77,8 +81,12 @@ class CelTypeRegistry {

// Get the first registered type provider.
std::shared_ptr<const LegacyTypeProvider> GetFirstTypeProvider() const {
return cel::runtime_internal::GetLegacyRuntimeTypeProvider(
modern_type_registry_);
absl::call_once(legacy_type_provider_once_, [&]() {
this->legacy_type_provider_ = std::make_shared<
google::api::expr::runtime::ProtobufDescriptorProvider>(
descriptor_pool_, message_factory_);
});
return legacy_type_provider_;
}

// Returns the effective type provider that has been configured with the
Expand Down Expand Up @@ -136,6 +144,13 @@ class CelTypeRegistry {
}

private:
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool_;
google::protobuf::MessageFactory* absl_nullable message_factory_;

// Legacy type provider. This is now disconnected from the actual type
// resolution, but preserved for legacy clients that used it directly.
mutable absl::once_flag legacy_type_provider_once_;
mutable std::shared_ptr<LegacyTypeProvider> legacy_type_provider_;
// Internal modern registry.
cel::TypeRegistry modern_type_registry_;
};
Expand Down
8 changes: 2 additions & 6 deletions runtime/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,14 @@ cc_library(
srcs = ["legacy_runtime_type_provider.cc"],
hdrs = ["legacy_runtime_type_provider.h"],
deps = [
":runtime_type_provider",
"//common:legacy_value",
"//common:type",
"//common:value",
"//eval/public:message_wrapper",
"//eval/public/structs:legacy_type_info_apis",
"//eval/public/structs:protobuf_descriptor_type_provider",
"//internal:status_macros",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
],
)
Expand All @@ -204,13 +201,12 @@ cc_test(
srcs = ["legacy_runtime_type_provider_test.cc"],
deps = [
":legacy_runtime_type_provider",
":runtime_type_provider",
"//common:type",
"//common:value",
"//internal:testing",
"//internal:testing_descriptor_pool",
"//internal:testing_message_factory",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/types:optional",
"@com_google_cel_spec//proto/cel/expr/conformance/proto3:test_all_types_cc_proto",
"@com_google_protobuf//:protobuf",
],
Expand Down
56 changes: 0 additions & 56 deletions runtime/internal/legacy_runtime_type_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,16 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "common/legacy_value.h"
#include "common/type.h"
#include "common/type_introspector.h"
#include "common/value.h"
#include "common/values/value_builder.h"
#include "eval/public/message_wrapper.h"
#include "eval/public/structs/legacy_type_info_apis.h"
#include "internal/status_macros.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace cel::runtime_internal {

namespace {

using google::api::expr::runtime::LegacyTypeInfoApis;
using google::api::expr::runtime::MessageWrapper;

class LegacyValueBuilder final : public cel::ValueBuilder {
public:
LegacyValueBuilder(google::protobuf::Arena* absl_nonnull arena,
Expand Down Expand Up @@ -92,52 +84,4 @@ LegacyRuntimeTypeProvider::NewValueBuilder(
return std::make_unique<LegacyValueBuilder>(arena, std::move(builder));
}

absl::StatusOr<std::optional<Type>> LegacyRuntimeTypeProvider::FindTypeImpl(
absl::string_view name) const {
if (auto type = cel::FindWellKnownType(name); type.has_value()) {
return type;
}
if (auto type_info = ProvideLegacyTypeInfo(name); type_info.has_value()) {
const auto* descriptor = (*type_info)->GetDescriptor(MessageWrapper());
if (descriptor != nullptr) {
return cel::MessageType(descriptor);
}
return cel::common_internal::MakeBasicStructType(
(*type_info)->GetTypename(MessageWrapper()));
}
return std::nullopt;
}

absl::StatusOr<std::optional<StructTypeField>>
LegacyRuntimeTypeProvider::FindStructTypeFieldByNameImpl(
absl::string_view type, absl::string_view name) const {
if (auto result = cel::FindWellKnownTypeFieldByName(type, name);
result.has_value()) {
return result;
}
std::optional<const LegacyTypeInfoApis*> type_info =
ProvideLegacyTypeInfo(type);
if (!type_info.has_value()) {
return std::nullopt;
}
if (const auto* descriptor = (*type_info)->GetDescriptor(MessageWrapper());
descriptor != nullptr) {
// If it's a normal proto, just use the descriptor to find the field.
// Allows us to get the same optimizations as the modern value in most
// cases.
const google::protobuf::FieldDescriptor* field = descriptor->FindFieldByName(name);
if (field != nullptr) {
return cel::StructTypeField(cel::MessageTypeField(field));
}
}

if (auto field_desc = (*type_info)->FindFieldByName(name);
field_desc.has_value()) {
return cel::common_internal::BasicStructTypeField(
field_desc->name, field_desc->number, cel::DynType{});
}

return std::nullopt;
}

} // namespace cel::runtime_internal
26 changes: 16 additions & 10 deletions runtime/internal/legacy_runtime_type_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@
#include "common/type.h"
#include "common/type_reflector.h"
#include "common/value.h"
#include "eval/public/structs/protobuf_descriptor_type_provider.h"
#include "runtime/internal/runtime_type_provider.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace cel::runtime_internal {

class LegacyRuntimeTypeProvider final
: public google::api::expr::runtime::ProtobufDescriptorProvider,
public TypeReflector {
// LegacyRuntimeTypeProvider is a TypeReflector that uses a RuntimeTypeProvider
// internally to provide types with the google::api::expr::runtime::CelValue
// APIs. It prefers to create wrapped legacy values but otherwise proxies to
// the standard RuntimeTypeProvider.
class LegacyRuntimeTypeProvider final : public TypeReflector {
public:
LegacyRuntimeTypeProvider(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nullable message_factory)
: google::api::expr::runtime::ProtobufDescriptorProvider(descriptor_pool,
message_factory),
descriptor_pool_(descriptor_pool) {}
const RuntimeTypeProvider* absl_nonnull runtime_type_provider)
: descriptor_pool_(descriptor_pool),
runtime_type_provider_(runtime_type_provider) {}

absl::StatusOr<absl_nullable ValueBuilderPtr> NewValueBuilder(
absl::string_view name,
Expand All @@ -48,13 +49,18 @@ class LegacyRuntimeTypeProvider final

protected:
absl::StatusOr<std::optional<Type>> FindTypeImpl(
absl::string_view name) const override;
absl::string_view name) const override {
return runtime_type_provider_->FindType(name);
}

absl::StatusOr<std::optional<StructTypeField>> FindStructTypeFieldByNameImpl(
absl::string_view type, absl::string_view name) const override;
absl::string_view type, absl::string_view name) const override {
return runtime_type_provider_->FindStructTypeFieldByName(type, name);
}

private:
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool_;
const RuntimeTypeProvider* absl_nonnull runtime_type_provider_;
};

} // namespace cel::runtime_internal
Expand Down
34 changes: 19 additions & 15 deletions runtime/internal/legacy_runtime_type_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "internal/testing.h"
#include "internal/testing_descriptor_pool.h"
#include "internal/testing_message_factory.h"
#include "runtime/internal/runtime_type_provider.h"
#include "cel/expr/conformance/proto3/test_all_types.pb.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
Expand All @@ -33,8 +34,9 @@ namespace {
using ::cel::expr::conformance::proto3::TestAllTypes;

TEST(LegacyRuntimeTypeProviderTest, FindType) {
RuntimeTypeProvider type_provider(cel::internal::GetTestingDescriptorPool());
LegacyRuntimeTypeProvider provider(cel::internal::GetTestingDescriptorPool(),
cel::internal::GetTestingMessageFactory());
&type_provider);
ASSERT_OK_AND_ASSIGN(std::optional<Type> wrapper_type,
provider.FindType("google.protobuf.Int64Value"));
ASSERT_TRUE(wrapper_type.has_value());
Expand All @@ -50,18 +52,18 @@ TEST(LegacyRuntimeTypeProviderTest, FindType) {
}

TEST(LegacyRuntimeTypeProviderTest, FindTypeNotFound) {
LegacyRuntimeTypeProvider provider(
google::protobuf::DescriptorPool::generated_pool(),
google::protobuf::MessageFactory::generated_factory());
RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool());
LegacyRuntimeTypeProvider provider(google::protobuf::DescriptorPool::generated_pool(),
&type_provider);
ASSERT_OK_AND_ASSIGN(std::optional<Type> type,
provider.FindType("UnknownType"));
EXPECT_FALSE(type.has_value());
}

TEST(LegacyRuntimeTypeProviderTest, FindStructTypeFieldByName) {
LegacyRuntimeTypeProvider provider(
google::protobuf::DescriptorPool::generated_pool(),
google::protobuf::MessageFactory::generated_factory());
RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool());
LegacyRuntimeTypeProvider provider(google::protobuf::DescriptorPool::generated_pool(),
&type_provider);
ASSERT_OK_AND_ASSIGN(std::optional<StructTypeField> field,
provider.FindStructTypeFieldByName(
"google.protobuf.Int64Value", "value"));
Expand All @@ -72,9 +74,9 @@ TEST(LegacyRuntimeTypeProviderTest, FindStructTypeFieldByName) {
}

TEST(LegacyRuntimeTypeProviderTest, FindStructTypeFieldByNameNotFound) {
LegacyRuntimeTypeProvider provider(
google::protobuf::DescriptorPool::generated_pool(),
google::protobuf::MessageFactory::generated_factory());
RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool());
LegacyRuntimeTypeProvider provider(google::protobuf::DescriptorPool::generated_pool(),
&type_provider);
ASSERT_OK_AND_ASSIGN(std::optional<StructTypeField> field,
provider.FindStructTypeFieldByName(
"google.protobuf.Int64Value", "unknown_field"));
Expand All @@ -87,8 +89,9 @@ TEST(LegacyRuntimeTypeProviderTest, FindStructTypeFieldByNameNotFound) {
}

TEST(LegacyRuntimeTypeProviderTest, NewValueBuilderMessage) {
RuntimeTypeProvider type_provider(cel::internal::GetTestingDescriptorPool());
LegacyRuntimeTypeProvider provider(cel::internal::GetTestingDescriptorPool(),
cel::internal::GetTestingMessageFactory());
&type_provider);
google::protobuf::Arena arena;
ASSERT_OK_AND_ASSIGN(auto builder,
provider.NewValueBuilder(
Expand All @@ -109,8 +112,9 @@ TEST(LegacyRuntimeTypeProviderTest, NewValueBuilderMessage) {
}

TEST(LegacyRuntimeTypeProviderTest, NewValueBuilderWellKnownType) {
RuntimeTypeProvider type_provider(cel::internal::GetTestingDescriptorPool());
LegacyRuntimeTypeProvider provider(cel::internal::GetTestingDescriptorPool(),
cel::internal::GetTestingMessageFactory());
&type_provider);
google::protobuf::Arena arena;
ASSERT_OK_AND_ASSIGN(auto builder,
provider.NewValueBuilder(
Expand All @@ -128,9 +132,9 @@ TEST(LegacyRuntimeTypeProviderTest, NewValueBuilderWellKnownType) {
}

TEST(LegacyRuntimeTypeProviderTest, NewValueBuilderNotFound) {
LegacyRuntimeTypeProvider provider(
google::protobuf::DescriptorPool::generated_pool(),
google::protobuf::MessageFactory::generated_factory());
RuntimeTypeProvider type_provider(google::protobuf::DescriptorPool::generated_pool());
LegacyRuntimeTypeProvider provider(google::protobuf::DescriptorPool::generated_pool(),
&type_provider);
google::protobuf::LinkMessageReflection<TestAllTypes>();
google::protobuf::Arena arena;
ASSERT_OK_AND_ASSIGN(
Expand Down
4 changes: 1 addition & 3 deletions runtime/type_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ TypeRegistry::TypeRegistry(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nullable message_factory)
: type_provider_(descriptor_pool),
legacy_type_provider_(
std::make_shared<runtime_internal::LegacyRuntimeTypeProvider>(
descriptor_pool, message_factory)) {
legacy_type_provider_(descriptor_pool, &type_provider_) {
RegisterEnum("google.protobuf.NullValue", {{"NULL_VALUE", 0}});
}

Expand Down
Loading
Loading