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
5 changes: 2 additions & 3 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,7 @@ cc_library(
"//eval/internal:cel_value_equal",
"//eval/public:cel_value",
"//eval/public:message_wrapper",
"//eval/public/containers:field_backed_list_impl",
"//eval/public/containers:field_backed_map_impl",
"//eval/public/structs:cel_proto_wrap_util",
"//eval/public/structs:legacy_type_adapter",
"//eval/public/structs:legacy_type_info_apis",
"//eval/public/structs:proto_message_type_adapter",
"//eval/public/structs:trivial_legacy_type_info_internal",
Expand Down Expand Up @@ -865,6 +862,8 @@ cc_test(
":value_kind",
":value_testing",
"//base:attributes",
"//eval/public:cel_value",
"//eval/public/structs:proto_message_type_adapter",
"//internal:parse_text_proto",
"//internal:status_macros",
"//internal:testing",
Expand Down
92 changes: 57 additions & 35 deletions common/legacy_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@
#include "common/unknown.h"
#include "common/value.h"
#include "common/value_kind.h"
#include "common/values/legacy_list_value.h"
#include "common/values/legacy_map_value.h"
#include "common/values/list_value_builder.h"
#include "common/values/map_value_builder.h"
#include "common/values/values.h"
#include "eval/internal/cel_value_equal.h"
#include "eval/public/cel_value.h"
#include "eval/public/containers/field_backed_list_impl.h"
#include "eval/public/containers/field_backed_map_impl.h"
#include "eval/public/message_wrapper.h"
#include "eval/public/structs/cel_proto_wrap_util.h"
#include "eval/public/structs/legacy_type_adapter.h"
#include "eval/public/structs/legacy_type_info_apis.h"
#include "eval/public/structs/proto_message_type_adapter.h"
#include "eval/public/structs/trivial_legacy_type_info_internal.h"
#include "internal/json.h"
#include "internal/status_macros.h"
#include "internal/well_known_types.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
Expand All @@ -77,8 +75,6 @@ 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;
Expand Down Expand Up @@ -284,19 +280,17 @@ CelValue LegacyTrivialListValue(google::protobuf::Arena* absl_nonnull arena,
}
if (auto parsed_repeated_field_value = value.AsParsedRepeatedField();
parsed_repeated_field_value) {
auto maybe_cloned = parsed_repeated_field_value->Clone(arena);
return CelValue::CreateList(google::protobuf::Arena::Create<FieldBackedListImpl>(
arena, &maybe_cloned.message(), maybe_cloned.field(), arena));
auto wrapped = common_internal::WrapLegacyParsedRepeatedField(
*parsed_repeated_field_value, arena);
return CelValue::CreateList(
common_internal::AsLegacyListValue(wrapped)->cel_list());
}
if (auto parsed_json_list_value = value.AsParsedJsonList();
parsed_json_list_value) {
auto maybe_cloned = parsed_json_list_value->Clone(arena);
return CelValue::CreateList(google::protobuf::Arena::Create<FieldBackedListImpl>(
arena, cel::to_address(maybe_cloned),
well_known_types::GetListValueReflectionOrDie(
maybe_cloned->GetDescriptor())
.GetValuesDescriptor(),
arena));
auto wrapped = common_internal::WrapLegacyParsedJsonList(
*parsed_json_list_value, arena);
return CelValue::CreateList(
common_internal::AsLegacyListValue(wrapped)->cel_list());
}
if (auto custom_list_value = value.AsCustomList(); custom_list_value) {
auto status_or_compat_list = common_internal::MakeCompatListValue(
Expand All @@ -322,19 +316,17 @@ CelValue LegacyTrivialMapValue(google::protobuf::Arena* absl_nonnull arena,
}
if (auto parsed_map_field_value = value.AsParsedMapField();
parsed_map_field_value) {
auto maybe_cloned = parsed_map_field_value->Clone(arena);
return CelValue::CreateMap(google::protobuf::Arena::Create<FieldBackedMapImpl>(
arena, &maybe_cloned.message(), maybe_cloned.field(), arena));
auto wrapped = common_internal::WrapLegacyParsedMapField(
*parsed_map_field_value, arena);
return CelValue::CreateMap(
common_internal::AsLegacyMapValue(wrapped)->cel_map());
}
if (auto parsed_json_map_value = value.AsParsedJsonMap();
parsed_json_map_value) {
auto maybe_cloned = parsed_json_map_value->Clone(arena);
return CelValue::CreateMap(google::protobuf::Arena::Create<FieldBackedMapImpl>(
arena, cel::to_address(maybe_cloned),
well_known_types::GetStructReflectionOrDie(
maybe_cloned->GetDescriptor())
.GetFieldsDescriptor(),
arena));
auto wrapped =
common_internal::WrapLegacyParsedJsonMap(*parsed_json_map_value, arena);
return CelValue::CreateMap(
common_internal::AsLegacyMapValue(wrapped)->cel_map());
}
if (auto custom_map_value = value.AsCustomMap(); custom_map_value) {
auto status_or_compat_map = common_internal::MakeCompatMapValue(
Expand All @@ -352,6 +344,25 @@ CelValue LegacyTrivialMapValue(google::protobuf::Arena* absl_nonnull arena,
value.GetRuntimeType().DebugString()))));
}

LegacyStructValue ParsedMessageToLegacyStructValue(
const ParsedMessageValue& parsed_message) {
return LegacyStructValue(cel::to_address(parsed_message),
&GetGenericProtoTypeInfoInstance());
}

LegacyStructValue MakeLegacyStructValue(
const google::protobuf::Message* absl_nonnull message,
const LegacyTypeInfoApis* legacy_type_info) {
// Guard against edge cases where a custom implementation of Message
// misbehaves.
// Modern value handles this with DCHECKs on value creation, legacy value
// would allow it and just report an ErrorValue on accesses.
if (message->GetReflection() == nullptr || legacy_type_info == nullptr) {
legacy_type_info = TrivialTypeInfo::GetInstance();
}
return LegacyStructValue(message, legacy_type_info);
}

} // namespace

google::api::expr::runtime::CelValue UnsafeLegacyValue(
Expand Down Expand Up @@ -394,10 +405,6 @@ google::api::expr::runtime::CelValue UnsafeLegacyValue(
}
}

} // namespace common_internal

