Skip to content
Open
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
3 changes: 1 addition & 2 deletions common/descriptor_pool_type_introspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ FindStructTypeFieldByNameDirectly(
if (descriptor == nullptr) {
return std::nullopt;
}
const google::protobuf::FieldDescriptor* absl_nullable field =
descriptor->FindFieldByName(name);
const google::protobuf::FieldDescriptor* field = descriptor->FindFieldByName(name);
if (field != nullptr) {
return StructTypeField(MessageTypeField(field));
}
Expand Down
47 changes: 39 additions & 8 deletions common/legacy_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ namespace cel {

namespace {

using google::api::expr::runtime::CelList;
using google::api::expr::runtime::CelMap;
using google::api::expr::runtime::CelValue;
using google::api::expr::runtime::FieldBackedListImpl;
using google::api::expr::runtime::FieldBackedMapImpl;
using google::api::expr::runtime::GetGenericProtoTypeInfoInstance;
using google::api::expr::runtime::LegacyTypeInfoApis;
using google::api::expr::runtime::MessageWrapper;
using ::google::api::expr::runtime::CelList;
using ::google::api::expr::runtime::CelMap;
using ::google::api::expr::runtime::CelValue;
using ::google::api::expr::runtime::CreateCelValueFromField;
using ::google::api::expr::runtime::FieldBackedListImpl;
using ::google::api::expr::runtime::FieldBackedMapImpl;
using ::google::api::expr::runtime::GetGenericProtoTypeInfoInstance;
using ::google::api::expr::runtime::LegacyTypeInfoApis;
using ::google::api::expr::runtime::MessageWrapper;
using ::google::api::expr::runtime::internal::MaybeWrapValueToMessage;

absl::Status InvalidMapKeyTypeError(ValueKind kind) {
Expand Down Expand Up @@ -1288,6 +1289,36 @@ TypeValue CreateTypeValueFromView(google::protobuf::Arena* arena,
return TypeValue(common_internal::LegacyRuntimeType(input));
}

const google::protobuf::Message* absl_nullable GetLegacyMessage(const Value& value) {
if (!common_internal::IsLegacyStructValue(value)) {
return nullptr;
}

auto legacy = common_internal::GetLegacyStructValue(value);
const auto* legacy_type_info = legacy.legacy_type_info();
if (legacy_type_info == nullptr) {
return nullptr;
}
if (legacy_type_info != &GetGenericProtoTypeInfoInstance()) {
return nullptr;
}
if (IsWellKnownMessageType(legacy.message_ptr()->GetDescriptor())) {
return nullptr;
}
return legacy.message_ptr();
}

absl::Status WrapLegacyMessageField(
const google::protobuf::Message* absl_nonnull message,
const google::protobuf::FieldDescriptor* absl_nonnull field_descriptor,
ProtoWrapperTypeOptions unboxing_option, google::protobuf::Arena* arena,
Value* absl_nonnull out) {
CEL_ASSIGN_OR_RETURN(CelValue result,
CreateCelValueFromField(message, field_descriptor,
unboxing_option, arena));
return ModernValue(arena, result, *out);
}

} // namespace interop_internal

} // namespace cel
15 changes: 15 additions & 0 deletions common/legacy_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include "common/value.h"
#include "eval/public/cel_value.h"
#include "internal/status_macros.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"

namespace cel {

Expand Down Expand Up @@ -59,6 +61,19 @@ google::api::expr::runtime::CelValue UnsafeLegacyValue(

namespace cel::interop_internal {

// Returns the underlying `google::protobuf::Message` of a `cel::Value` if it is a legacy
// message with the default type info, or `nullptr` otherwise.
const google::protobuf::Message* absl_nullable GetLegacyMessage(const Value& value);

// Access a field on a legacy message value, writing the result to `out`.
// Prefers wrapping legacy values instead of using the modern value
// representation.
absl::Status WrapLegacyMessageField(
const google::protobuf::Message* absl_nonnull message,
const google::protobuf::FieldDescriptor* absl_nonnull field_descriptor,
ProtoWrapperTypeOptions unboxing_option, google::protobuf::Arena* arena,
Value* absl_nonnull out);

absl::StatusOr<Value> FromLegacyValue(
google::protobuf::Arena* arena,
const google::api::expr::runtime::CelValue& legacy_value,
Expand Down
5 changes: 5 additions & 0 deletions common/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ absl::optional<MessageTypeField> StructTypeField::AsMessage() const {
return std::nullopt;
}

MessageTypeField StructTypeField::GetMessage() const {
ABSL_DCHECK(IsMessage());
return absl::get<MessageTypeField>(variant_);
}

StructTypeField::operator MessageTypeField() const {
ABSL_DCHECK(IsMessage());
return absl::get<MessageTypeField>(variant_);
Expand Down
1 change: 1 addition & 0 deletions common/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ class StructTypeField final {
}

absl::optional<MessageTypeField> AsMessage() const;
MessageTypeField GetMessage() const;

explicit operator MessageTypeField() const;

Expand Down
13 changes: 11 additions & 2 deletions common/types/message_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class MessageType final {
return descriptor_;
}

const google::protobuf::Descriptor* absl_nonnull descriptor() const {
ABSL_DCHECK(*this);
return descriptor_;
}

explicit operator bool() const { return descriptor_ != nullptr; }

private:
Expand Down Expand Up @@ -166,8 +171,12 @@ class MessageTypeField final {
return *descriptor_;
}

const google::protobuf::FieldDescriptor* absl_nonnull operator->() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
const google::protobuf::FieldDescriptor* absl_nonnull operator->() const {
ABSL_DCHECK(*this);
return descriptor_;
}

const google::protobuf::FieldDescriptor* absl_nonnull descriptor() const {
ABSL_DCHECK(*this);
return descriptor_;
}
Expand Down
1 change: 1 addition & 0 deletions common/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <type_traits>
Expand Down
24 changes: 11 additions & 13 deletions common/values/parsed_message_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ class ParsedMessageValue final
return *value_;
}

const google::protobuf::Message* absl_nonnull operator->() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_;
}
const google::protobuf::Message* absl_nonnull operator->() const { return value_; }
const google::protobuf::Message* absl_nonnull message() const { return value_; }

bool IsZeroValue() const;

Expand Down Expand Up @@ -175,6 +173,15 @@ class ParsedMessageValue final
swap(lhs.arena_, rhs.arena_);
}

absl::Status GetField(
const google::protobuf::FieldDescriptor* absl_nonnull field,
ProtoWrapperTypeOptions unboxing_options,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const;

bool HasField(const google::protobuf::FieldDescriptor* absl_nonnull field) const;

private:
friend std::pointer_traits<ParsedMessageValue>;
friend class StructValue;
Expand Down Expand Up @@ -203,15 +210,6 @@ class ParsedMessageValue final
return absl::OkStatus();
}

absl::Status GetField(
const google::protobuf::FieldDescriptor* absl_nonnull field,
ProtoWrapperTypeOptions unboxing_options,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const;

bool HasField(const google::protobuf::FieldDescriptor* absl_nonnull field) const;

const google::protobuf::Message* absl_nonnull value_;
// Arena that is attributed as owning the value. May be null to indicate that
// the value is managed externally.
Expand Down
1 change: 1 addition & 0 deletions conformance/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ cc_library(
"//runtime:constant_folding",
"//runtime:optional_types",
"//runtime:reference_resolver",
"//runtime:regex_precompilation",
"//runtime:runtime_options",
"//runtime:standard_runtime_builder_factory",
"//testutil:test_macros",
Expand Down
6 changes: 6 additions & 0 deletions conformance/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#include "runtime/constant_folding.h"
#include "runtime/optional_types.h"
#include "runtime/reference_resolver.h"
#include "runtime/regex_precompilation.h"
#include "runtime/runtime.h"
#include "runtime/runtime_options.h"
#include "runtime/standard_runtime_builder_factory.h"
Expand Down Expand Up @@ -283,6 +284,7 @@ class LegacyConformanceServiceImpl : public ConformanceServiceInterface {
std::cerr << "Enabling optimizations" << std::endl;
options.constant_folding = true;
options.constant_arena = constant_arena;
options.enable_typed_field_access = true;
}

if (select_optimization) {
Expand Down Expand Up @@ -486,13 +488,17 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface {
absl::string_view container) {
RuntimeOptions options(options_);
options.container = std::string(container);
if (enable_optimizations_) {
options.enable_typed_field_access = true;
}
CEL_ASSIGN_OR_RETURN(
auto builder, CreateStandardRuntimeBuilder(
google::protobuf::DescriptorPool::generated_pool(), options));

if (enable_optimizations_) {
CEL_RETURN_IF_ERROR(cel::extensions::EnableConstantFolding(
builder, google::protobuf::MessageFactory::generated_factory()));
CEL_RETURN_IF_ERROR(cel::extensions::EnableRegexPrecompilation(builder));
}
CEL_RETURN_IF_ERROR(cel::EnableReferenceResolver(
builder, cel::ReferenceResolverEnabled::kAlways));
Expand Down
1 change: 1 addition & 0 deletions eval/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ cc_library(
"//common:expr",
"//common:kind",
"//common:type",
"//common:type_spec_resolver",
"//common:value",
"//eval/eval:comprehension_step",
"//eval/eval:const_value_step",
Expand Down
Loading
Loading