From 1c1991678b2fbaa902de9f9e96b2ed966d910397 Mon Sep 17 00:00:00 2001 From: Justin King Date: Wed, 23 Oct 2024 15:36:07 -0700 Subject: [PATCH] Migrate remaining well known type handling and remove deadcode PiperOrigin-RevId: 689126721 --- common/BUILD | 1 - common/legacy_value.cc | 10 +- eval/public/structs/BUILD | 14 +- eval/public/structs/cel_proto_wrap_util.cc | 558 ++++++++-------- extensions/protobuf/internal/BUILD | 254 ------- extensions/protobuf/internal/any.cc | 157 ----- extensions/protobuf/internal/any.h | 48 -- extensions/protobuf/internal/any_lite.cc | 38 -- extensions/protobuf/internal/any_lite.h | 47 -- extensions/protobuf/internal/any_test.cc | 88 --- extensions/protobuf/internal/duration.cc | 155 ----- extensions/protobuf/internal/duration.h | 36 - extensions/protobuf/internal/duration_lite.cc | 39 -- extensions/protobuf/internal/duration_lite.h | 36 - extensions/protobuf/internal/duration_test.cc | 94 --- extensions/protobuf/internal/enum.h | 27 - extensions/protobuf/internal/field_mask.cc | 76 --- extensions/protobuf/internal/field_mask.h | 31 - .../protobuf/internal/field_mask_lite.cc | 29 - .../protobuf/internal/field_mask_lite.h | 31 - .../protobuf/internal/field_mask_test.cc | 70 -- .../protobuf/internal/is_generated_message.h | 31 - .../protobuf/internal/is_message_lite.h | 32 - extensions/protobuf/internal/struct.cc | 621 ------------------ extensions/protobuf/internal/struct.h | 67 -- extensions/protobuf/internal/struct_lite.cc | 172 ----- extensions/protobuf/internal/struct_lite.h | 69 -- extensions/protobuf/internal/struct_test.cc | 181 ----- extensions/protobuf/internal/timestamp.cc | 157 ----- extensions/protobuf/internal/timestamp.h | 36 - .../protobuf/internal/timestamp_lite.cc | 42 -- extensions/protobuf/internal/timestamp_lite.h | 36 - .../protobuf/internal/timestamp_test.cc | 94 --- extensions/protobuf/internal/wrappers.cc | 287 -------- extensions/protobuf/internal/wrappers.h | 84 --- extensions/protobuf/internal/wrappers_lite.cc | 126 ---- extensions/protobuf/internal/wrappers_lite.h | 86 --- extensions/protobuf/internal/wrappers_test.cc | 442 ------------- internal/BUILD | 1 - internal/json.cc | 271 ++++++-- internal/json.h | 6 + internal/proto_util.h | 5 +- internal/status_builder.h | 46 +- internal/well_known_types.cc | 58 +- internal/well_known_types.h | 12 + 45 files changed, 612 insertions(+), 4189 deletions(-) delete mode 100644 extensions/protobuf/internal/any.cc delete mode 100644 extensions/protobuf/internal/any.h delete mode 100644 extensions/protobuf/internal/any_lite.cc delete mode 100644 extensions/protobuf/internal/any_lite.h delete mode 100644 extensions/protobuf/internal/any_test.cc delete mode 100644 extensions/protobuf/internal/duration.cc delete mode 100644 extensions/protobuf/internal/duration.h delete mode 100644 extensions/protobuf/internal/duration_lite.cc delete mode 100644 extensions/protobuf/internal/duration_lite.h delete mode 100644 extensions/protobuf/internal/duration_test.cc delete mode 100644 extensions/protobuf/internal/enum.h delete mode 100644 extensions/protobuf/internal/field_mask.cc delete mode 100644 extensions/protobuf/internal/field_mask.h delete mode 100644 extensions/protobuf/internal/field_mask_lite.cc delete mode 100644 extensions/protobuf/internal/field_mask_lite.h delete mode 100644 extensions/protobuf/internal/field_mask_test.cc delete mode 100644 extensions/protobuf/internal/is_generated_message.h delete mode 100644 extensions/protobuf/internal/is_message_lite.h delete mode 100644 extensions/protobuf/internal/struct.cc delete mode 100644 extensions/protobuf/internal/struct.h delete mode 100644 extensions/protobuf/internal/struct_lite.cc delete mode 100644 extensions/protobuf/internal/struct_lite.h delete mode 100644 extensions/protobuf/internal/struct_test.cc delete mode 100644 extensions/protobuf/internal/timestamp.cc delete mode 100644 extensions/protobuf/internal/timestamp.h delete mode 100644 extensions/protobuf/internal/timestamp_lite.cc delete mode 100644 extensions/protobuf/internal/timestamp_lite.h delete mode 100644 extensions/protobuf/internal/timestamp_test.cc delete mode 100644 extensions/protobuf/internal/wrappers.cc delete mode 100644 extensions/protobuf/internal/wrappers.h delete mode 100644 extensions/protobuf/internal/wrappers_lite.cc delete mode 100644 extensions/protobuf/internal/wrappers_lite.h delete mode 100644 extensions/protobuf/internal/wrappers_test.cc diff --git a/common/BUILD b/common/BUILD index e68fcc223..59898a4de 100644 --- a/common/BUILD +++ b/common/BUILD @@ -652,7 +652,6 @@ cc_library( "//extensions/protobuf:memory_manager", "//extensions/protobuf/internal:map_reflection", "//extensions/protobuf/internal:qualify", - "//extensions/protobuf/internal:struct_lite", "//internal:casts", "//internal:deserialize", "//internal:json", diff --git a/common/legacy_value.cc b/common/legacy_value.cc index a1c523b50..b1aa72bcb 100644 --- a/common/legacy_value.cc +++ b/common/legacy_value.cc @@ -58,8 +58,8 @@ #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 "extensions/protobuf/internal/struct_lite.h" #include "extensions/protobuf/memory_manager.h" +#include "internal/json.h" #include "internal/status_macros.h" #include "internal/time.h" #include "internal/well_known_types.h" @@ -268,9 +268,7 @@ absl::Status cel_common_internal_LegacyListValue_SerializeTo( google::protobuf::ListValue message; google::protobuf::Arena arena; CEL_ASSIGN_OR_RETURN(auto array, CelListToJsonArray(&arena, AsCelList(impl))); - CEL_RETURN_IF_ERROR( - extensions::protobuf_internal::GeneratedListValueProtoFromJson(array, - message)); + CEL_RETURN_IF_ERROR(internal::NativeJsonListToProtoJsonList(array, &message)); if (!message.SerializePartialToCord(&serialized_value)) { return absl::UnknownError("failed to serialize google.protobuf.ListValue"); } @@ -559,9 +557,7 @@ absl::Status cel_common_internal_LegacyMapValue_SerializeTo( google::protobuf::Struct message; google::protobuf::Arena arena; CEL_ASSIGN_OR_RETURN(auto object, CelMapToJsonObject(&arena, AsCelMap(impl))); - CEL_RETURN_IF_ERROR( - extensions::protobuf_internal::GeneratedStructProtoFromJson(object, - message)); + CEL_RETURN_IF_ERROR(internal::NativeJsonMapToProtoJsonMap(object, &message)); if (!message.SerializePartialToCord(&serialized_value)) { return absl::UnknownError("failed to serialize google.protobuf.Struct"); } diff --git a/eval/public/structs/BUILD b/eval/public/structs/BUILD index b1e658c77..2da148ef6 100644 --- a/eval/public/structs/BUILD +++ b/eval/public/structs/BUILD @@ -56,25 +56,25 @@ cc_library( ], deps = [ ":protobuf_value_factory", - "//common:any", "//eval/public:cel_value", "//eval/testutil:test_message_cc_proto", - "//extensions/protobuf/internal:any", - "//extensions/protobuf/internal:duration", - "//extensions/protobuf/internal:struct", - "//extensions/protobuf/internal:timestamp", - "//extensions/protobuf/internal:wrappers", "//internal:overflow", "//internal:proto_time_encoding", + "//internal:status_macros", "//internal:time", + "//internal:well_known_types", "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/base:nullability", + "@com_google_absl//absl/functional:overload", + "@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:cord", "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", "@com_google_absl//absl/types:optional", + "@com_google_absl//absl/types:variant", "@com_google_protobuf//:protobuf", ], ) diff --git a/eval/public/structs/cel_proto_wrap_util.cc b/eval/public/structs/cel_proto_wrap_util.cc index 40a8bd07d..4a740a22b 100644 --- a/eval/public/structs/cel_proto_wrap_util.cc +++ b/eval/public/structs/cel_proto_wrap_util.cc @@ -14,11 +14,9 @@ #include "eval/public/structs/cel_proto_wrap_util.h" -#include - +#include #include #include -#include #include #include #include @@ -29,31 +27,28 @@ #include "google/protobuf/struct.pb.h" #include "google/protobuf/timestamp.pb.h" #include "google/protobuf/wrappers.pb.h" -#include "google/protobuf/message.h" -#include "absl/base/attributes.h" +#include "absl/base/nullability.h" #include "absl/base/optimization.h" -#include "absl/container/flat_hash_map.h" +#include "absl/functional/overload.h" +#include "absl/log/absl_check.h" #include "absl/status/status.h" +#include "absl/status/statusor.h" #include "absl/strings/cord.h" #include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "absl/strings/substitute.h" #include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "absl/types/optional.h" -#include "common/any.h" +#include "absl/types/variant.h" #include "eval/public/cel_value.h" #include "eval/public/structs/protobuf_value_factory.h" #include "eval/testutil/test_message.pb.h" -#include "extensions/protobuf/internal/any.h" -#include "extensions/protobuf/internal/duration.h" -#include "extensions/protobuf/internal/struct.h" -#include "extensions/protobuf/internal/timestamp.h" -#include "extensions/protobuf/internal/wrappers.h" #include "internal/overflow.h" #include "internal/proto_time_encoding.h" +#include "internal/status_macros.h" #include "internal/time.h" +#include "internal/well_known_types.h" #include "google/protobuf/arena.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/message.h" @@ -65,7 +60,6 @@ namespace { using cel::internal::DecodeDuration; using cel::internal::DecodeTime; -using cel::internal::EncodeTime; using google::protobuf::Any; using google::protobuf::BoolValue; using google::protobuf::BytesValue; @@ -194,6 +188,29 @@ class DynamicMap : public CelMap { const DynamicMapKeyList key_list_; }; +// Adapter for usage with CEL_RETURN_IF_ERROR and CEL_ASSIGN_OR_RETURN. +class ReturnCelValueError { + public: + explicit ReturnCelValueError(absl::Nonnull arena) + : arena_(arena) {} + + CelValue operator()(const absl::Status& status) const { + ABSL_DCHECK(!status.ok()); + return CelValue::CreateError( + google::protobuf::Arena::Create(arena_, status)); + } + + private: + absl::Nonnull arena_; +}; + +struct IgnoreErrorAndReturnNullptr { + std::nullptr_t operator()(const absl::Status& status) const { + status.IgnoreError(); + return nullptr; + } +}; + // ValueManager provides ValueFromMessage(....) function family. // Functions of this family create CelValue object from specific subtypes of // protobuf message. @@ -221,13 +238,13 @@ class ValueManager { } CelValue ValueFromDuration(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicDurationProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromDuration(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetDurationReflection(message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + CEL_ASSIGN_OR_RETURN(auto duration, reflection.ToAbslDuration(*message), + _.With(ReturnCelValueError(arena_))); + return CelValue::CreateDuration(duration); } CelValue ValueFromMessage(const Duration* duration) { @@ -235,13 +252,13 @@ class ValueManager { } CelValue ValueFromTimestamp(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicTimestampProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromTimestamp(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetTimestampReflection(message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + CEL_ASSIGN_OR_RETURN(auto time, reflection.ToAbslTime(*message), + _.With(ReturnCelValueError(arena_))); + return CelValue::CreateTimestamp(time); } static CelValue ValueFromTimestamp(absl::Time timestamp) { @@ -263,27 +280,42 @@ class ValueManager { } CelValue ValueFromAny(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicAnyProto(*message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromAny(status_or_unwrapped->type_url(), - cel::GetAnyValueAsCord(*status_or_unwrapped), + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetAnyReflection(message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + std::string type_url_scratch; + std::string value_scratch; + return ValueFromAny(reflection.GetTypeUrl(*message, type_url_scratch), + reflection.GetValue(*message, value_scratch), descriptor_pool_, message_factory_); } - CelValue ValueFromAny(absl::string_view type_url, const absl::Cord& payload, + CelValue ValueFromAny(const cel::well_known_types::StringValue& type_url, + const cel::well_known_types::BytesValue& payload, const DescriptorPool* descriptor_pool, MessageFactory* message_factory) { - auto pos = type_url.find_last_of('/'); - if (pos == absl::string_view::npos) { + std::string type_url_string_scratch; + absl::string_view type_url_string = absl::visit( + absl::Overload([](absl::string_view string) + -> absl::string_view { return string; }, + [&type_url_string_scratch]( + const absl::Cord& cord) -> absl::string_view { + if (auto flat = cord.TryFlat(); flat) { + return *flat; + } + absl::CopyCordToString(cord, &type_url_string_scratch); + return absl::string_view(type_url_string_scratch); + }), + cel::well_known_types::AsVariant(type_url)); + auto pos = type_url_string.find_last_of('/'); + if (pos == type_url_string.npos) { // TODO What error code? // Malformed type_url return CreateErrorValue(arena_, "Malformed type_url string"); } - std::string full_name = std::string(type_url.substr(pos + 1)); + absl::string_view full_name = type_url_string.substr(pos + 1); const Descriptor* nested_descriptor = descriptor_pool->FindMessageTypeByName(full_name); @@ -301,7 +333,16 @@ class ValueManager { } Message* nested_message = prototype->New(arena_); - if (!nested_message->ParseFromCord(payload)) { + bool ok = + absl::visit(absl::Overload( + [nested_message](absl::string_view string) -> bool { + return nested_message->ParsePartialFromString(string); + }, + [nested_message](const absl::Cord& cord) -> bool { + return nested_message->ParsePartialFromCord(cord); + }), + cel::well_known_types::AsVariant(payload)); + if (!ok) { // Failed to unpack. // TODO What error code? return CreateErrorValue(arena_, "Failed to unpack Any into message"); @@ -322,13 +363,11 @@ class ValueManager { } CelValue ValueFromBool(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicBoolValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromBool(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetBoolValueReflection(message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromBool(reflection.GetValue(*message)); } static CelValue ValueFromBool(bool value) { @@ -340,13 +379,11 @@ class ValueManager { } CelValue ValueFromInt32(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicInt32ValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromInt32(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetInt32ValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromInt32(reflection.GetValue(*message)); } static CelValue ValueFromInt32(int32_t value) { @@ -358,13 +395,11 @@ class ValueManager { } CelValue ValueFromUInt32(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicUInt32ValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromUInt32(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetUInt32ValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromUInt32(reflection.GetValue(*message)); } static CelValue ValueFromUInt32(uint32_t value) { @@ -376,13 +411,11 @@ class ValueManager { } CelValue ValueFromInt64(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicInt64ValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromInt64(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetInt64ValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromInt64(reflection.GetValue(*message)); } static CelValue ValueFromInt64(int64_t value) { @@ -394,13 +427,11 @@ class ValueManager { } CelValue ValueFromUInt64(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicUInt64ValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromUInt64(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetUInt64ValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromUInt64(reflection.GetValue(*message)); } static CelValue ValueFromUInt64(uint64_t value) { @@ -412,13 +443,11 @@ class ValueManager { } CelValue ValueFromFloat(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicFloatValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromFloat(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetFloatValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromFloat(reflection.GetValue(*message)); } static CelValue ValueFromFloat(float value) { @@ -430,13 +459,11 @@ class ValueManager { } CelValue ValueFromDouble(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicDoubleValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromDouble(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetDoubleValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + return ValueFromDouble(reflection.GetValue(*message)); } static CelValue ValueFromDouble(double value) { @@ -448,13 +475,30 @@ class ValueManager { } CelValue ValueFromString(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicStringValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromString(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetStringValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + std::string scratch; + return absl::visit( + absl::Overload( + [&](absl::string_view string) -> CelValue { + if (string.data() == scratch.data() && + string.size() == scratch.size()) { + return CelValue::CreateString( + google::protobuf::Arena::Create(arena_, + std::move(scratch))); + } + return CelValue::CreateString(google::protobuf::Arena::Create( + arena_, std::string(string))); + }, + [&](absl::Cord&& cord) -> CelValue { + auto* string = google::protobuf::Arena::Create(arena_); + absl::CopyCordToString(cord, string); + return CelValue::CreateString(string); + }), + cel::well_known_types::AsVariant( + reflection.GetValue(*message, scratch))); } CelValue ValueFromString(const absl::Cord& value) { @@ -471,13 +515,29 @@ class ValueManager { } CelValue ValueFromBytes(const google::protobuf::Message* message) { - auto status_or_unwrapped = - cel::extensions::protobuf_internal::UnwrapDynamicBytesValueProto( - *message); - if (!status_or_unwrapped.ok()) { - return CreateErrorValue(arena_, status_or_unwrapped.status()); - } - return ValueFromBytes(*status_or_unwrapped); + CEL_ASSIGN_OR_RETURN(auto reflection, + cel::well_known_types::GetBytesValueReflection( + message->GetDescriptor()), + _.With(ReturnCelValueError(arena_))); + std::string scratch; + return absl::visit( + absl::Overload( + [&](absl::string_view string) -> CelValue { + if (string.data() == scratch.data() && + string.size() == scratch.size()) { + return CelValue::CreateBytes(google::protobuf::Arena::Create( + arena_, std::move(scratch))); + } + return CelValue::CreateBytes(google::protobuf::Arena::Create( + arena_, std::string(string))); + }, + [&](absl::Cord&& cord) -> CelValue { + auto* string = google::protobuf::Arena::Create(arena_); + absl::CopyCordToString(cord, string); + return CelValue::CreateBytes(string); + }), + cel::well_known_types::AsVariant( + reflection.GetValue(*message, scratch))); } CelValue ValueFromBytes(const absl::Cord& value) { @@ -678,13 +738,12 @@ google::protobuf::Message* DurationFromValue(const google::protobuf::Message* pr return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicDurationProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetDurationReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + CEL_RETURN_IF_ERROR(reflection.SetFromAbslDuration(message, val)) + .With(IgnoreErrorAndReturnNullptr()); return message; } @@ -695,13 +754,11 @@ google::protobuf::Message* BoolFromValue(const google::protobuf::Message* protot return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicBoolValueProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetBoolValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, val); return message; } @@ -712,13 +769,11 @@ google::protobuf::Message* BytesFromValue(const google::protobuf::Message* proto return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicBytesValueProto( - absl::Cord(view_val.value()), *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetBytesValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, view_val.value()); return message; } @@ -729,13 +784,11 @@ google::protobuf::Message* DoubleFromValue(const google::protobuf::Message* prot return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicDoubleValueProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetDoubleValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, val); return message; } @@ -753,13 +806,11 @@ google::protobuf::Message* FloatFromValue(const google::protobuf::Message* proto fval = -std::numeric_limits::infinity(); } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicFloatValueProto(fval, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetFloatValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, static_cast(fval)); return message; } @@ -774,13 +825,11 @@ google::protobuf::Message* Int32FromValue(const google::protobuf::Message* proto } int32_t ival = static_cast(val); auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicInt32ValueProto(ival, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetInt32ValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, ival); return message; } @@ -791,13 +840,11 @@ google::protobuf::Message* Int64FromValue(const google::protobuf::Message* proto return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicInt64ValueProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetInt64ValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, val); return message; } @@ -808,13 +855,11 @@ google::protobuf::Message* StringFromValue(const google::protobuf::Message* prot return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicStringValueProto( - absl::Cord(view_val.value()), *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetStringValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, view_val.value()); return message; } @@ -829,13 +874,12 @@ google::protobuf::Message* TimestampFromValue(const google::protobuf::Message* p return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicTimestampProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetTimestampReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + CEL_RETURN_IF_ERROR(reflection.SetFromAbslTime(message, val)) + .With(IgnoreErrorAndReturnNullptr()); return message; } @@ -850,13 +894,11 @@ google::protobuf::Message* UInt32FromValue(const google::protobuf::Message* prot } uint32_t ival = static_cast(val); auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicUInt32ValueProto(ival, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetUInt32ValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, ival); return message; } @@ -867,13 +909,11 @@ google::protobuf::Message* UInt64FromValue(const google::protobuf::Message* prot return nullptr; } auto* message = prototype->New(arena); - auto status_or_wrapped = - cel::extensions::protobuf_internal::WrapDynamicUInt64ValueProto(val, - *message); - if (!status_or_wrapped.ok()) { - status_or_wrapped.IgnoreError(); - return nullptr; - } + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetUInt64ValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetValue(message, val); return message; } @@ -891,15 +931,14 @@ google::protobuf::Message* ListFromValue(google::protobuf::Message* message, con return nullptr; } const CelList& list = *value.ListOrDie(); + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetListValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); for (int i = 0; i < list.size(); i++) { auto e = list.Get(arena, i); - auto status_or_elem = - cel::extensions::protobuf_internal::DynamicListValueProtoAddElement( - message); - if (!status_or_elem.ok()) { - return nullptr; - } - if (ValueFromValue(*status_or_elem, e, arena) == nullptr) { + auto* elem = reflection.AddValues(message); + if (ValueFromValue(elem, e, arena) == nullptr) { return nullptr; } } @@ -928,6 +967,10 @@ google::protobuf::Message* StructFromValue(google::protobuf::Message* message, return nullptr; } const CelList& keys = **keys_or; + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetStructReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); for (int i = 0; i < keys.size(); i++) { auto k = keys.Get(arena, i); // If the key is not a string type, abort the conversion. @@ -940,13 +983,8 @@ google::protobuf::Message* StructFromValue(google::protobuf::Message* message, if (!v.has_value()) { return nullptr; } - auto status_or_value = - cel::extensions::protobuf_internal::DynamicStructValueProtoAddField( - key, message); - if (!status_or_value.ok()) { - return nullptr; - } - if (ValueFromValue(*status_or_value, *v, arena) == nullptr) { + auto* field = reflection.InsertField(message, key); + if (ValueFromValue(field, *v, arena) == nullptr) { return nullptr; } } @@ -963,52 +1001,42 @@ google::protobuf::Message* StructFromValue(const google::protobuf::Message* prot google::protobuf::Message* ValueFromValue(google::protobuf::Message* message, const CelValue& value, google::protobuf::Arena* arena) { + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetValueReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); switch (value.type()) { case CelValue::Type::kBool: { bool val; if (value.GetValue(&val)) { - if (cel::extensions::protobuf_internal::DynamicValueProtoSetBoolValue( - val, message) - .ok()) { - return message; - } + reflection.SetBoolValue(message, val); + return message; } } break; case CelValue::Type::kBytes: { - // Base64 encode byte strings to ensure they can safely be transpored + // Base64 encode byte strings to ensure they can safely be transported // in a JSON string. CelValue::BytesHolder val; if (value.GetValue(&val)) { - if (cel::extensions::protobuf_internal::DynamicValueProtoSetStringValue( - absl::Base64Escape(val.value()), message) - .ok()) { - return message; - } + reflection.SetStringValueFromBytes(message, val.value()); + return message; } } break; case CelValue::Type::kDouble: { double val; if (value.GetValue(&val)) { - if (cel::extensions::protobuf_internal::DynamicValueProtoSetNumberValue( - val, message) - .ok()) { - return message; - } + reflection.SetNumberValue(message, val); + return message; } } break; case CelValue::Type::kDuration: { // Convert duration values to a protobuf JSON format. absl::Duration val; if (value.GetValue(&val)) { - auto encode = cel::internal::EncodeDurationToString(val); - if (!encode.ok()) { - return nullptr; - } - if (cel::extensions::protobuf_internal::DynamicValueProtoSetStringValue( - *encode, message) - .ok()) { - return message; - } + CEL_RETURN_IF_ERROR(cel::internal::ValidateDuration(val)) + .With(IgnoreErrorAndReturnNullptr()); + reflection.SetStringValueFromDuration(message, val); + return message; } } break; case CelValue::Type::kInt64: { @@ -1016,45 +1044,25 @@ google::protobuf::Message* ValueFromValue(google::protobuf::Message* message, co // Convert int64_t values within the int53 range to doubles, otherwise // serialize the value to a string. if (value.GetValue(&val)) { - if (IsJSONSafe(val)) { - if (cel::extensions::protobuf_internal:: - DynamicValueProtoSetNumberValue(static_cast(val), - message) - .ok()) { - return message; - } - } else { - if (cel::extensions::protobuf_internal:: - DynamicValueProtoSetStringValue(absl::StrCat(val), message) - .ok()) { - return message; - } - } + reflection.SetNumberValue(message, val); + return message; } } break; case CelValue::Type::kString: { CelValue::StringHolder val; if (value.GetValue(&val)) { - if (cel::extensions::protobuf_internal::DynamicValueProtoSetStringValue( - val.value(), message) - .ok()) { - return message; - } + reflection.SetStringValue(message, val.value()); + return message; } } break; case CelValue::Type::kTimestamp: { // Convert timestamp values to a protobuf JSON format. absl::Time val; if (value.GetValue(&val)) { - auto encode = cel::internal::EncodeTimeToString(val); - if (!encode.ok()) { - return nullptr; - } - if (cel::extensions::protobuf_internal::DynamicValueProtoSetStringValue( - *encode, message) - .ok()) { - return message; - } + CEL_RETURN_IF_ERROR(cel::internal::ValidateTimestamp(val)) + .With(IgnoreErrorAndReturnNullptr()); + reflection.SetStringValueFromTimestamp(message, val); + return message; } } break; case CelValue::Type::kUint64: { @@ -1062,49 +1070,25 @@ google::protobuf::Message* ValueFromValue(google::protobuf::Message* message, co // Convert uint64_t values within the int53 range to doubles, otherwise // serialize the value to a string. if (value.GetValue(&val)) { - if (IsJSONSafe(val)) { - if (cel::extensions::protobuf_internal:: - DynamicValueProtoSetNumberValue(static_cast(val), - message) - .ok()) { - return message; - } - } else { - if (cel::extensions::protobuf_internal:: - DynamicValueProtoSetStringValue(absl::StrCat(val), message) - .ok()) { - return message; - } - } + reflection.SetNumberValue(message, val); + return message; } } break; case CelValue::Type::kList: { - auto status_or_list = - cel::extensions::protobuf_internal::DynamicValueProtoMutableListValue( - message); - if (!status_or_list.ok()) { - return nullptr; - } - if (ListFromValue(*status_or_list, value, arena) != nullptr) { + if (ListFromValue(reflection.MutableListValue(message), value, arena) != + nullptr) { return message; } } break; case CelValue::Type::kMap: { - auto status_or_struct = cel::extensions::protobuf_internal:: - DynamicValueProtoMutableStructValue(message); - if (!status_or_struct.ok()) { - return nullptr; - } - if (StructFromValue(*status_or_struct, value, arena) != nullptr) { + if (StructFromValue(reflection.MutableStructValue(message), value, + arena) != nullptr) { return message; } } break; case CelValue::Type::kNullType: - if (cel::extensions::protobuf_internal::DynamicValueProtoSetNullValue( - message) - .ok()) { - return message; - } + reflection.SetNullValue(message); + return message; break; default: return nullptr; @@ -1176,7 +1160,7 @@ bool ValueFromValue(Value* json, const CelValue& value, google::protobuf::Arena* } } break; case CelValue::Type::kBytes: { - // Base64 encode byte strings to ensure they can safely be transpored + // Base64 encode byte strings to ensure they can safely be transported // in a JSON string. CelValue::BytesHolder val; if (value.GetValue(&val)) { @@ -1351,12 +1335,14 @@ google::protobuf::Message* AnyFromValue(const google::protobuf::Message* prototy } auto* message = prototype->New(arena); - if (cel::extensions::protobuf_internal::WrapDynamicAnyProto( - absl::StrCat("type.googleapis.com/", type_name), payload, *message) - .ok()) { - return message; - } - return nullptr; + CEL_ASSIGN_OR_RETURN( + auto reflection, + cel::well_known_types::GetAnyReflection(message->GetDescriptor()), + _.With(IgnoreErrorAndReturnNullptr())); + reflection.SetTypeUrl(message, + absl::StrCat("type.googleapis.com/", type_name)); + reflection.SetValue(message, payload); + return message; } bool IsAlreadyWrapped(google::protobuf::Descriptor::WellKnownType wkt, diff --git a/extensions/protobuf/internal/BUILD b/extensions/protobuf/internal/BUILD index 59a25592c..b9e560074 100644 --- a/extensions/protobuf/internal/BUILD +++ b/extensions/protobuf/internal/BUILD @@ -19,54 +19,6 @@ package( licenses(["notice"]) -cc_library( - name = "any_lite", - srcs = ["any_lite.cc"], - hdrs = ["any_lite.h"], - deps = [ - "//common:any", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings:cord", - "@com_google_absl//absl/strings:string_view", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "any", - srcs = ["any.cc"], - hdrs = ["any.h"], - deps = [ - ":any_lite", - ":is_generated_message", - ":is_message_lite", - "//common:any", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:cord", - "@com_google_absl//absl/strings:string_view", - "@com_google_protobuf//:protobuf", - ], -) - -cc_test( - name = "any_test", - srcs = ["any_test.cc"], - deps = [ - ":any", - ":any_lite", - "//common:any", - "//internal:testing", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/strings:cord", - "@com_google_protobuf//:protobuf", - ], -) - cc_library( name = "ast", srcs = ["ast.cc"], @@ -120,98 +72,6 @@ cc_library( ], ) -cc_library( - name = "duration_lite", - srcs = ["duration_lite.cc"], - hdrs = ["duration_lite.h"], - deps = [ - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "duration", - srcs = ["duration.cc"], - hdrs = ["duration.h"], - deps = [ - ":duration_lite", - ":is_generated_message", - ":is_message_lite", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - -cc_test( - name = "duration_test", - srcs = ["duration_test.cc"], - deps = [ - ":duration", - ":duration_lite", - "//internal:testing", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "enum", - hdrs = ["enum.h"], - deps = ["@com_google_protobuf//:protobuf"], -) - -cc_library( - name = "timestamp_lite", - srcs = ["timestamp_lite.cc"], - hdrs = ["timestamp_lite.h"], - deps = [ - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "timestamp", - srcs = ["timestamp.cc"], - hdrs = ["timestamp.h"], - deps = [ - ":is_generated_message", - ":is_message_lite", - ":timestamp_lite", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - -cc_test( - name = "timestamp_test", - srcs = ["timestamp_test.cc"], - deps = [ - ":timestamp", - ":timestamp_lite", - "//internal:testing", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/time", - "@com_google_protobuf//:protobuf", - ], -) - cc_library( name = "map_reflection", srcs = ["map_reflection.cc"], @@ -223,108 +83,6 @@ cc_library( ], ) -cc_library( - name = "wrappers_lite", - srcs = ["wrappers_lite.cc"], - hdrs = ["wrappers_lite.h"], - deps = [ - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings:cord", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "wrappers", - srcs = ["wrappers.cc"], - hdrs = ["wrappers.h"], - deps = [ - ":is_generated_message", - ":is_message_lite", - ":wrappers_lite", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/functional:function_ref", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:cord", - "@com_google_protobuf//:protobuf", - ], -) - -cc_test( - name = "wrappers_test", - srcs = ["wrappers_test.cc"], - deps = [ - ":wrappers", - ":wrappers_lite", - "//internal:testing", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/status", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "struct_lite", - srcs = ["struct_lite.cc"], - hdrs = ["struct_lite.h"], - deps = [ - "//common:json", - "//internal:status_macros", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/functional:overload", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:cord", - "@com_google_absl//absl/types:variant", - "@com_google_protobuf//:protobuf", - ], -) - -cc_library( - name = "struct", - srcs = ["struct.cc"], - hdrs = ["struct.h"], - deps = [ - ":is_generated_message", - ":is_message_lite", - ":map_reflection", - ":struct_lite", - "//common:json", - "//internal:status_macros", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/base:nullability", - "@com_google_absl//absl/functional:overload", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/status", - "@com_google_absl//absl/status:statusor", - "@com_google_absl//absl/strings", - "@com_google_absl//absl/strings:cord", - "@com_google_absl//absl/types:variant", - "@com_google_protobuf//:protobuf", - ], -) - -cc_test( - name = "struct_test", - srcs = ["struct_test.cc"], - deps = [ - ":struct", - ":struct_lite", - "//common:json", - "//internal:testing", - "//testutil:util", - "@com_google_absl//absl/log:absl_check", - "@com_google_absl//absl/memory", - "@com_google_protobuf//:protobuf", - ], -) - cc_library( name = "qualify", srcs = ["qualify.cc"], @@ -349,15 +107,3 @@ cc_library( "@com_google_protobuf//:protobuf", ], ) - -cc_library( - name = "is_message_lite", - hdrs = ["is_message_lite.h"], - deps = ["@com_google_protobuf//:protobuf"], -) - -cc_library( - name = "is_generated_message", - hdrs = ["is_generated_message.h"], - deps = ["@com_google_protobuf//:protobuf"], -) diff --git a/extensions/protobuf/internal/any.cc b/extensions/protobuf/internal/any.cc deleted file mode 100644 index 6175d220b..000000000 --- a/extensions/protobuf/internal/any.cc +++ /dev/null @@ -1,157 +0,0 @@ -// 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 "extensions/protobuf/internal/any.h" - -#include - -#include "google/protobuf/any.pb.h" -#include "absl/base/optimization.h" -#include "absl/log/absl_check.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" -#include "common/any.h" -#include "extensions/protobuf/internal/any_lite.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicAnyProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Any"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return UnwrapGeneratedAnyProto( - google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* type_url_field = - desc->FindFieldByNumber(google::protobuf::Any::kTypeUrlFieldNumber); - if (ABSL_PREDICT_FALSE(type_url_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing type_url field descriptor")); - } - if (ABSL_PREDICT_FALSE(type_url_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected type_url field type: ", - type_url_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(type_url_field->is_map() || - type_url_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - type_url_field->name(), " field cardinality: REPEATED")); - } - const auto* value_field = - desc->FindFieldByNumber(google::protobuf::Any::kValueFieldNumber); - if (ABSL_PREDICT_FALSE(value_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing value field descriptor")); - } - if (ABSL_PREDICT_FALSE(value_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected value field type: ", value_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(value_field->is_map() || value_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - value_field->name(), " field cardinality: REPEATED")); - } - return MakeAny(reflect->GetString(message, type_url_field), - reflect->GetCord(message, value_field)); -} - -absl::Status WrapDynamicAnyProto(absl::string_view type_url, - const absl::Cord& value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Any"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return WrapGeneratedAnyProto( - type_url, value, - google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* type_url_field = - desc->FindFieldByNumber(google::protobuf::Any::kTypeUrlFieldNumber); - if (ABSL_PREDICT_FALSE(type_url_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing type_url field descriptor")); - } - if (ABSL_PREDICT_FALSE(type_url_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected type_url field type: ", - type_url_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(type_url_field->is_map() || - type_url_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - type_url_field->name(), " field cardinality: REPEATED")); - } - const auto* value_field = - desc->FindFieldByNumber(google::protobuf::Any::kValueFieldNumber); - if (ABSL_PREDICT_FALSE(value_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing value field descriptor")); - } - if (ABSL_PREDICT_FALSE(value_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected value field type: ", value_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(value_field->is_map() || value_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - value_field->name(), " field cardinality: REPEATED")); - } - reflect->SetString(&message, type_url_field, std::string(type_url)); - reflect->SetString(&message, value_field, value); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/any.h b/extensions/protobuf/internal/any.h deleted file mode 100644 index 4dbbc0b88..000000000 --- a/extensions/protobuf/internal/any.h +++ /dev/null @@ -1,48 +0,0 @@ -// 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/any.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ - -#include "google/protobuf/any.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/string_view.h" -#include "common/any.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -// Converts `google.protobuf.Any` to `Any`. No validation is performed. The type -// name of `message` must be `google.protobuf.Any`. -absl::StatusOr UnwrapDynamicAnyProto( - const google::protobuf::Message& message); - -// Converts `Any` to `google.protobuf.Any`. No validation is performed. The type -// name of `message` must be `google.protobuf.Any`. -absl::Status WrapDynamicAnyProto(absl::string_view type_url, - const absl::Cord& value, - google::protobuf::Message& message); -inline absl::Status WrapDynamicAnyProto(const google::protobuf::Any& any, - google::protobuf::Message& message) { - return WrapDynamicAnyProto(any.type_url(), GetAnyValueAsCord(any), message); -} - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_H_ diff --git a/extensions/protobuf/internal/any_lite.cc b/extensions/protobuf/internal/any_lite.cc deleted file mode 100644 index 5311808df..000000000 --- a/extensions/protobuf/internal/any_lite.cc +++ /dev/null @@ -1,38 +0,0 @@ -// 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 "extensions/protobuf/internal/any_lite.h" - -#include "google/protobuf/any.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/string_view.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedAnyProto( - const google::protobuf::Any& message) { - return message; -} - -absl::Status WrapGeneratedAnyProto(absl::string_view type_url, - const absl::Cord& value, - google::protobuf::Any& message) { - message.set_type_url(type_url); - message.set_value(static_cast(value)); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/any_lite.h b/extensions/protobuf/internal/any_lite.h deleted file mode 100644 index e088cf413..000000000 --- a/extensions/protobuf/internal/any_lite.h +++ /dev/null @@ -1,47 +0,0 @@ -// 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/any.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_LITE_H_ - -#include "google/protobuf/any.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/string_view.h" -#include "common/any.h" - -namespace cel::extensions::protobuf_internal { - -// Converts `google.protobuf.Any` to `Any`. No validation is performed. The type -// name of `message` must be `google.protobuf.Any`. -absl::StatusOr UnwrapGeneratedAnyProto( - const google::protobuf::Any& message); - -// Converts `Any` to `google.protobuf.Any`. No validation is performed. The type -// name of `message` must be `google.protobuf.Any`. -absl::Status WrapGeneratedAnyProto(absl::string_view type_url, - const absl::Cord& value, - google::protobuf::Any& message); -inline absl::Status WrapGeneratedAnyProto(const google::protobuf::Any& any, - google::protobuf::Any& message) { - return WrapGeneratedAnyProto(any.type_url(), GetAnyValueAsCord(any), message); -} - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ANY_LITE_H_ diff --git a/extensions/protobuf/internal/any_test.cc b/extensions/protobuf/internal/any_test.cc deleted file mode 100644 index c43042196..000000000 --- a/extensions/protobuf/internal/any_test.cc +++ /dev/null @@ -1,88 +0,0 @@ -// 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 "extensions/protobuf/internal/any.h" - -#include - -#include "google/protobuf/any.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/memory/memory.h" -#include "absl/strings/cord.h" -#include "extensions/protobuf/internal/any_lite.h" -#include "internal/testing.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -TEST(Any, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Any::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - ASSERT_OK_AND_ASSIGN(auto unwrapped, - UnwrapDynamicAnyProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.Any")))); - EXPECT_EQ(unwrapped.type_url(), ""); - EXPECT_EQ(unwrapped.value(), ""); -} - -TEST(Any, GeneratedToProto) { - google::protobuf::Any proto; - ASSERT_OK(WrapGeneratedAnyProto("type.googleapis.com/foo.Bar", - absl::Cord("blah"), proto)); - EXPECT_EQ(proto.type_url(), "type.googleapis.com/foo.Bar"); - EXPECT_EQ(proto.value(), "blah"); -} - -TEST(Any, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Any::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory.GetPrototype(pool.FindMessageTypeByName("google.protobuf.Any")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* type_url_field = descriptor->FindFieldByName("type_url"); - ASSERT_NE(type_url_field, nullptr); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicAnyProto("type.googleapis.com/foo.Bar", - absl::Cord("blah"), *proto)); - - EXPECT_EQ(reflection->GetString(*proto, type_url_field), - "type.googleapis.com/foo.Bar"); - EXPECT_EQ(reflection->GetString(*proto, value_field), "blah"); -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/duration.cc b/extensions/protobuf/internal/duration.cc deleted file mode 100644 index 546014b3f..000000000 --- a/extensions/protobuf/internal/duration.cc +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/duration.h" - -#include - -#include "google/protobuf/duration.pb.h" -#include "absl/base/optimization.h" -#include "absl/log/absl_check.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/str_cat.h" -#include "absl/time/time.h" -#include "extensions/protobuf/internal/duration_lite.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicDurationProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Duration"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return UnwrapGeneratedDurationProto( - google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* seconds_field = - desc->FindFieldByNumber(google::protobuf::Duration::kSecondsFieldNumber); - if (ABSL_PREDICT_FALSE(seconds_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing seconds field descriptor")); - } - if (ABSL_PREDICT_FALSE(seconds_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT64)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected seconds field type: ", - seconds_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(seconds_field->is_map() || - seconds_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - seconds_field->name(), " field cardinality: REPEATED")); - } - const auto* nanos_field = - desc->FindFieldByNumber(google::protobuf::Duration::kNanosFieldNumber); - if (ABSL_PREDICT_FALSE(nanos_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing nanos field descriptor")); - } - if (ABSL_PREDICT_FALSE(nanos_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT32)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected nanos field type: ", nanos_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(nanos_field->is_map() || nanos_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - nanos_field->name(), " field cardinality: REPEATED")); - } - return absl::Seconds(reflect->GetInt64(message, seconds_field)) + - absl::Nanoseconds(reflect->GetInt32(message, nanos_field)); -} - -absl::Status WrapDynamicDurationProto(absl::Duration value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Duration"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return WrapGeneratedDurationProto( - value, google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* seconds_field = - desc->FindFieldByNumber(google::protobuf::Duration::kSecondsFieldNumber); - if (ABSL_PREDICT_FALSE(seconds_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing seconds field descriptor")); - } - if (ABSL_PREDICT_FALSE(seconds_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT64)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected seconds field type: ", - seconds_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(seconds_field->is_map() || - seconds_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - seconds_field->name(), " field cardinality: REPEATED")); - } - const auto* nanos_field = - desc->FindFieldByNumber(google::protobuf::Duration::kNanosFieldNumber); - if (ABSL_PREDICT_FALSE(nanos_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing nanos field descriptor")); - } - if (ABSL_PREDICT_FALSE(nanos_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT32)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected nanos field type: ", nanos_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(nanos_field->is_map() || nanos_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - nanos_field->name(), " field cardinality: REPEATED")); - } - reflect->SetInt64(&message, seconds_field, - absl::IDivDuration(value, absl::Seconds(1), &value)); - reflect->SetInt32(&message, nanos_field, - static_cast(absl::IDivDuration( - value, absl::Nanoseconds(1), &value))); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/duration.h b/extensions/protobuf/internal/duration.h deleted file mode 100644 index 4fc7f183d..000000000 --- a/extensions/protobuf/internal/duration.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/duration.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_H_ - -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicDurationProto( - const google::protobuf::Message& message); - -absl::Status WrapDynamicDurationProto(absl::Duration value, - google::protobuf::Message& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_H_ diff --git a/extensions/protobuf/internal/duration_lite.cc b/extensions/protobuf/internal/duration_lite.cc deleted file mode 100644 index 543127a84..000000000 --- a/extensions/protobuf/internal/duration_lite.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/duration_lite.h" - -#include - -#include "google/protobuf/duration.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedDurationProto( - const google::protobuf::Duration& message) { - return absl::Seconds(message.seconds()) + absl::Nanoseconds(message.nanos()); -} - -absl::Status WrapGeneratedDurationProto(absl::Duration value, - google::protobuf::Duration& message) { - message.set_seconds(absl::IDivDuration(value, absl::Seconds(1), &value)); - message.set_nanos(static_cast( - absl::IDivDuration(value, absl::Nanoseconds(1), &value))); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/duration_lite.h b/extensions/protobuf/internal/duration_lite.h deleted file mode 100644 index 967c2530d..000000000 --- a/extensions/protobuf/internal/duration_lite.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/duration.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_LITE_H_ - -#include "google/protobuf/duration.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedDurationProto( - const google::protobuf::Duration& message); - -absl::Status WrapGeneratedDurationProto(absl::Duration value, - google::protobuf::Duration& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_DURATION_LITE_H_ diff --git a/extensions/protobuf/internal/duration_test.cc b/extensions/protobuf/internal/duration_test.cc deleted file mode 100644 index 8dfd47979..000000000 --- a/extensions/protobuf/internal/duration_test.cc +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/duration.h" - -#include - -#include "google/protobuf/duration.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/memory/memory.h" -#include "absl/time/time.h" -#include "extensions/protobuf/internal/duration_lite.h" -#include "internal/testing.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -using ::absl_testing::IsOkAndHolds; -using ::testing::Eq; - -TEST(Duration, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedDurationProto(google::protobuf::Duration()), - IsOkAndHolds(Eq(absl::ZeroDuration()))); -} - -TEST(Duration, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Duration::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicDurationProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.Duration"))), - IsOkAndHolds(Eq(absl::ZeroDuration()))); -} - -TEST(Duration, GeneratedToProto) { - google::protobuf::Duration proto; - ASSERT_OK(WrapGeneratedDurationProto(absl::Seconds(1) + absl::Nanoseconds(2), - proto)); - EXPECT_EQ(proto.seconds(), 1); - EXPECT_EQ(proto.nanos(), 2); -} - -TEST(Duration, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Duration::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory - .GetPrototype(pool.FindMessageTypeByName("google.protobuf.Duration")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* seconds_field = descriptor->FindFieldByName("seconds"); - ASSERT_NE(seconds_field, nullptr); - const auto* nanos_field = descriptor->FindFieldByName("nanos"); - ASSERT_NE(nanos_field, nullptr); - - ASSERT_OK(WrapDynamicDurationProto(absl::Seconds(1) + absl::Nanoseconds(2), - *proto)); - - EXPECT_EQ(reflection->GetInt64(*proto, seconds_field), 1); - EXPECT_EQ(reflection->GetInt32(*proto, nanos_field), 2); -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/enum.h b/extensions/protobuf/internal/enum.h deleted file mode 100644 index b5f1c36a2..000000000 --- a/extensions/protobuf/internal/enum.h +++ /dev/null @@ -1,27 +0,0 @@ -// 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_EXTENSIONS_PROTOBUF_INTERNAL_ENUM_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ENUM_H_ - -#include "google/protobuf/generated_enum_util.h" - -namespace cel::extensions::protobuf_internal { - -template -inline constexpr bool IsProtoEnum = google::protobuf::is_proto_enum::value; - -} - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_ENUM_H_ diff --git a/extensions/protobuf/internal/field_mask.cc b/extensions/protobuf/internal/field_mask.cc deleted file mode 100644 index 08b16578a..000000000 --- a/extensions/protobuf/internal/field_mask.cc +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/field_mask.h" - -#include - -#include "google/protobuf/field_mask.pb.h" -#include "absl/base/optimization.h" -#include "absl/log/absl_check.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/str_join.h" -#include "common/json.h" -#include "extensions/protobuf/internal/field_mask_lite.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" -#include "google/protobuf/reflection.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr DynamicFieldMaskProtoToJsonString( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.FieldMask"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedFieldMaskProtoToJsonString( - google::protobuf::DownCastMessage(message)); - } - } - const auto* reflection = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflection == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* paths_field = - desc->FindFieldByNumber(google::protobuf::FieldMask::kPathsFieldNumber); - if (ABSL_PREDICT_FALSE(paths_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing paths field descriptor")); - } - if (ABSL_PREDICT_FALSE(paths_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_STRING)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected paths field type: ", paths_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(!paths_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), - " has unexpected paths field cardinality: UNKNOWN")); - } - return JsonString(absl::StrJoin( - reflection->GetRepeatedFieldRef(message, paths_field), ",")); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/field_mask.h b/extensions/protobuf/internal/field_mask.h deleted file mode 100644 index b75f22ae6..000000000 --- a/extensions/protobuf/internal/field_mask.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2023 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_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_H_ - -#include "absl/status/statusor.h" -#include "common/json.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -// Formats `google.protobuf.FieldMask` according to -// https://protobuf.dev/programming-guides/proto3/#json. -absl::StatusOr DynamicFieldMaskProtoToJsonString( - const google::protobuf::Message& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_H_ diff --git a/extensions/protobuf/internal/field_mask_lite.cc b/extensions/protobuf/internal/field_mask_lite.cc deleted file mode 100644 index 093db9fbd..000000000 --- a/extensions/protobuf/internal/field_mask_lite.cc +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/field_mask_lite.h" - -#include "google/protobuf/field_mask.pb.h" -#include "absl/status/statusor.h" -#include "absl/strings/str_join.h" -#include "common/json.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr GeneratedFieldMaskProtoToJsonString( - const google::protobuf::FieldMask& message) { - return JsonString(absl::StrJoin(message.paths(), ",")); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/field_mask_lite.h b/extensions/protobuf/internal/field_mask_lite.h deleted file mode 100644 index 4606f471d..000000000 --- a/extensions/protobuf/internal/field_mask_lite.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2023 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_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_LITE_H_ - -#include "google/protobuf/field_mask.pb.h" -#include "absl/status/statusor.h" -#include "common/json.h" - -namespace cel::extensions::protobuf_internal { - -// Formats `google.protobuf.FieldMask` according to -// https://protobuf.dev/programming-guides/proto3/#json. -absl::StatusOr GeneratedFieldMaskProtoToJsonString( - const google::protobuf::FieldMask& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_FIELD_MASK_LITE_H_ diff --git a/extensions/protobuf/internal/field_mask_test.cc b/extensions/protobuf/internal/field_mask_test.cc deleted file mode 100644 index 8e1f5d349..000000000 --- a/extensions/protobuf/internal/field_mask_test.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/field_mask.h" - -#include - -#include "google/protobuf/field_mask.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/memory/memory.h" -#include "extensions/protobuf/internal/field_mask_lite.h" -#include "internal/testing.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -using ::absl_testing::IsOkAndHolds; -using ::testing::Eq; - -TEST(FieldMask, GeneratedFromProto) { - google::protobuf::FieldMask proto; - proto.add_paths("foo"); - proto.add_paths("bar"); - EXPECT_THAT(GeneratedFieldMaskProtoToJsonString(proto), - IsOkAndHolds(Eq(JsonString("foo,bar")))); -} - -TEST(Any, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::FieldMask::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory - .GetPrototype(pool.FindMessageTypeByName("google.protobuf.FieldMask")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* paths_field = descriptor->FindFieldByName("paths"); - ASSERT_NE(paths_field, nullptr); - - reflection->AddString(proto.get(), paths_field, "foo"); - reflection->AddString(proto.get(), paths_field, "bar"); - - EXPECT_THAT(DynamicFieldMaskProtoToJsonString(*proto), - IsOkAndHolds(Eq(JsonString("foo,bar")))); -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/is_generated_message.h b/extensions/protobuf/internal/is_generated_message.h deleted file mode 100644 index 1f403fa96..000000000 --- a/extensions/protobuf/internal/is_generated_message.h +++ /dev/null @@ -1,31 +0,0 @@ -// 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_EXTENSIONS_PROTOBUF_INTERNAL_IS_GENERATED_MESSAGE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_IS_GENERATED_MESSAGE_H_ - -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -inline bool IsGeneratedMessage(const google::protobuf::Message& message) { - const auto* reflection = message.GetReflection(); - return reflection != nullptr && - reflection->GetMessageFactory() == - google::protobuf::MessageFactory::generated_factory(); -} - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_IS_GENERATED_MESSAGE_H_ diff --git a/extensions/protobuf/internal/is_message_lite.h b/extensions/protobuf/internal/is_message_lite.h deleted file mode 100644 index 98905371b..000000000 --- a/extensions/protobuf/internal/is_message_lite.h +++ /dev/null @@ -1,32 +0,0 @@ -// 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_EXTENSIONS_PROTOBUF_INTERNAL_IS_MESSAGE_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_IS_MESSAGE_LITE_H_ - -#include - -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -template -inline constexpr bool NotMessageLite = std::is_base_of_v; - -template -inline constexpr bool IsMessageLite = !NotMessageLite; - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_IS_MESSAGE_LITE_H_ diff --git a/extensions/protobuf/internal/struct.cc b/extensions/protobuf/internal/struct.cc deleted file mode 100644 index 689677952..000000000 --- a/extensions/protobuf/internal/struct.cc +++ /dev/null @@ -1,621 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/struct.h" - -#include -#include -#include - -#include "google/protobuf/struct.pb.h" -#include "absl/base/nullability.h" -#include "absl/base/optimization.h" -#include "absl/functional/overload.h" -#include "absl/log/absl_check.h" -#include "absl/memory/memory.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" -#include "absl/types/variant.h" -#include "common/json.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "extensions/protobuf/internal/map_reflection.h" -#include "extensions/protobuf/internal/struct_lite.h" -#include "internal/status_macros.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/map_field.h" -#include "google/protobuf/message.h" -#include "google/protobuf/reflection.h" - -namespace cel::extensions::protobuf_internal { - -namespace { - -template -std::enable_if_t, google::protobuf::Message*> UpCastMessage( - T* message) { - return message; -} - -template -std::enable_if_t, google::protobuf::Message*> UpCastMessage(T* message); - -// Gets the `Descriptor` for `message`, verifying that it is not `nullptr`. -absl::StatusOr> GetDescriptor( - const google::protobuf::Message& message) { - const auto* descriptor = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(descriptor == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - return descriptor; -} - -// Gets the `Reflection` for `message`, verifying that it is not `nullptr`. -absl::StatusOr> GetReflection( - const google::protobuf::Message& message) { - const auto* reflection = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflection == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - return reflection; -} - -// Gets the `FieldDescriptor` for `number` in `message`, verifying that it is -// not `nullptr`. -absl::StatusOr> FindFieldByNumber( - absl::Nonnull descriptor, int number) { - const auto* field = descriptor->FindFieldByNumber(number); - if (ABSL_PREDICT_FALSE(field == nullptr)) { - return absl::InternalError( - absl::StrCat(descriptor->full_name(), - " missing descriptor for field number: ", number)); - } - return field; -} - -// Gets the `OneofDescriptor` for `name` in `message`, verifying that it is -// not `nullptr`. -absl::StatusOr> FindOneofByName( - absl::Nonnull descriptor, - absl::string_view name) { - const auto* oneof = descriptor->FindOneofByName(name); - if (ABSL_PREDICT_FALSE(oneof == nullptr)) { - return absl::InternalError(absl::StrCat( - descriptor->full_name(), " missing descriptor for oneof: ", name)); - } - return oneof; -} - -absl::Status CheckFieldType(absl::Nonnull field, - google::protobuf::FieldDescriptor::CppType type) { - if (ABSL_PREDICT_FALSE(field->cpp_type() != type)) { - return absl::InternalError(absl::StrCat( - field->full_name(), " has unexpected type: ", field->cpp_type_name())); - } - return absl::OkStatus(); -} - -absl::Status CheckFieldSingular( - absl::Nonnull field) { - if (ABSL_PREDICT_FALSE(field->is_repeated() || field->is_map())) { - return absl::InternalError(absl::StrCat( - field->full_name(), " has unexpected cardinality: REPEATED")); - } - return absl::OkStatus(); -} - -absl::Status CheckFieldRepeated( - absl::Nonnull field) { - if (ABSL_PREDICT_FALSE(!field->is_repeated() && !field->is_map())) { - return absl::InternalError(absl::StrCat( - field->full_name(), " has unexpected cardinality: SINGULAR")); - } - return absl::OkStatus(); -} - -absl::Status CheckFieldMap( - absl::Nonnull field) { - if (ABSL_PREDICT_FALSE(!field->is_map())) { - if (field->is_repeated()) { - return absl::InternalError( - absl::StrCat(field->full_name(), - " has unexpected type: ", field->cpp_type_name())); - } else { - return absl::InternalError(absl::StrCat( - field->full_name(), " has unexpected cardinality: SINGULAR")); - } - } - return absl::OkStatus(); -} - -absl::Status CheckFieldEnumType( - absl::Nonnull field, - absl::string_view name) { - CEL_RETURN_IF_ERROR( - CheckFieldType(field, google::protobuf::FieldDescriptor::CPPTYPE_ENUM)); - if (ABSL_PREDICT_FALSE(field->enum_type()->full_name() != name)) { - return absl::InternalError(absl::StrCat( - field->full_name(), - " has unexpected type: ", field->enum_type()->full_name())); - } - return absl::OkStatus(); -} - -absl::Status CheckFieldMessageType( - absl::Nonnull field, - absl::string_view name) { - CEL_RETURN_IF_ERROR( - CheckFieldType(field, google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)); - if (ABSL_PREDICT_FALSE(field->message_type()->full_name() != name)) { - return absl::InternalError(absl::StrCat( - field->full_name(), - " has unexpected type: ", field->message_type()->full_name())); - } - return absl::OkStatus(); -} - -} // namespace - -absl::StatusOr DynamicValueProtoToJson(const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedValueProtoToJson( - google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - CEL_ASSIGN_OR_RETURN(const auto* kind_desc, FindOneofByName(desc, "kind")); - const auto* value_desc = - reflection->GetOneofFieldDescriptor(message, kind_desc); - if (value_desc == nullptr) { - return kJsonNull; - } - switch (value_desc->number()) { - case google::protobuf::Value::kNullValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldEnumType(value_desc, "google.protobuf.NullValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return kJsonNull; - case google::protobuf::Value::kNumberValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldType(value_desc, google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return reflection->GetDouble(message, value_desc); - case google::protobuf::Value::kStringValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldType(value_desc, google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return reflection->GetCord(message, value_desc); - case google::protobuf::Value::kBoolValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldType(value_desc, google::protobuf::FieldDescriptor::CPPTYPE_BOOL)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return reflection->GetBool(message, value_desc); - case google::protobuf::Value::kStructValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(value_desc, "google.protobuf.Struct")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return DynamicStructProtoToJson( - reflection->GetMessage(message, value_desc)); - case google::protobuf::Value::kListValueFieldNumber: - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(value_desc, "google.protobuf.ListValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(value_desc)); - return DynamicListValueProtoToJson( - reflection->GetMessage(message, value_desc)); - default: - return absl::InternalError( - absl::StrCat(value_desc->full_name(), - " has unexpected number: ", value_desc->number())); - } -} - -absl::StatusOr DynamicListValueProtoToJson( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.ListValue"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedListValueProtoToJson( - google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - CEL_ASSIGN_OR_RETURN( - const auto* values_field, - FindFieldByNumber(desc, google::protobuf::ListValue::kValuesFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(values_field, "google.protobuf.Value")); - CEL_RETURN_IF_ERROR(CheckFieldRepeated(values_field)); - const auto& repeated_field_ref = - reflection->GetRepeatedFieldRef(message, values_field); - JsonArrayBuilder builder; - builder.reserve(repeated_field_ref.size()); - for (const auto& element : repeated_field_ref) { - CEL_ASSIGN_OR_RETURN(auto value, DynamicValueProtoToJson(element)); - builder.push_back(std::move(value)); - } - return std::move(builder).Build(); -} - -absl::StatusOr DynamicStructProtoToJson(const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Struct"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedStructProtoToJson( - google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - CEL_ASSIGN_OR_RETURN( - const auto* fields_field, - FindFieldByNumber(desc, google::protobuf::Struct::kFieldsFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldMap(fields_field)); - CEL_RETURN_IF_ERROR(CheckFieldType(fields_field->message_type()->map_key(), - google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldMessageType( - fields_field->message_type()->map_value(), "google.protobuf.Value")); - auto map_begin = - protobuf_internal::MapBegin(*reflection, message, *fields_field); - auto map_end = protobuf_internal::MapEnd(*reflection, message, *fields_field); - JsonObjectBuilder builder; - builder.reserve( - protobuf_internal::MapSize(*reflection, message, *fields_field)); - for (; map_begin != map_end; ++map_begin) { - CEL_ASSIGN_OR_RETURN( - auto value, - DynamicValueProtoToJson(map_begin.GetValueRef().GetMessageValue())); - builder.insert_or_assign(absl::Cord(map_begin.GetKey().GetStringValue()), - std::move(value)); - } - return std::move(builder).Build(); -} - -absl::Status DynamicValueProtoFromJson(const Json& json, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedValueProtoFromJson( - json, google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - return absl::visit( - absl::Overload( - [&message, &desc, &reflection](JsonNull) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* null_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kNullValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldEnumType( - null_value_field, "google.protobuf.NullValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(null_value_field)); - reflection->SetEnumValue(&message, null_value_field, 0); - return absl::OkStatus(); - }, - [&message, &desc, &reflection](JsonBool value) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* bool_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kBoolValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldType( - bool_value_field, google::protobuf::FieldDescriptor::CPPTYPE_BOOL)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(bool_value_field)); - reflection->SetBool(&message, bool_value_field, value); - return absl::OkStatus(); - }, - [&message, &desc, &reflection](JsonNumber value) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* number_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kNumberValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldType( - number_value_field, google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(number_value_field)); - reflection->SetDouble(&message, number_value_field, value); - return absl::OkStatus(); - }, - [&message, &desc, - &reflection](const JsonString& value) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* string_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kStringValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldType( - string_value_field, google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(string_value_field)); - reflection->SetString(&message, string_value_field, - static_cast(value)); - return absl::OkStatus(); - }, - [&message, &desc, - &reflection](const JsonArray& value) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* list_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kListValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldMessageType( - list_value_field, "google.protobuf.ListValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(list_value_field)); - return DynamicListValueProtoFromJson( - value, *reflection->MutableMessage(&message, list_value_field)); - }, - [&message, &desc, - &reflection](const JsonObject& value) -> absl::Status { - CEL_ASSIGN_OR_RETURN( - const auto* struct_value_field, - FindFieldByNumber( - desc, google::protobuf::Value::kStructValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldMessageType( - struct_value_field, "google.protobuf.Struct")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(struct_value_field)); - return DynamicStructProtoFromJson( - value, - *reflection->MutableMessage(&message, struct_value_field)); - }), - json); -} - -absl::Status DynamicListValueProtoFromJson(const JsonArray& json, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.ListValue"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedListValueProtoFromJson( - json, google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - CEL_ASSIGN_OR_RETURN( - const auto* values_field, - FindFieldByNumber(desc, google::protobuf::ListValue::kValuesFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(values_field, "google.protobuf.Value")); - CEL_RETURN_IF_ERROR(CheckFieldRepeated(values_field)); - auto repeated_field_ref = - reflection->GetMutableRepeatedFieldRef(&message, - values_field); - repeated_field_ref.Clear(); - for (const auto& element : json) { - auto scratch = absl::WrapUnique(repeated_field_ref.NewMessage()); - CEL_RETURN_IF_ERROR(DynamicValueProtoFromJson(element, *scratch)); - repeated_field_ref.Add(*scratch); - } - return absl::OkStatus(); -} - -absl::Status DynamicStructProtoFromJson(const JsonObject& json, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Struct"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return GeneratedStructProtoFromJson( - json, google::protobuf::DownCastMessage(message)); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(message)); - CEL_ASSIGN_OR_RETURN( - const auto* fields_field, - FindFieldByNumber(desc, google::protobuf::Struct::kFieldsFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldMap(fields_field)); - CEL_RETURN_IF_ERROR(CheckFieldType(fields_field->message_type()->map_key(), - google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldMessageType( - fields_field->message_type()->map_value(), "google.protobuf.Value")); - for (const auto& entry : json) { - std::string map_key_string = static_cast(entry.first); - google::protobuf::MapKey map_key; - map_key.SetStringValue(map_key_string); - google::protobuf::MapValueRef map_value; - protobuf_internal::InsertOrLookupMapValue( - *reflection, &message, *fields_field, map_key, &map_value); - CEL_RETURN_IF_ERROR(DynamicValueProtoFromJson( - entry.second, *map_value.MutableMessageValue())); - } - return absl::OkStatus(); -} - -absl::Status DynamicValueProtoSetNullValue(google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - GeneratedValueProtoSetNullValue( - google::protobuf::DownCastMessage(message)); - return absl::OkStatus(); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* null_value_field, - FindFieldByNumber(desc, google::protobuf::Value::kNullValueFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldEnumType(null_value_field, "google.protobuf.NullValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(null_value_field)); - reflection->SetEnumValue(message, null_value_field, 0); - return absl::OkStatus(); -} - -absl::Status DynamicValueProtoSetBoolValue(bool value, - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - GeneratedValueProtoSetBoolValue( - value, google::protobuf::DownCastMessage(message)); - return absl::OkStatus(); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* bool_value_field, - FindFieldByNumber(desc, google::protobuf::Value::kBoolValueFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldType(bool_value_field, google::protobuf::FieldDescriptor::CPPTYPE_BOOL)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(bool_value_field)); - reflection->SetBool(message, bool_value_field, value); - return absl::OkStatus(); -} - -absl::Status DynamicValueProtoSetNumberValue(double value, - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - GeneratedValueProtoSetNumberValue( - value, google::protobuf::DownCastMessage(message)); - return absl::OkStatus(); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* number_value_field, - FindFieldByNumber(desc, - google::protobuf::Value::kNumberValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldType(number_value_field, - google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(number_value_field)); - reflection->SetDouble(message, number_value_field, value); - return absl::OkStatus(); -} - -absl::Status DynamicValueProtoSetStringValue(absl::string_view value, - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - GeneratedValueProtoSetStringValue( - value, google::protobuf::DownCastMessage(message)); - return absl::OkStatus(); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* string_value_field, - FindFieldByNumber(desc, - google::protobuf::Value::kStringValueFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldType(string_value_field, - google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldSingular(string_value_field)); - reflection->SetString(message, string_value_field, - static_cast(value)); - return absl::OkStatus(); -} - -absl::StatusOr DynamicValueProtoMutableListValue( - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite && - NotMessageLite) { - if (IsGeneratedMessage(*message)) { - return UpCastMessage(GeneratedValueProtoMutableListValue( - google::protobuf::DownCastMessage(message))); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* list_value_field, - FindFieldByNumber(desc, google::protobuf::Value::kListValueFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(list_value_field, "google.protobuf.ListValue")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(list_value_field)); - return reflection->MutableMessage(message, list_value_field); -} - -absl::StatusOr DynamicValueProtoMutableStructValue( - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Value"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite && - NotMessageLite) { - if (IsGeneratedMessage(*message)) { - return UpCastMessage(GeneratedValueProtoMutableStructValue( - google::protobuf::DownCastMessage(message))); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* struct_value_field, - FindFieldByNumber(desc, - google::protobuf::Value::kStructValueFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(struct_value_field, "google.protobuf.Struct")); - CEL_RETURN_IF_ERROR(CheckFieldSingular(struct_value_field)); - return reflection->MutableMessage(message, struct_value_field); -} - -absl::StatusOr DynamicListValueProtoAddElement( - google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.ListValue"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - return UpCastMessage(GeneratedListValueProtoAddElement( - google::protobuf::DownCastMessage(message))); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* element_field, - FindFieldByNumber(desc, google::protobuf::ListValue::kValuesFieldNumber)); - CEL_RETURN_IF_ERROR( - CheckFieldMessageType(element_field, "google.protobuf.Value")); - CEL_RETURN_IF_ERROR(CheckFieldRepeated(element_field)); - return reflection->AddMessage(message, element_field); -} - -absl::StatusOr DynamicStructValueProtoAddField( - absl::string_view name, google::protobuf::Message* message) { - ABSL_DCHECK_EQ(message->GetTypeName(), "google.protobuf.Struct"); - CEL_ASSIGN_OR_RETURN(const auto* desc, GetDescriptor(*message)); - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(*message)) { - return UpCastMessage(GeneratedStructValueProtoAddField( - name, google::protobuf::DownCastMessage(message))); - } - } - CEL_ASSIGN_OR_RETURN(const auto* reflection, GetReflection(*message)); - CEL_ASSIGN_OR_RETURN( - const auto* map_field, - FindFieldByNumber(desc, google::protobuf::Struct::kFieldsFieldNumber)); - CEL_RETURN_IF_ERROR(CheckFieldMap(map_field)); - CEL_RETURN_IF_ERROR(CheckFieldType(map_field->message_type()->map_key(), - google::protobuf::FieldDescriptor::CPPTYPE_STRING)); - CEL_RETURN_IF_ERROR(CheckFieldMessageType( - map_field->message_type()->map_value(), "google.protobuf.Value")); - std::string key_string = std::string(name); - google::protobuf::MapKey key; - key.SetStringValue(key_string); - google::protobuf::MapValueRef value; - InsertOrLookupMapValue(*reflection, message, *map_field, key, &value); - return value.MutableMessageValue(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/struct.h b/extensions/protobuf/internal/struct.h deleted file mode 100644 index 54d160a6a..000000000 --- a/extensions/protobuf/internal/struct.h +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2023 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_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_H_ - -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" -#include "common/json.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr DynamicValueProtoToJson(const google::protobuf::Message& message); - -absl::StatusOr DynamicListValueProtoToJson( - const google::protobuf::Message& message); - -absl::StatusOr DynamicStructProtoToJson(const google::protobuf::Message& message); - -absl::Status DynamicValueProtoFromJson(const Json& json, - google::protobuf::Message& message); - -absl::Status DynamicListValueProtoFromJson(const JsonArray& json, - google::protobuf::Message& message); - -absl::Status DynamicStructProtoFromJson(const JsonObject& json, - google::protobuf::Message& message); - -absl::Status DynamicValueProtoSetNullValue(google::protobuf::Message* message); - -absl::Status DynamicValueProtoSetBoolValue(bool value, - google::protobuf::Message* message); - -absl::Status DynamicValueProtoSetNumberValue(double value, - google::protobuf::Message* message); - -absl::Status DynamicValueProtoSetStringValue(absl::string_view value, - google::protobuf::Message* message); - -absl::StatusOr DynamicValueProtoMutableListValue( - google::protobuf::Message* message); - -absl::StatusOr DynamicValueProtoMutableStructValue( - google::protobuf::Message* message); - -absl::StatusOr DynamicListValueProtoAddElement( - google::protobuf::Message* message); - -absl::StatusOr DynamicStructValueProtoAddField( - absl::string_view name, google::protobuf::Message* message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_H_ diff --git a/extensions/protobuf/internal/struct_lite.cc b/extensions/protobuf/internal/struct_lite.cc deleted file mode 100644 index db6a81790..000000000 --- a/extensions/protobuf/internal/struct_lite.cc +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/struct_lite.h" - -#include -#include - -#include "google/protobuf/struct.pb.h" -#include "absl/base/attributes.h" -#include "absl/functional/overload.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/str_cat.h" -#include "absl/strings/string_view.h" -#include "absl/types/variant.h" -#include "common/json.h" -#include "internal/status_macros.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr GeneratedValueProtoToJson( - const google::protobuf::Value& message) { - switch (message.kind_case()) { - case google::protobuf::Value::KIND_NOT_SET: - ABSL_FALLTHROUGH_INTENDED; - case google::protobuf::Value::kNullValue: - return kJsonNull; - case google::protobuf::Value::kBoolValue: - return message.bool_value(); - case google::protobuf::Value::kNumberValue: - return message.number_value(); - case google::protobuf::Value::kStringValue: - return absl::Cord(message.string_value()); - case google::protobuf::Value::kStructValue: - return GeneratedStructProtoToJson(message.struct_value()); - case google::protobuf::Value::kListValue: - return GeneratedListValueProtoToJson(message.list_value()); - default: - return absl::InternalError( - absl::StrCat("unexpected google.protobuf.Value oneof kind: ", - message.kind_case())); - } -} - -absl::StatusOr GeneratedListValueProtoToJson( - const google::protobuf::ListValue& message) { - JsonArrayBuilder builder; - builder.reserve(message.values_size()); - for (const auto& element : message.values()) { - CEL_ASSIGN_OR_RETURN(auto value, GeneratedValueProtoToJson(element)); - builder.push_back(std::move(value)); - } - return std::move(builder).Build(); -} - -absl::StatusOr GeneratedStructProtoToJson( - const google::protobuf::Struct& message) { - JsonObjectBuilder builder; - builder.reserve(message.fields_size()); - for (const auto& field : message.fields()) { - CEL_ASSIGN_OR_RETURN(auto value, GeneratedValueProtoToJson(field.second)); - builder.insert_or_assign(absl::Cord(field.first), std::move(value)); - } - return std::move(builder).Build(); -} - -absl::Status GeneratedValueProtoFromJson(const Json& json, - google::protobuf::Value& message) { - return absl::visit( - absl::Overload( - [&message](JsonNull) { - message.set_null_value(google::protobuf::NULL_VALUE); - return absl::OkStatus(); - }, - [&message](JsonBool value) { - message.set_bool_value(value); - return absl::OkStatus(); - }, - [&message](JsonNumber value) { - message.set_number_value(value); - return absl::OkStatus(); - }, - [&message](const JsonString& value) { - message.set_string_value(static_cast(value)); - return absl::OkStatus(); - }, - [&message](const JsonArray& value) { - return GeneratedListValueProtoFromJson( - value, *message.mutable_list_value()); - }, - [&message](const JsonObject& value) { - return GeneratedStructProtoFromJson( - value, *message.mutable_struct_value()); - }), - json); -} - -absl::Status GeneratedListValueProtoFromJson( - const JsonArray& json, google::protobuf::ListValue& message) { - auto* elements = message.mutable_values(); - elements->Clear(); - elements->Reserve(static_cast(json.size())); - for (const auto& element : json) { - CEL_RETURN_IF_ERROR(GeneratedValueProtoFromJson(element, *elements->Add())); - } - return absl::OkStatus(); -} - -absl::Status GeneratedStructProtoFromJson(const JsonObject& json, - google::protobuf::Struct& message) { - auto* fields = message.mutable_fields(); - fields->clear(); - for (const auto& field : json) { - CEL_RETURN_IF_ERROR(GeneratedValueProtoFromJson( - field.second, (*fields)[static_cast(field.first)])); - } - return absl::OkStatus(); -} - -void GeneratedValueProtoSetNullValue(google::protobuf::Value* message) { - message->set_null_value(google::protobuf::NULL_VALUE); -} - -void GeneratedValueProtoSetBoolValue(bool value, - google::protobuf::Value* message) { - message->set_bool_value(value); -} - -void GeneratedValueProtoSetNumberValue(double value, - google::protobuf::Value* message) { - message->set_number_value(value); -} - -void GeneratedValueProtoSetStringValue(absl::string_view value, - google::protobuf::Value* message) { - message->set_string_value(std::string(value)); -} - -google::protobuf::ListValue* GeneratedValueProtoMutableListValue( - google::protobuf::Value* message) { - return message->mutable_list_value(); -} - -google::protobuf::Struct* GeneratedValueProtoMutableStructValue( - google::protobuf::Value* message) { - return message->mutable_struct_value(); -} - -google::protobuf::Value* GeneratedListValueProtoAddElement( - google::protobuf::ListValue* message) { - return message->add_values(); -} - -google::protobuf::Value* GeneratedStructValueProtoAddField( - absl::string_view name, google::protobuf::Struct* message) { - return &(*message->mutable_fields())[std::string(name)]; -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/struct_lite.h b/extensions/protobuf/internal/struct_lite.h deleted file mode 100644 index 449df9623..000000000 --- a/extensions/protobuf/internal/struct_lite.h +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2023 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_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_LITE_H_ - -#include "google/protobuf/struct.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/string_view.h" -#include "common/json.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr GeneratedValueProtoToJson( - const google::protobuf::Value& message); - -absl::StatusOr GeneratedListValueProtoToJson( - const google::protobuf::ListValue& message); - -absl::StatusOr GeneratedStructProtoToJson( - const google::protobuf::Struct& message); - -absl::Status GeneratedValueProtoFromJson(const Json& json, - google::protobuf::Value& message); - -absl::Status GeneratedListValueProtoFromJson( - const JsonArray& json, google::protobuf::ListValue& message); - -absl::Status GeneratedStructProtoFromJson(const JsonObject& json, - google::protobuf::Struct& message); - -void GeneratedValueProtoSetNullValue(google::protobuf::Value* message); - -void GeneratedValueProtoSetBoolValue(bool value, - google::protobuf::Value* message); - -void GeneratedValueProtoSetNumberValue(double value, - google::protobuf::Value* message); - -void GeneratedValueProtoSetStringValue(absl::string_view value, - google::protobuf::Value* message); - -google::protobuf::ListValue* GeneratedValueProtoMutableListValue( - google::protobuf::Value* message); - -google::protobuf::Struct* GeneratedValueProtoMutableStructValue( - google::protobuf::Value* message); - -google::protobuf::Value* GeneratedListValueProtoAddElement( - google::protobuf::ListValue* message); - -google::protobuf::Value* GeneratedStructValueProtoAddField( - absl::string_view name, google::protobuf::Struct* message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_STRUCT_LITE_H_ diff --git a/extensions/protobuf/internal/struct_test.cc b/extensions/protobuf/internal/struct_test.cc deleted file mode 100644 index a015df299..000000000 --- a/extensions/protobuf/internal/struct_test.cc +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/struct.h" - -#include - -#include "google/protobuf/struct.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/log/absl_check.h" -#include "absl/memory/memory.h" -#include "common/json.h" -#include "extensions/protobuf/internal/struct_lite.h" -#include "internal/testing.h" -#include "testutil/util.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" -#include "google/protobuf/text_format.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -using ::absl_testing::IsOkAndHolds; -using ::google::api::expr::testutil::EqualsProto; -using ::testing::IsEmpty; -using ::testing::VariantWith; - -template -T ParseTextOrDie(absl::string_view text) { - T proto; - ABSL_CHECK(google::protobuf::TextFormat::ParseFromString(text, &proto)); - return proto; -} - -std::unique_ptr ParseTextOrDie( - absl::string_view text, const google::protobuf::Message& prototype) { - auto message = absl::WrapUnique(prototype.New()); - ABSL_CHECK(google::protobuf::TextFormat::ParseFromString(text, message.get())); - return message; -} - -TEST(Value, Generated) { - google::protobuf::Value proto; - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(kJsonNull))); - proto.set_null_value(google::protobuf::NULL_VALUE); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(kJsonNull))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(null_value: 0)pb"))); - - proto.set_bool_value(true); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(true))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(true), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(bool_value: true)pb"))); - - proto.set_number_value(1.0); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(1.0))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(1.0), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(number_value: 1.0)pb"))); - - proto.set_string_value("foo"); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(JsonString("foo")))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(JsonString("foo")), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(string_value: "foo")pb"))); - - proto.mutable_list_value(); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(IsEmpty()))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(JsonArray()), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(list_value: {})pb"))); - - proto.mutable_struct_value(); - EXPECT_THAT(GeneratedValueProtoToJson(proto), - IsOkAndHolds(VariantWith(IsEmpty()))); - proto.Clear(); - EXPECT_OK(GeneratedValueProtoFromJson(Json(JsonObject()), proto)); - EXPECT_THAT(proto, EqualsProto(ParseTextOrDie( - R"pb(struct_value: {})pb"))); -} - -TEST(Value, Dynamic) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Value::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory.GetPrototype(pool.FindMessageTypeByName("google.protobuf.Value")) - ->New()); - const auto* reflection = proto->GetReflection(); - const auto* descriptor = proto->GetDescriptor(); - - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(kJsonNull))); - reflection->SetEnumValue(proto.get(), - descriptor->FindFieldByName("null_value"), 0); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(kJsonNull))); - proto->Clear(); - EXPECT_OK(DynamicValueProtoFromJson(Json(), *proto)); - EXPECT_THAT(*proto, - EqualsProto(*ParseTextOrDie(R"pb(null_value: 0)pb", *proto))); - - reflection->SetBool(proto.get(), descriptor->FindFieldByName("bool_value"), - true); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(true))); - proto->Clear(); - EXPECT_OK(DynamicValueProtoFromJson(Json(true), *proto)); - EXPECT_THAT(*proto, - EqualsProto(*ParseTextOrDie(R"pb(bool_value: true)pb", *proto))); - - reflection->SetDouble(proto.get(), - descriptor->FindFieldByName("number_value"), 1.0); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(1.0))); - proto->Clear(); - EXPECT_OK(DynamicValueProtoFromJson(Json(1.0), *proto)); - EXPECT_THAT(*proto, - EqualsProto(*ParseTextOrDie(R"pb(number_value: 1.0)pb", *proto))); - - reflection->SetString(proto.get(), - descriptor->FindFieldByName("string_value"), "foo"); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(JsonString("foo")))); - proto->Clear(); - EXPECT_OK(DynamicValueProtoFromJson(Json(JsonString("foo")), *proto)); - EXPECT_THAT(*proto, EqualsProto(*ParseTextOrDie(R"pb(string_value: "foo")pb", - *proto))); - - reflection->MutableMessage( - proto.get(), descriptor->FindFieldByName("list_value"), &factory); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(IsEmpty()))); - proto->Clear(); - EXPECT_OK(DynamicValueProtoFromJson(Json(JsonArray()), *proto)); - EXPECT_THAT(*proto, - EqualsProto(*ParseTextOrDie(R"pb(list_value: {})pb", *proto))); - - reflection->MutableMessage( - proto.get(), descriptor->FindFieldByName("struct_value"), &factory); - EXPECT_THAT(DynamicValueProtoToJson(*proto), - IsOkAndHolds(VariantWith(IsEmpty()))); - EXPECT_OK(DynamicValueProtoFromJson(Json(JsonObject()), *proto)); - EXPECT_THAT(*proto, - EqualsProto(*ParseTextOrDie(R"pb(struct_value: {})pb", *proto))); -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/timestamp.cc b/extensions/protobuf/internal/timestamp.cc deleted file mode 100644 index 9fa984a96..000000000 --- a/extensions/protobuf/internal/timestamp.cc +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/timestamp.h" - -#include - -#include "google/protobuf/timestamp.pb.h" -#include "absl/base/optimization.h" -#include "absl/log/absl_check.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/str_cat.h" -#include "absl/time/time.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "extensions/protobuf/internal/timestamp_lite.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicTimestampProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Timestamp"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return UnwrapGeneratedTimestampProto( - google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* seconds_field = - desc->FindFieldByNumber(google::protobuf::Timestamp::kSecondsFieldNumber); - if (ABSL_PREDICT_FALSE(seconds_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing seconds field descriptor")); - } - if (ABSL_PREDICT_FALSE(seconds_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT64)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected seconds field type: ", - seconds_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(seconds_field->is_map() || - seconds_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - seconds_field->name(), " field cardinality: REPEATED")); - } - const auto* nanos_field = - desc->FindFieldByNumber(google::protobuf::Timestamp::kNanosFieldNumber); - if (ABSL_PREDICT_FALSE(nanos_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing nanos field descriptor")); - } - if (ABSL_PREDICT_FALSE(nanos_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT32)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected nanos field type: ", nanos_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(nanos_field->is_map() || nanos_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - nanos_field->name(), " field cardinality: REPEATED")); - } - return absl::UnixEpoch() + - absl::Seconds(reflect->GetInt64(message, seconds_field)) + - absl::Nanoseconds(reflect->GetInt32(message, nanos_field)); -} - -absl::Status WrapDynamicTimestampProto(absl::Time value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Timestamp"); - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - return WrapGeneratedTimestampProto( - value, google::protobuf::DownCastMessage(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* seconds_field = - desc->FindFieldByNumber(google::protobuf::Timestamp::kSecondsFieldNumber); - if (ABSL_PREDICT_FALSE(seconds_field == nullptr)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " missing seconds field descriptor")); - } - if (ABSL_PREDICT_FALSE(seconds_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT64)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), " has unexpected seconds field type: ", - seconds_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(seconds_field->is_map() || - seconds_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - seconds_field->name(), " field cardinality: REPEATED")); - } - const auto* nanos_field = - desc->FindFieldByNumber(google::protobuf::Timestamp::kNanosFieldNumber); - if (ABSL_PREDICT_FALSE(nanos_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing nanos field descriptor")); - } - if (ABSL_PREDICT_FALSE(nanos_field->cpp_type() != - google::protobuf::FieldDescriptor::CPPTYPE_INT32)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected nanos field type: ", nanos_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(nanos_field->is_map() || nanos_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " has unexpected ", - nanos_field->name(), " field cardinality: REPEATED")); - } - auto duration = value - absl::UnixEpoch(); - reflect->SetInt64(&message, seconds_field, - absl::IDivDuration(duration, absl::Seconds(1), &duration)); - reflect->SetInt32(&message, nanos_field, - static_cast(absl::IDivDuration( - duration, absl::Nanoseconds(1), &duration))); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/timestamp.h b/extensions/protobuf/internal/timestamp.h deleted file mode 100644 index 2d622bcd6..000000000 --- a/extensions/protobuf/internal/timestamp.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/timestamp.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_H_ - -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicTimestampProto( - const google::protobuf::Message& message); - -absl::Status WrapDynamicTimestampProto(absl::Time value, - google::protobuf::Message& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_H_ diff --git a/extensions/protobuf/internal/timestamp_lite.cc b/extensions/protobuf/internal/timestamp_lite.cc deleted file mode 100644 index 335fbe390..000000000 --- a/extensions/protobuf/internal/timestamp_lite.cc +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/timestamp_lite.h" - -#include - -#include "google/protobuf/timestamp.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedTimestampProto( - const google::protobuf::Timestamp& message) { - return absl::UnixEpoch() + absl::Seconds(message.seconds()) + - absl::Nanoseconds(message.nanos()); -} - -absl::Status WrapGeneratedTimestampProto(absl::Time value, - google::protobuf::Timestamp& message) { - auto duration = value - absl::UnixEpoch(); - message.set_seconds( - absl::IDivDuration(duration, absl::Seconds(1), &duration)); - message.set_nanos(static_cast( - absl::IDivDuration(duration, absl::Nanoseconds(1), &duration))); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/timestamp_lite.h b/extensions/protobuf/internal/timestamp_lite.h deleted file mode 100644 index 0ea92e517..000000000 --- a/extensions/protobuf/internal/timestamp_lite.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/timestamp.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_LITE_H_ - -#include "google/protobuf/timestamp.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/time/time.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedTimestampProto( - const google::protobuf::Timestamp& message); - -absl::Status WrapGeneratedTimestampProto(absl::Time value, - google::protobuf::Timestamp& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_TIMESTAMP_LITE_H_ diff --git a/extensions/protobuf/internal/timestamp_test.cc b/extensions/protobuf/internal/timestamp_test.cc deleted file mode 100644 index dbbefc916..000000000 --- a/extensions/protobuf/internal/timestamp_test.cc +++ /dev/null @@ -1,94 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/timestamp.h" - -#include - -#include "google/protobuf/timestamp.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/memory/memory.h" -#include "absl/time/time.h" -#include "extensions/protobuf/internal/timestamp_lite.h" -#include "internal/testing.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -using ::absl_testing::IsOkAndHolds; -using ::testing::Eq; - -TEST(Timestamp, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedTimestampProto(google::protobuf::Timestamp()), - IsOkAndHolds(Eq(absl::UnixEpoch()))); -} - -TEST(Timestamp, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Timestamp::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicTimestampProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.Timestamp"))), - IsOkAndHolds(Eq(absl::UnixEpoch()))); -} - -TEST(Timestamp, GeneratedToProto) { - google::protobuf::Timestamp proto; - ASSERT_OK(WrapGeneratedTimestampProto( - absl::UnixEpoch() + absl::Seconds(1) + absl::Nanoseconds(2), proto)); - EXPECT_EQ(proto.seconds(), 1); - EXPECT_EQ(proto.nanos(), 2); -} - -TEST(Timestamp, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Timestamp::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory - .GetPrototype(pool.FindMessageTypeByName("google.protobuf.Timestamp")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* seconds_field = descriptor->FindFieldByName("seconds"); - ASSERT_NE(seconds_field, nullptr); - const auto* nanos_field = descriptor->FindFieldByName("nanos"); - ASSERT_NE(nanos_field, nullptr); - - ASSERT_OK(WrapDynamicTimestampProto( - absl::UnixEpoch() + absl::Seconds(1) + absl::Nanoseconds(2), *proto)); - - EXPECT_EQ(reflection->GetInt64(*proto, seconds_field), 1); - EXPECT_EQ(reflection->GetInt32(*proto, nanos_field), 2); -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/wrappers.cc b/extensions/protobuf/internal/wrappers.cc deleted file mode 100644 index 6fb2b7e49..000000000 --- a/extensions/protobuf/internal/wrappers.cc +++ /dev/null @@ -1,287 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/wrappers.h" - -#include -#include - -#include "google/protobuf/wrappers.pb.h" -#include "absl/base/optimization.h" -#include "absl/functional/function_ref.h" -#include "absl/log/absl_check.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "absl/strings/str_cat.h" -#include "extensions/protobuf/internal/is_generated_message.h" -#include "extensions/protobuf/internal/is_message_lite.h" -#include "extensions/protobuf/internal/wrappers_lite.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -namespace { - -template -using FieldGetterRef = - absl::FunctionRef; - -template -using GeneratedUnwrapperRef = absl::FunctionRef(const T&)>; - -template -using FieldSetterRef = - absl::FunctionRef; - -template -using GeneratedWrapperRef = absl::FunctionRef; - -template -absl::StatusOr

UnwrapValueProto(const google::protobuf::Message& message, - google::protobuf::FieldDescriptor::CppType cpp_type, - GeneratedUnwrapperRef unwrapper, - FieldGetterRef

getter) { - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return unwrapper(google::protobuf::DownCastToGenerated(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* value_field = desc->FindFieldByNumber(T::kValueFieldNumber); - if (ABSL_PREDICT_FALSE(value_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing value field descriptor")); - } - if (ABSL_PREDICT_FALSE(value_field->cpp_type() != cpp_type)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected value field type: ", value_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(value_field->is_map() || value_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), - " has unexpected value field cardinality: REPEATED")); - } - return getter(*reflect, message, value_field); -} - -template -absl::Status WrapValueProto(google::protobuf::Message& message, const P& value, - google::protobuf::FieldDescriptor::CppType cpp_type, - GeneratedWrapperRef wrapper, - FieldSetterRef

setter) { - const auto* desc = message.GetDescriptor(); - if (ABSL_PREDICT_FALSE(desc == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing descriptor")); - } - if constexpr (NotMessageLite) { - if (IsGeneratedMessage(message)) { - // Fast path. - return wrapper(value, google::protobuf::DownCastToGenerated(message)); - } - } - const auto* reflect = message.GetReflection(); - if (ABSL_PREDICT_FALSE(reflect == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing reflection")); - } - const auto* value_field = desc->FindFieldByNumber(T::kValueFieldNumber); - if (ABSL_PREDICT_FALSE(value_field == nullptr)) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), " missing value field descriptor")); - } - if (ABSL_PREDICT_FALSE(value_field->cpp_type() != cpp_type)) { - return absl::InternalError(absl::StrCat( - message.GetTypeName(), - " has unexpected value field type: ", value_field->cpp_type_name())); - } - if (ABSL_PREDICT_FALSE(value_field->is_map() || value_field->is_repeated())) { - return absl::InternalError( - absl::StrCat(message.GetTypeName(), - " has unexpected value field cardinality: REPEATED")); - } - setter(*reflect, &message, value_field, value); - return absl::OkStatus(); -} - -} // namespace - -absl::StatusOr UnwrapDynamicBoolValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.BoolValue"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_BOOL, - UnwrapGeneratedBoolValueProto, &google::protobuf::Reflection::GetBool); -} - -absl::StatusOr UnwrapDynamicBytesValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.BytesValue"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_STRING, - UnwrapGeneratedBytesValueProto, &google::protobuf::Reflection::GetCord); -} - -absl::StatusOr UnwrapDynamicFloatValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.FloatValue"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_FLOAT, - UnwrapGeneratedFloatValueProto, &google::protobuf::Reflection::GetFloat); -} - -absl::StatusOr UnwrapDynamicDoubleValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.DoubleValue"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE, - UnwrapGeneratedDoubleValueProto, &google::protobuf::Reflection::GetDouble); -} - -absl::StatusOr UnwrapDynamicInt32ValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Int32Value"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_INT32, - UnwrapGeneratedInt32ValueProto, &google::protobuf::Reflection::GetInt32); -} - -absl::StatusOr UnwrapDynamicInt64ValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Int64Value"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_INT64, - UnwrapGeneratedInt64ValueProto, &google::protobuf::Reflection::GetInt64); -} - -absl::StatusOr UnwrapDynamicStringValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.StringValue"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_STRING, - UnwrapGeneratedStringValueProto, &google::protobuf::Reflection::GetCord); -} - -absl::StatusOr UnwrapDynamicUInt32ValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.UInt32Value"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_UINT32, - UnwrapGeneratedUInt32ValueProto, &google::protobuf::Reflection::GetUInt32); -} - -absl::StatusOr UnwrapDynamicUInt64ValueProto( - const google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.UInt64Value"); - return UnwrapValueProto( - message, google::protobuf::FieldDescriptor::CPPTYPE_UINT64, - UnwrapGeneratedUInt64ValueProto, &google::protobuf::Reflection::GetUInt64); -} - -absl::Status WrapDynamicBoolValueProto(bool value, google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.BoolValue"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_BOOL, - WrapGeneratedBoolValueProto, &google::protobuf::Reflection::SetBool); -} - -absl::Status WrapDynamicBytesValueProto(const absl::Cord& value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.BytesValue"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_STRING, - WrapGeneratedBytesValueProto, - [](const google::protobuf::Reflection& reflection, google::protobuf::Message* message, - const google::protobuf::FieldDescriptor* field, - const absl::Cord& value) -> void { - reflection.SetString(message, field, value); - }); -} - -absl::Status WrapDynamicFloatValueProto(float value, google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.FloatValue"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_FLOAT, - WrapGeneratedFloatValueProto, &google::protobuf::Reflection::SetFloat); -} - -absl::Status WrapDynamicDoubleValueProto(double value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.DoubleValue"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE, - WrapGeneratedDoubleValueProto, &google::protobuf::Reflection::SetDouble); -} - -absl::Status WrapDynamicInt32ValueProto(int32_t value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Int32Value"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_INT32, - WrapGeneratedInt32ValueProto, &google::protobuf::Reflection::SetInt32); -} - -absl::Status WrapDynamicInt64ValueProto(int64_t value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.Int64Value"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_INT64, - WrapGeneratedInt64ValueProto, &google::protobuf::Reflection::SetInt64); -} - -absl::Status WrapDynamicUInt32ValueProto(uint32_t value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.UInt32Value"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_UINT32, - WrapGeneratedUInt32ValueProto, &google::protobuf::Reflection::SetUInt32); -} - -absl::Status WrapDynamicUInt64ValueProto(uint64_t value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.UInt64Value"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_UINT64, - WrapGeneratedUInt64ValueProto, &google::protobuf::Reflection::SetUInt64); -} - -absl::Status WrapDynamicStringValueProto(const absl::Cord& value, - google::protobuf::Message& message) { - ABSL_DCHECK_EQ(message.GetTypeName(), "google.protobuf.StringValue"); - return WrapValueProto( - message, value, google::protobuf::FieldDescriptor::CPPTYPE_STRING, - WrapGeneratedStringValueProto, - [](const google::protobuf::Reflection& reflection, google::protobuf::Message* message, - const google::protobuf::FieldDescriptor* field, - const absl::Cord& value) -> void { - reflection.SetString(message, field, static_cast(value)); - }); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/wrappers.h b/extensions/protobuf/internal/wrappers.h deleted file mode 100644 index 6497a1dac..000000000 --- a/extensions/protobuf/internal/wrappers.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/wrappers.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_H_ - -#include - -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" -#include "google/protobuf/message.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapDynamicBoolValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicBytesValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicFloatValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicDoubleValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicInt32ValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicInt64ValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicStringValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicUInt32ValueProto( - const google::protobuf::Message& message); - -absl::StatusOr UnwrapDynamicUInt64ValueProto( - const google::protobuf::Message& message); - -absl::Status WrapDynamicBoolValueProto(bool value, google::protobuf::Message& message); - -absl::Status WrapDynamicBytesValueProto(const absl::Cord& value, - google::protobuf::Message& message); - -absl::Status WrapDynamicFloatValueProto(float value, google::protobuf::Message& message); - -absl::Status WrapDynamicDoubleValueProto(double value, - google::protobuf::Message& message); - -absl::Status WrapDynamicInt32ValueProto(int32_t value, - google::protobuf::Message& message); - -absl::Status WrapDynamicInt64ValueProto(int64_t value, - google::protobuf::Message& message); - -absl::Status WrapDynamicStringValueProto(const absl::Cord& value, - google::protobuf::Message& message); - -absl::Status WrapDynamicUInt32ValueProto(uint32_t value, - google::protobuf::Message& message); - -absl::Status WrapDynamicUInt64ValueProto(uint64_t value, - google::protobuf::Message& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_H_ diff --git a/extensions/protobuf/internal/wrappers_lite.cc b/extensions/protobuf/internal/wrappers_lite.cc deleted file mode 100644 index 13f5e4dca..000000000 --- a/extensions/protobuf/internal/wrappers_lite.cc +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/wrappers_lite.h" - -#include -#include - -#include "google/protobuf/wrappers.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedBoolValueProto( - const google::protobuf::BoolValue& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedBytesValueProto( - const google::protobuf::BytesValue& message) { - return absl::Cord(message.value()); -} - -absl::StatusOr UnwrapGeneratedFloatValueProto( - const google::protobuf::FloatValue& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedDoubleValueProto( - const google::protobuf::DoubleValue& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedInt32ValueProto( - const google::protobuf::Int32Value& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedInt64ValueProto( - const google::protobuf::Int64Value& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedStringValueProto( - const google::protobuf::StringValue& message) { - return absl::Cord(message.value()); -} - -absl::StatusOr UnwrapGeneratedUInt32ValueProto( - const google::protobuf::UInt32Value& message) { - return message.value(); -} - -absl::StatusOr UnwrapGeneratedUInt64ValueProto( - const google::protobuf::UInt64Value& message) { - return message.value(); -} - -absl::Status WrapGeneratedBoolValueProto(bool value, - google::protobuf::BoolValue& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedBytesValueProto( - const absl::Cord& value, google::protobuf::BytesValue& message) { - message.set_value(static_cast(value)); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedFloatValueProto( - float value, google::protobuf::FloatValue& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedDoubleValueProto( - double value, google::protobuf::DoubleValue& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedInt32ValueProto( - int32_t value, google::protobuf::Int32Value& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedInt64ValueProto( - int64_t value, google::protobuf::Int64Value& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedStringValueProto( - const absl::Cord& value, google::protobuf::StringValue& message) { - message.set_value(static_cast(value)); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedUInt32ValueProto( - uint32_t value, google::protobuf::UInt32Value& message) { - message.set_value(value); - return absl::OkStatus(); -} - -absl::Status WrapGeneratedUInt64ValueProto( - uint64_t value, google::protobuf::UInt64Value& message) { - message.set_value(value); - return absl::OkStatus(); -} - -} // namespace cel::extensions::protobuf_internal diff --git a/extensions/protobuf/internal/wrappers_lite.h b/extensions/protobuf/internal/wrappers_lite.h deleted file mode 100644 index 7365015dd..000000000 --- a/extensions/protobuf/internal/wrappers_lite.h +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright 2023 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. - -// Utilities for converting to and from the well known protocol buffer message -// types in `google/protobuf/wrappers.proto`. - -#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_LITE_H_ -#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_LITE_H_ - -#include - -#include "google/protobuf/wrappers.pb.h" -#include "absl/status/status.h" -#include "absl/status/statusor.h" -#include "absl/strings/cord.h" - -namespace cel::extensions::protobuf_internal { - -absl::StatusOr UnwrapGeneratedBoolValueProto( - const google::protobuf::BoolValue& message); - -absl::StatusOr UnwrapGeneratedBytesValueProto( - const google::protobuf::BytesValue& message); - -absl::StatusOr UnwrapGeneratedFloatValueProto( - const google::protobuf::FloatValue& message); - -absl::StatusOr UnwrapGeneratedDoubleValueProto( - const google::protobuf::DoubleValue& message); - -absl::StatusOr UnwrapGeneratedInt32ValueProto( - const google::protobuf::Int32Value& message); - -absl::StatusOr UnwrapGeneratedInt64ValueProto( - const google::protobuf::Int64Value& message); - -absl::StatusOr UnwrapGeneratedStringValueProto( - const google::protobuf::StringValue& message); - -absl::StatusOr UnwrapGeneratedUInt32ValueProto( - const google::protobuf::UInt32Value& message); - -absl::StatusOr UnwrapGeneratedUInt64ValueProto( - const google::protobuf::UInt64Value& message); - -absl::Status WrapGeneratedBoolValueProto(bool value, - google::protobuf::BoolValue& message); - -absl::Status WrapGeneratedBytesValueProto( - const absl::Cord& value, google::protobuf::BytesValue& message); - -absl::Status WrapGeneratedFloatValueProto( - float value, google::protobuf::FloatValue& message); - -absl::Status WrapGeneratedDoubleValueProto( - double value, google::protobuf::DoubleValue& message); - -absl::Status WrapGeneratedInt32ValueProto( - int32_t value, google::protobuf::Int32Value& message); - -absl::Status WrapGeneratedInt64ValueProto( - int64_t value, google::protobuf::Int64Value& message); - -absl::Status WrapGeneratedStringValueProto( - const absl::Cord& value, google::protobuf::StringValue& message); - -absl::Status WrapGeneratedUInt32ValueProto( - uint32_t value, google::protobuf::UInt32Value& message); - -absl::Status WrapGeneratedUInt64ValueProto( - uint64_t value, google::protobuf::UInt64Value& message); - -} // namespace cel::extensions::protobuf_internal - -#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_INTERNAL_WRAPPERS_LITE_H_ diff --git a/extensions/protobuf/internal/wrappers_test.cc b/extensions/protobuf/internal/wrappers_test.cc deleted file mode 100644 index 3b9ad8712..000000000 --- a/extensions/protobuf/internal/wrappers_test.cc +++ /dev/null @@ -1,442 +0,0 @@ -// Copyright 2023 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 "extensions/protobuf/internal/wrappers.h" - -#include -#include - -#include "google/protobuf/wrappers.pb.h" -#include "google/protobuf/descriptor.pb.h" -#include "absl/memory/memory.h" -#include "absl/status/status.h" -#include "extensions/protobuf/internal/wrappers_lite.h" -#include "internal/testing.h" -#include "google/protobuf/descriptor.h" -#include "google/protobuf/descriptor_database.h" -#include "google/protobuf/dynamic_message.h" - -namespace cel::extensions::protobuf_internal { -namespace { - -using ::absl_testing::IsOkAndHolds; -using ::absl_testing::StatusIs; -using ::testing::Eq; - -TEST(BoolWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedBoolValueProto(google::protobuf::BoolValue()), - IsOkAndHolds(Eq(false))); -} - -TEST(BoolWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::BoolValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicBoolValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.BoolValue"))), - IsOkAndHolds(Eq(false))); -} - -TEST(BoolWrapper, GeneratedToProto) { - google::protobuf::BoolValue proto; - ASSERT_OK(WrapGeneratedBoolValueProto(true, proto)); - EXPECT_TRUE(proto.value()); -} - -TEST(BoolWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::BoolValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = absl::WrapUnique( - factory - .GetPrototype(pool.FindMessageTypeByName("google.protobuf.BoolValue")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicBoolValueProto(true, *proto)); - - EXPECT_TRUE(reflection->GetBool(*proto, value_field)); -} - -TEST(BytesWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedBytesValueProto(google::protobuf::BytesValue()), - IsOkAndHolds(Eq(absl::Cord()))); -} - -TEST(BytesWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::BytesValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicBytesValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.BytesValue"))), - IsOkAndHolds(Eq(absl::Cord()))); -} - -TEST(BytesWrapper, GeneratedToProto) { - google::protobuf::BytesValue proto; - ASSERT_OK(WrapGeneratedBytesValueProto(absl::Cord("foo"), proto)); - EXPECT_EQ(proto.value(), "foo"); -} - -TEST(BytesWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::BytesValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.BytesValue")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicBytesValueProto(absl::Cord("foo"), *proto)); - - EXPECT_EQ(reflection->GetString(*proto, value_field), "foo"); -} - -TEST(DoubleWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedFloatValueProto(google::protobuf::FloatValue()), - IsOkAndHolds(Eq(0.0f))); - EXPECT_THAT(UnwrapGeneratedDoubleValueProto(google::protobuf::DoubleValue()), - IsOkAndHolds(Eq(0.0))); -} - -TEST(DoubleWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::DoubleValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicFloatValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.FloatValue"))), - IsOkAndHolds(Eq(0.0f))); - EXPECT_THAT(UnwrapDynamicDoubleValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.DoubleValue"))), - IsOkAndHolds(Eq(0.0))); -} - -TEST(DoubleWrapper, GeneratedToProto) { - { - google::protobuf::FloatValue proto; - ASSERT_OK(WrapGeneratedFloatValueProto(1.0f, proto)); - EXPECT_EQ(proto.value(), 1.0f); - } - { - google::protobuf::DoubleValue proto; - ASSERT_OK(WrapGeneratedDoubleValueProto(1.0, proto)); - EXPECT_EQ(proto.value(), 1.0); - } -} - -TEST(DoubleWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::DoubleValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.FloatValue")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicFloatValueProto(1.0f, *proto)); - - EXPECT_EQ(reflection->GetFloat(*proto, value_field), 1.0f); - } - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.DoubleValue")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicDoubleValueProto(1.0, *proto)); - - EXPECT_EQ(reflection->GetDouble(*proto, value_field), 1.0); - } -} - -TEST(IntWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedInt32ValueProto(google::protobuf::Int32Value()), - IsOkAndHolds(Eq(0))); - EXPECT_THAT(UnwrapGeneratedInt64ValueProto(google::protobuf::Int64Value()), - IsOkAndHolds(Eq(0))); -} - -TEST(IntWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Int64Value::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicInt32ValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.Int32Value"))), - IsOkAndHolds(Eq(0))); - EXPECT_THAT(UnwrapDynamicInt64ValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.Int64Value"))), - IsOkAndHolds(Eq(0))); -} - -TEST(IntWrapper, GeneratedToProto) { - { - google::protobuf::Int32Value proto; - ASSERT_OK(WrapGeneratedInt32ValueProto(1, proto)); - EXPECT_EQ(proto.value(), 1); - } - { - google::protobuf::Int64Value proto; - ASSERT_OK(WrapGeneratedInt64ValueProto(1, proto)); - EXPECT_EQ(proto.value(), 1); - } -} - -TEST(IntWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::Int64Value::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.Int32Value")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicInt32ValueProto(1, *proto)); - - EXPECT_EQ(reflection->GetInt32(*proto, value_field), 1); - } - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.Int64Value")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicInt64ValueProto(1, *proto)); - - EXPECT_EQ(reflection->GetInt64(*proto, value_field), 1); - } -} - -TEST(StringWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedStringValueProto(google::protobuf::StringValue()), - IsOkAndHolds(absl::Cord())); -} - -TEST(StringWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::StringValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicStringValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.StringValue"))), - IsOkAndHolds(absl::Cord())); -} - -TEST(StringWrapper, GeneratedToProto) { - google::protobuf::StringValue proto; - ASSERT_OK(WrapGeneratedStringValueProto(absl::Cord("foo"), proto)); - EXPECT_EQ(proto.value(), "foo"); -} - -TEST(StringWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::StringValue::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.StringValue")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicStringValueProto(absl::Cord("foo"), *proto)); - - EXPECT_EQ(reflection->GetString(*proto, value_field), "foo"); -} - -TEST(UintWrapper, GeneratedFromProto) { - EXPECT_THAT(UnwrapGeneratedUInt32ValueProto(google::protobuf::UInt32Value()), - IsOkAndHolds(Eq(0u))); - EXPECT_THAT(UnwrapGeneratedUInt64ValueProto(google::protobuf::UInt64Value()), - IsOkAndHolds(Eq(0u))); -} - -TEST(UintWrapper, CustomFromProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::UInt64Value::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - EXPECT_THAT(UnwrapDynamicUInt32ValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.UInt32Value"))), - IsOkAndHolds(Eq(0u))); - EXPECT_THAT(UnwrapDynamicUInt64ValueProto(*factory.GetPrototype( - pool.FindMessageTypeByName("google.protobuf.UInt64Value"))), - IsOkAndHolds(Eq(0u))); -} - -TEST(UintWrapper, GeneratedToProto) { - { - google::protobuf::UInt32Value proto; - ASSERT_OK(WrapGeneratedUInt32ValueProto(1, proto)); - EXPECT_EQ(proto.value(), 1); - } - { - google::protobuf::UInt64Value proto; - ASSERT_OK(WrapGeneratedUInt64ValueProto(1, proto)); - EXPECT_EQ(proto.value(), 1); - } -} - -TEST(UintWrapper, CustomToProto) { - google::protobuf::SimpleDescriptorDatabase database; - { - google::protobuf::FileDescriptorProto fd; - google::protobuf::UInt64Value::descriptor()->file()->CopyTo(&fd); - ASSERT_TRUE(database.Add(fd)); - } - google::protobuf::DescriptorPool pool(&database); - pool.AllowUnknownDependencies(); - google::protobuf::DynamicMessageFactory factory(&pool); - factory.SetDelegateToGeneratedFactory(false); - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.UInt32Value")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicUInt32ValueProto(1, *proto)); - - EXPECT_EQ(reflection->GetUInt32(*proto, value_field), 1); - } - { - std::unique_ptr proto = - absl::WrapUnique(factory - .GetPrototype(pool.FindMessageTypeByName( - "google.protobuf.UInt64Value")) - ->New()); - const auto* descriptor = proto->GetDescriptor(); - const auto* reflection = proto->GetReflection(); - const auto* value_field = descriptor->FindFieldByName("value"); - ASSERT_NE(value_field, nullptr); - - ASSERT_OK(WrapDynamicUInt64ValueProto(1, *proto)); - - EXPECT_EQ(reflection->GetUInt64(*proto, value_field), 1); - } -} - -} // namespace -} // namespace cel::extensions::protobuf_internal diff --git a/internal/BUILD b/internal/BUILD index 81ee3bc9f..ff716fa01 100644 --- a/internal/BUILD +++ b/internal/BUILD @@ -281,7 +281,6 @@ cc_library( hdrs = ["proto_util.h"], deps = [ ":status_macros", - "//extensions/protobuf/internal:is_message_lite", "@com_google_absl//absl/status", "@com_google_absl//absl/strings:str_format", "@com_google_protobuf//:protobuf", diff --git a/internal/json.cc b/internal/json.cc index c696ee3b3..aa5d6cce0 100644 --- a/internal/json.cc +++ b/internal/json.cc @@ -2176,113 +2176,292 @@ absl::StatusOr ProtoJsonMapToNativeJsonMap( namespace { -struct NativeJsonToProtoJsonState { - ValueReflection value_reflection; - ListValueReflection list_value_reflection; - StructReflection struct_reflection; - std::string scratch; +class JsonMutator { + public: + virtual ~JsonMutator() = default; - absl::Status Initialize(absl::Nonnull proto) { - CEL_RETURN_IF_ERROR(value_reflection.Initialize(proto->GetDescriptor())); - CEL_RETURN_IF_ERROR(list_value_reflection.Initialize( - value_reflection.GetListValueDescriptor())); + virtual void SetNullValue( + absl::Nonnull message) const = 0; + + virtual void SetBoolValue(absl::Nonnull message, + bool value) const = 0; + + virtual void SetNumberValue(absl::Nonnull message, + double value) const = 0; + + virtual void SetStringValue(absl::Nonnull message, + const absl::Cord& value) const = 0; + + virtual absl::Nonnull MutableListValue( + absl::Nonnull message) const = 0; + + virtual void ReserveValues(absl::Nonnull message, + int capacity) const = 0; + + virtual absl::Nonnull AddValues( + absl::Nonnull message) const = 0; + + virtual absl::Nonnull MutableStructValue( + absl::Nonnull message) const = 0; + + virtual absl::Nonnull InsertField( + absl::Nonnull message, + absl::string_view name) const = 0; +}; + +class GeneratedJsonMutator final : public JsonMutator { + public: + static absl::Nonnull Singleton() { + static const absl::NoDestructor instance; + return &*instance; + } + + void SetNullValue( + absl::Nonnull message) const override { + ValueReflection::SetNullValue( + google::protobuf::DownCastMessage(message)); + } + + void SetBoolValue(absl::Nonnull message, + bool value) const override { + ValueReflection::SetBoolValue( + google::protobuf::DownCastMessage(message), value); + } + + void SetNumberValue(absl::Nonnull message, + double value) const override { + ValueReflection::SetNumberValue( + google::protobuf::DownCastMessage(message), value); + } + + void SetStringValue(absl::Nonnull message, + const absl::Cord& value) const override { + ValueReflection::SetStringValue( + google::protobuf::DownCastMessage(message), value); + } + + absl::Nonnull MutableListValue( + absl::Nonnull message) const override { + return ValueReflection::MutableListValue( + google::protobuf::DownCastMessage(message)); + } + + void ReserveValues(absl::Nonnull message, + int capacity) const override { + ListValueReflection::ReserveValues( + google::protobuf::DownCastMessage(message), + capacity); + } + + absl::Nonnull AddValues( + absl::Nonnull message) const override { + return ListValueReflection::AddValues( + google::protobuf::DownCastMessage(message)); + } + + absl::Nonnull MutableStructValue( + absl::Nonnull message) const override { + return ValueReflection::MutableStructValue( + google::protobuf::DownCastMessage(message)); + } + + absl::Nonnull InsertField( + absl::Nonnull message, + absl::string_view name) const override { + return StructReflection::InsertField( + google::protobuf::DownCastMessage(message), name); + } +}; + +class DynamicJsonMutator final : public JsonMutator { + public: + absl::Status InitializeValue( + absl::Nonnull descriptor) { + CEL_RETURN_IF_ERROR(value_reflection_.Initialize(descriptor)); + CEL_RETURN_IF_ERROR(list_value_reflection_.Initialize( + value_reflection_.GetListValueDescriptor())); CEL_RETURN_IF_ERROR( - struct_reflection.Initialize(value_reflection.GetStructDescriptor())); + struct_reflection_.Initialize(value_reflection_.GetStructDescriptor())); return absl::OkStatus(); } - absl::Status InitializeListValue(absl::Nonnull proto) { - CEL_RETURN_IF_ERROR( - list_value_reflection.Initialize(proto->GetDescriptor())); - CEL_RETURN_IF_ERROR(value_reflection.Initialize( - list_value_reflection.GetValueDescriptor())); + absl::Status InitializeListValue( + absl::Nonnull descriptor) { + CEL_RETURN_IF_ERROR(list_value_reflection_.Initialize(descriptor)); + CEL_RETURN_IF_ERROR(value_reflection_.Initialize( + list_value_reflection_.GetValueDescriptor())); CEL_RETURN_IF_ERROR( - struct_reflection.Initialize(value_reflection.GetStructDescriptor())); + struct_reflection_.Initialize(value_reflection_.GetStructDescriptor())); return absl::OkStatus(); } - absl::Status InitializeStruct(absl::Nonnull proto) { - CEL_RETURN_IF_ERROR(struct_reflection.Initialize(proto->GetDescriptor())); + absl::Status InitializeStruct( + absl::Nonnull descriptor) { + CEL_RETURN_IF_ERROR(struct_reflection_.Initialize(descriptor)); CEL_RETURN_IF_ERROR( - value_reflection.Initialize(struct_reflection.GetValueDescriptor())); - CEL_RETURN_IF_ERROR(list_value_reflection.Initialize( - value_reflection.GetListValueDescriptor())); + value_reflection_.Initialize(struct_reflection_.GetValueDescriptor())); + CEL_RETURN_IF_ERROR(list_value_reflection_.Initialize( + value_reflection_.GetListValueDescriptor())); return absl::OkStatus(); } + void SetNullValue( + absl::Nonnull message) const override { + value_reflection_.SetNullValue( + google::protobuf::DownCastMessage(message)); + } + + void SetBoolValue(absl::Nonnull message, + bool value) const override { + value_reflection_.SetBoolValue( + google::protobuf::DownCastMessage(message), value); + } + + void SetNumberValue(absl::Nonnull message, + double value) const override { + value_reflection_.SetNumberValue( + google::protobuf::DownCastMessage(message), value); + } + + void SetStringValue(absl::Nonnull message, + const absl::Cord& value) const override { + value_reflection_.SetStringValue( + google::protobuf::DownCastMessage(message), value); + } + + absl::Nonnull MutableListValue( + absl::Nonnull message) const override { + return value_reflection_.MutableListValue( + google::protobuf::DownCastMessage(message)); + } + + void ReserveValues(absl::Nonnull message, + int capacity) const override { + list_value_reflection_.ReserveValues( + google::protobuf::DownCastMessage(message), capacity); + } + + absl::Nonnull AddValues( + absl::Nonnull message) const override { + return list_value_reflection_.AddValues( + google::protobuf::DownCastMessage(message)); + } + + absl::Nonnull MutableStructValue( + absl::Nonnull message) const override { + return value_reflection_.MutableStructValue( + google::protobuf::DownCastMessage(message)); + } + + absl::Nonnull InsertField( + absl::Nonnull message, + absl::string_view name) const override { + return struct_reflection_.InsertField( + google::protobuf::DownCastMessage(message), name); + } + + private: + ValueReflection value_reflection_; + ListValueReflection list_value_reflection_; + StructReflection struct_reflection_; +}; + +class NativeJsonToProtoJsonState { + public: + explicit NativeJsonToProtoJsonState(absl::Nonnull mutator) + : mutator_(mutator) {} + absl::Status ToProtoJson(const Json& json, - absl::Nonnull proto) { + absl::Nonnull proto) { return absl::visit( absl::Overload( [&](JsonNull) -> absl::Status { - value_reflection.SetNullValue(proto); + mutator_->SetNullValue(proto); return absl::OkStatus(); }, [&](JsonBool value) -> absl::Status { - value_reflection.SetBoolValue(proto, value); + mutator_->SetBoolValue(proto, value); return absl::OkStatus(); }, [&](JsonNumber value) -> absl::Status { - value_reflection.SetNumberValue(proto, value); + mutator_->SetNumberValue(proto, value); return absl::OkStatus(); }, [&](const JsonString& value) -> absl::Status { - value_reflection.SetStringValue(proto, value); + mutator_->SetStringValue(proto, value); return absl::OkStatus(); }, [&](const JsonArray& value) -> absl::Status { - return ToProtoJsonList(value, - value_reflection.MutableListValue(proto)); + return ToProtoJsonList(value, mutator_->MutableListValue(proto)); }, [&](const JsonObject& value) -> absl::Status { - return ToProtoJsonMap(value, - value_reflection.MutableStructValue(proto)); + return ToProtoJsonMap(value, mutator_->MutableStructValue(proto)); }), json); } absl::Status ToProtoJsonList(const JsonArray& json, - absl::Nonnull proto) { - list_value_reflection.ReserveValues(proto, static_cast(json.size())); + absl::Nonnull proto) { + mutator_->ReserveValues(proto, static_cast(json.size())); for (const auto& element : json) { - CEL_RETURN_IF_ERROR( - ToProtoJson(element, list_value_reflection.AddValues(proto))); + CEL_RETURN_IF_ERROR(ToProtoJson(element, mutator_->AddValues(proto))); } return absl::OkStatus(); } absl::Status ToProtoJsonMap(const JsonObject& json, - absl::Nonnull proto) { + absl::Nonnull proto) { for (const auto& entry : json) { CEL_RETURN_IF_ERROR(ToProtoJson( - entry.second, struct_reflection.InsertField( - proto, static_cast(entry.first)))); + entry.second, + mutator_->InsertField(proto, static_cast(entry.first)))); } return absl::OkStatus(); } + + private: + absl::Nonnull const mutator_; }; } // namespace absl::Status NativeJsonToProtoJson(const Json& json, absl::Nonnull proto) { - NativeJsonToProtoJsonState state; - CEL_RETURN_IF_ERROR(state.Initialize(proto)); - return state.ToProtoJson(json, proto); + DynamicJsonMutator mutator; + CEL_RETURN_IF_ERROR(mutator.InitializeValue(proto->GetDescriptor())); + return NativeJsonToProtoJsonState(&mutator).ToProtoJson(json, proto); +} + +absl::Status NativeJsonToProtoJson( + const Json& json, absl::Nonnull proto) { + return NativeJsonToProtoJsonState(GeneratedJsonMutator::Singleton()) + .ToProtoJson(json, proto); } absl::Status NativeJsonListToProtoJsonList( const JsonArray& json, absl::Nonnull proto) { - NativeJsonToProtoJsonState state; - CEL_RETURN_IF_ERROR(state.InitializeListValue(proto)); - return state.ToProtoJsonList(json, proto); + DynamicJsonMutator mutator; + CEL_RETURN_IF_ERROR(mutator.InitializeListValue(proto->GetDescriptor())); + return NativeJsonToProtoJsonState(&mutator).ToProtoJsonList(json, proto); +} + +absl::Status NativeJsonListToProtoJsonList( + const JsonArray& json, absl::Nonnull proto) { + return NativeJsonToProtoJsonState(GeneratedJsonMutator::Singleton()) + .ToProtoJsonList(json, proto); } absl::Status NativeJsonMapToProtoJsonMap( const JsonObject& json, absl::Nonnull proto) { - NativeJsonToProtoJsonState state; - CEL_RETURN_IF_ERROR(state.InitializeStruct(proto)); - return state.ToProtoJsonMap(json, proto); + DynamicJsonMutator mutator; + CEL_RETURN_IF_ERROR(mutator.InitializeStruct(proto->GetDescriptor())); + return NativeJsonToProtoJsonState(&mutator).ToProtoJsonMap(json, proto); +} + +absl::Status NativeJsonMapToProtoJsonMap( + const JsonObject& json, absl::Nonnull proto) { + return NativeJsonToProtoJsonState(GeneratedJsonMutator::Singleton()) + .ToProtoJsonMap(json, proto); } } // namespace cel::internal diff --git a/internal/json.h b/internal/json.h index 4fe4ef20a..5cef14496 100644 --- a/internal/json.h +++ b/internal/json.h @@ -148,18 +148,24 @@ absl::StatusOr ProtoJsonMapToNativeJsonMap( // pure proto. absl::Status NativeJsonToProtoJson(const Json& json, absl::Nonnull proto); +absl::Status NativeJsonToProtoJson( + const Json& json, absl::Nonnull proto); // Temporary function which converts from `cel::JsonArray` to // `google.protobuf.ListValue`. In future `cel::JsonArray` will be killed in // favor of pure proto. absl::Status NativeJsonListToProtoJsonList( const JsonArray& json, absl::Nonnull proto); +absl::Status NativeJsonListToProtoJsonList( + const JsonArray& json, absl::Nonnull proto); // Temporary function which converts from `cel::JsonObject` to // `google.protobuf.Struct`. In future `cel::JsonObject` will be killed in // favor of pure proto. absl::Status NativeJsonMapToProtoJsonMap(const JsonObject& json, absl::Nonnull proto); +absl::Status NativeJsonMapToProtoJsonMap( + const JsonObject& json, absl::Nonnull proto); } // namespace cel::internal diff --git a/internal/proto_util.h b/internal/proto_util.h index 66124fbb9..2b07516eb 100644 --- a/internal/proto_util.h +++ b/internal/proto_util.h @@ -16,12 +16,12 @@ #define THIRD_PARTY_CEL_CPP_INTERNAL_PROTO_UTIL_H_ #include +#include #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/util/message_differencer.h" #include "absl/status/status.h" #include "absl/strings/str_format.h" -#include "extensions/protobuf/internal/is_message_lite.h" namespace google { namespace api { @@ -38,8 +38,7 @@ struct DefaultProtoEqual { template absl::Status ValidateStandardMessageType( const google::protobuf::DescriptorPool& descriptor_pool) { - if constexpr (cel::extensions::protobuf_internal::NotMessageLite< - MessageType>) { + if constexpr (std::is_base_of_v) { const google::protobuf::Descriptor* descriptor = MessageType::descriptor(); const google::protobuf::Descriptor* descriptor_from_pool = descriptor_pool.FindMessageTypeByName(descriptor->full_name()); diff --git a/internal/status_builder.h b/internal/status_builder.h index 76d263c07..9caa6c462 100644 --- a/internal/status_builder.h +++ b/internal/status_builder.h @@ -25,21 +25,26 @@ namespace cel::internal { class StatusBuilder; -template -inline constexpr bool kResultMatches = - std::is_same_v>, - Expected>; +template +inline constexpr bool StatusBuilderResultMatches = + std::is_same_v>, Expected>; template -using EnableIfStatusBuilder = - std::enable_if_t, - std::invoke_result_t>; +using StatusBuilderPurePolicy = std::enable_if_t< + StatusBuilderResultMatches, + std::invoke_result_t>; template -using EnableIfStatus = - std::enable_if_t, +using StatusBuilderSideEffect = + std::enable_if_t, std::invoke_result_t>; +template +using StatusBuilderConversion = std::enable_if_t< + !StatusBuilderResultMatches && + !StatusBuilderResultMatches, + std::invoke_result_t>; + class StatusBuilder final { public: StatusBuilder() = default; @@ -66,24 +71,37 @@ class StatusBuilder final { template auto With( - Adaptor&& adaptor) & -> EnableIfStatusBuilder { + Adaptor&& adaptor) & -> StatusBuilderPurePolicy { return std::forward(adaptor)(*this); } - template ABSL_MUST_USE_RESULT auto With( - Adaptor&& adaptor) && -> EnableIfStatusBuilder { + Adaptor&& + adaptor) && -> StatusBuilderPurePolicy { return std::forward(adaptor)(std::move(*this)); } template - auto With(Adaptor&& adaptor) & -> EnableIfStatus { + auto With( + Adaptor&& adaptor) & -> StatusBuilderSideEffect { return std::forward(adaptor)(*this); } + template + ABSL_MUST_USE_RESULT auto With( + Adaptor&& + adaptor) && -> StatusBuilderSideEffect { + return std::forward(adaptor)(std::move(*this)); + } + template + auto With( + Adaptor&& adaptor) & -> StatusBuilderConversion { + return std::forward(adaptor)(*this); + } template ABSL_MUST_USE_RESULT auto With( - Adaptor&& adaptor) && -> EnableIfStatus { + Adaptor&& + adaptor) && -> StatusBuilderConversion { return std::forward(adaptor)(std::move(*this)); } diff --git a/internal/well_known_types.cc b/internal/well_known_types.cc index f3ff9f063..5fee50689 100644 --- a/internal/well_known_types.cc +++ b/internal/well_known_types.cc @@ -36,6 +36,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/cord.h" +#include "absl/strings/escaping.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_join.h" #include "absl/strings/string_view.h" @@ -1113,22 +1114,21 @@ void TimestampReflection::SetNanos(absl::Nonnull mes absl::Status TimestampReflection::SetFromAbslTime( absl::Nonnull message, absl::Time time) const { - absl::Duration duration = time - absl::UnixEpoch(); - int64_t seconds = absl::IDivDuration(duration, absl::Seconds(1), &duration); + int64_t seconds = absl::ToUnixSeconds(time); if (ABSL_PREDICT_FALSE(seconds < TimeUtil::kTimestampMinSeconds || seconds > TimeUtil::kTimestampMaxSeconds)) { return absl::InvalidArgumentError( absl::StrCat("invalid timestamp seconds: ", seconds)); } - int32_t nanos = static_cast( - absl::IDivDuration(duration, absl::Nanoseconds(1), &duration)); + int64_t nanos = static_cast((time - absl::FromUnixSeconds(seconds)) / + absl::Nanoseconds(1)); if (ABSL_PREDICT_FALSE(nanos < TimeUtil::kTimestampMinNanoseconds || nanos > TimeUtil::kTimestampMaxNanoseconds)) { return absl::InvalidArgumentError( absl::StrCat("invalid timestamp nanoseconds: ", nanos)); } SetSeconds(message, seconds); - SetNanos(message, nanos); + SetNanos(message, static_cast(nanos)); return absl::OkStatus(); } @@ -1332,6 +1332,54 @@ void ValueReflection::SetStringValue(absl::Nonnull m message->GetReflection()->SetString(message, string_value_field_, value); } +void ValueReflection::SetStringValueFromBytes( + absl::Nonnull message, absl::string_view value) const { + ABSL_DCHECK(IsInitialized()); + ABSL_DCHECK_EQ(message->GetDescriptor(), descriptor_); + if (value.empty()) { + SetStringValue(message, value); + return; + } + SetStringValue(message, absl::Base64Escape(value)); +} + +void ValueReflection::SetStringValueFromBytes( + absl::Nonnull message, const absl::Cord& value) const { + ABSL_DCHECK(IsInitialized()); + ABSL_DCHECK_EQ(message->GetDescriptor(), descriptor_); + if (value.empty()) { + SetStringValue(message, value); + return; + } + if (auto flat = value.TryFlat(); flat) { + SetStringValue(message, absl::Base64Escape(*flat)); + return; + } + std::string flat; + absl::CopyCordToString(value, &flat); + SetStringValue(message, absl::Base64Escape(flat)); +} + +void ValueReflection::SetStringValueFromDuration( + absl::Nonnull message, absl::Duration duration) const { + google::protobuf::Duration proto; + proto.set_seconds(absl::IDivDuration(duration, absl::Seconds(1), &duration)); + proto.set_nanos(static_cast( + absl::IDivDuration(duration, absl::Nanoseconds(1), &duration))); + ABSL_DCHECK(TimeUtil::IsDurationValid(proto)); + SetStringValue(message, TimeUtil::ToString(proto)); +} + +void ValueReflection::SetStringValueFromTimestamp( + absl::Nonnull message, absl::Time time) const { + google::protobuf::Timestamp proto; + proto.set_seconds(absl::ToUnixSeconds(time)); + proto.set_nanos((time - absl::FromUnixSeconds(proto.seconds())) / + absl::Nanoseconds(1)); + ABSL_DCHECK(TimeUtil::IsTimestampValid(proto)); + SetStringValue(message, TimeUtil::ToString(proto)); +} + absl::Nonnull ValueReflection::MutableListValue( absl::Nonnull message) const { ABSL_DCHECK(IsInitialized()); diff --git a/internal/well_known_types.h b/internal/well_known_types.h index 01dfabb1e..0e7005f6c 100644 --- a/internal/well_known_types.h +++ b/internal/well_known_types.h @@ -966,6 +966,18 @@ class ValueReflection final { void SetStringValue(absl::Nonnull message, const absl::Cord& value) const; + void SetStringValueFromBytes(absl::Nonnull message, + absl::string_view value) const; + + void SetStringValueFromBytes(absl::Nonnull message, + const absl::Cord& value) const; + + void SetStringValueFromDuration(absl::Nonnull message, + absl::Duration duration) const; + + void SetStringValueFromTimestamp(absl::Nonnull message, + absl::Time time) const; + absl::Nonnull MutableListValue( absl::Nonnull message) const;