namespace common_internal {

std::string LegacyListValue::DebugString() const {
return CelValue::CreateList(impl_).DebugString();
}
Expand Down Expand Up @@ -837,10 +844,8 @@ absl::Status LegacyStructValue::SerializeTo(
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(output != nullptr);

auto message_wrapper = AsMessageWrapper(message_ptr_, legacy_type_info_);
if (ABSL_PREDICT_TRUE(
message_wrapper.message_ptr()->SerializePartialToZeroCopyStream(
output))) {
message_ptr_->SerializePartialToZeroCopyStream(output))) {
return absl::OkStatus();
}
return absl::UnknownError("failed to serialize protocol buffer message");
Expand Down Expand Up @@ -1035,7 +1040,7 @@ absl::Status ModernValue(google::protobuf::Arena* arena,
return absl::OkStatus();
case CelValue::Type::kMessage: {
auto message_wrapper = legacy_value.MessageWrapperOrDie();
result = common_internal::LegacyStructValue(
result = common_internal::MakeLegacyStructValue(
google::protobuf::DownCastMessage<google::protobuf::Message>(
message_wrapper.message_ptr()),
message_wrapper.legacy_type_info());
Expand Down Expand Up @@ -1153,7 +1158,7 @@ absl::StatusOr<Value> FromLegacyValue(google::protobuf::Arena* arena,
legacy_value.BytesOrDie().value());
case CelValue::Type::kMessage: {
auto message_wrapper = legacy_value.MessageWrapperOrDie();
return common_internal::LegacyStructValue(
return common_internal::MakeLegacyStructValue(
google::protobuf::DownCastMessage<google::protobuf::Message>(
message_wrapper.message_ptr()),
message_wrapper.legacy_type_info());
Expand Down Expand Up @@ -1262,6 +1267,23 @@ google::api::expr::runtime::CelValue ModernValueToLegacyValueOrDie(
return std::move(*status_or_value);
}

void WrapLegacyFieldAccessResult(google::protobuf::Arena* absl_nonnull arena,
Value* absl_nonnull result) {
if (result->IsParsedMessage()) {
*result = common_internal::ParsedMessageToLegacyStructValue(
result->GetParsedMessage());
} else if (result->IsParsedRepeatedField()) {
*result =
WrapLegacyParsedRepeatedField(result->GetParsedRepeatedField(), arena);
} else if (result->IsParsedJsonList()) {
*result = WrapLegacyParsedJsonList(result->GetParsedJsonList(), arena);
} else if (result->IsParsedMapField()) {
*result = WrapLegacyParsedMapField(result->GetParsedMapField(), arena);
} else if (result->IsParsedJsonMap()) {
*result = WrapLegacyParsedJsonMap(result->GetParsedJsonMap(), arena);
}
}

TypeValue CreateTypeValueFromView(google::protobuf::Arena* arena,
absl::string_view input) {
return TypeValue(common_internal::LegacyRuntimeType(input));
Expand Down
11 changes: 11 additions & 0 deletions common/legacy_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,23 @@ google::api::expr::runtime::CelValue UnsafeLegacyValue(

} // namespace cel

namespace proto2 {
class MessageFactory;
} // namespace proto2

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);

// Helper for wrapping a field accesses for the legacy runtime.
//
// Adapts the output to avoid further allocations when converting to a legacy
// value when possible.
void WrapLegacyFieldAccessResult(google::protobuf::Arena* absl_nonnull arena,
Value* absl_nonnull result);

// Access a field on a legacy message value, writing the result to `out`.
// Prefers wrapping legacy values instead of using the modern value
// representation.
Expand Down
Loading
Loading