diff --git a/eval/public/BUILD b/eval/public/BUILD index d3340c893..3ccdf45a7 100644 --- a/eval/public/BUILD +++ b/eval/public/BUILD @@ -208,6 +208,8 @@ cc_library( "//eval/public/containers:field_backed_list_impl", "//eval/public/containers:field_backed_map_impl", "@com_google_absl//absl/status", + "@com_google_absl//absl/types:span", + "@com_google_protobuf//:protobuf", ], ) @@ -793,11 +795,13 @@ cc_test( deps = [ ":activation", ":activation_bind_helper", + "//eval/testutil:test_extensions_cc_proto", "//eval/testutil:test_message_cc_proto", "//internal:status_macros", "//internal:testing", "//testutil:util", "@com_google_absl//absl/status", + "@com_google_absl//absl/status:status_matchers", ], ) diff --git a/eval/public/activation_bind_helper.cc b/eval/public/activation_bind_helper.cc index 1e8004003..0461ef7f0 100644 --- a/eval/public/activation_bind_helper.cc +++ b/eval/public/activation_bind_helper.cc @@ -1,9 +1,12 @@ #include "eval/public/activation_bind_helper.h" #include "absl/status/status.h" +#include "absl/types/span.h" +#include "eval/public/activation.h" #include "eval/public/containers/field_access.h" #include "eval/public/containers/field_backed_list_impl.h" #include "eval/public/containers/field_backed_map_impl.h" +#include "google/protobuf/message.h" namespace google { namespace api { @@ -33,6 +36,29 @@ absl::Status CreateValueFromField(const google::protobuf::Message* msg, } } +// Binds a field of the message to the activation. With kSkip, an unset +// non-repeated field is not bound. +absl::Status BindField(const Message* message, + const google::protobuf::Reflection* reflection, + const FieldDescriptor* field_desc, Arena* arena, + Activation* activation, ProtoUnsetFieldOptions options) { + if (options == ProtoUnsetFieldOptions::kSkip) { + if (!field_desc->is_repeated() && + !reflection->HasField(*message, field_desc)) { + return absl::OkStatus(); + } + } + + CelValue value; + auto status = CreateValueFromField(message, field_desc, arena, &value); + if (!status.ok()) { + return status; + } + + activation->InsertValue(field_desc->name(), value); + return absl::OkStatus(); +} + } // namespace absl::Status BindProtoToActivation(const Message* message, Arena* arena, @@ -49,22 +75,41 @@ absl::Status BindProtoToActivation(const Message* message, Arena* arena, const Descriptor* desc = message->GetDescriptor(); const google::protobuf::Reflection* reflection = message->GetReflection(); for (int i = 0; i < desc->field_count(); i++) { - CelValue value; - const FieldDescriptor* field_desc = desc->field(i); - - if (options == ProtoUnsetFieldOptions::kSkip) { - if (!field_desc->is_repeated() && - !reflection->HasField(*message, field_desc)) { - continue; - } + auto status = BindField(message, reflection, desc->field(i), arena, + activation, options); + if (!status.ok()) { + return status; } + } - auto status = CreateValueFromField(message, field_desc, arena, &value); + return absl::OkStatus(); +} + +absl::Status BindProtoToActivation( + const Message* message, Arena* arena, Activation* activation, + absl::Span fields, + ProtoUnsetFieldOptions options) { + // If we need to bind any types that are backed by an arena allocation, we + // will cause a memory leak. + if (arena == nullptr) { + return absl::InvalidArgumentError( + "arena must not be null for BindProtoToActivation."); + } + + const Descriptor* desc = message->GetDescriptor(); + const google::protobuf::Reflection* reflection = message->GetReflection(); + for (const FieldDescriptor* field_desc : fields) { + if (field_desc == nullptr || field_desc->is_extension() || + field_desc->containing_type() != desc) { + return absl::InvalidArgumentError( + "fields must be non-null, non-extension fields of the message for " + "BindProtoToActivation."); + } + auto status = + BindField(message, reflection, field_desc, arena, activation, options); if (!status.ok()) { return status; } - - activation->InsertValue(field_desc->name(), value); } return absl::OkStatus(); diff --git a/eval/public/activation_bind_helper.h b/eval/public/activation_bind_helper.h index fe5828f12..d54628500 100644 --- a/eval/public/activation_bind_helper.h +++ b/eval/public/activation_bind_helper.h @@ -1,6 +1,7 @@ #ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_BIND_HELPER_H_ #define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_ACTIVATION_BIND_HELPER_H_ +#include "absl/types/span.h" #include "eval/public/activation.h" namespace google { @@ -52,6 +53,15 @@ absl::Status BindProtoToActivation( Activation* activation, ProtoUnsetFieldOptions options = ProtoUnsetFieldOptions::kSkip); +// Same as above, but binds only |fields|, which must be non-null, non-extension +// fields of the message. |arena| must be non-null. Useful when only a few +// fields of a large message are needed. +absl::Status BindProtoToActivation( + const google::protobuf::Message* message, google::protobuf::Arena* arena, + Activation* activation, + absl::Span fields, + ProtoUnsetFieldOptions options = ProtoUnsetFieldOptions::kSkip); + } // namespace runtime } // namespace expr } // namespace api diff --git a/eval/public/activation_bind_helper_test.cc b/eval/public/activation_bind_helper_test.cc index f644f1669..f47054134 100644 --- a/eval/public/activation_bind_helper_test.cc +++ b/eval/public/activation_bind_helper_test.cc @@ -1,7 +1,9 @@ #include "eval/public/activation_bind_helper.h" #include "absl/status/status.h" +#include "absl/status/status_matchers.h" #include "eval/public/activation.h" +#include "eval/testutil/test_extensions.pb.h" #include "eval/testutil/test_message.pb.h" #include "internal/status_macros.h" #include "internal/testing.h" @@ -14,6 +16,7 @@ namespace runtime { namespace { +using ::absl_testing::IsOk; using testutil::EqualsProto; TEST(ActivationBindHelperTest, TestSingleBoolBind) { @@ -138,6 +141,164 @@ TEST(ActivationBindHelperTest, RejectsNullArena) { "arena must not be null for BindProtoToActivation.")); } +TEST(ActivationBindHelperTest, BindsOnlySpecifiedFields) { + TestMessage message; + message.set_int32_value(42); + message.set_bool_value(true); + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor()->FindFieldByName("int32_value")}; + + ASSERT_THAT(BindProtoToActivation(&message, &arena, &activation, fields), + IsOk()); + + auto result = activation.FindValue("int32_value", &arena); + + ASSERT_TRUE(result.has_value()); + + CelValue value = result.value(); + + ASSERT_TRUE(value.IsInt64()); + EXPECT_EQ(value.Int64OrDie(), 42); + + result = activation.FindValue("bool_value", &arena); + ASSERT_FALSE(result.has_value()); +} + +TEST(ActivationBindHelperTest, SpecifiedUnsetRepeatedFieldIsEmptyList) { + TestMessage message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor()->FindFieldByName("int32_list")}; + + ASSERT_THAT(BindProtoToActivation(&message, &arena, &activation, fields), + IsOk()); + + auto result = activation.FindValue("int32_list", &arena); + + ASSERT_TRUE(result.has_value()); + + CelValue value = result.value(); + + ASSERT_TRUE(value.IsList()); + EXPECT_TRUE(value.ListOrDie()->empty()); +} + +TEST(ActivationBindHelperTest, SkipsSpecifiedUnsetField) { + TestMessage message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor()->FindFieldByName("int32_value")}; + + ASSERT_THAT(BindProtoToActivation(&message, &arena, &activation, fields, + ProtoUnsetFieldOptions::kSkip), + IsOk()); + + auto result = activation.FindValue("int32_value", &arena); + ASSERT_FALSE(result.has_value()); +} + +TEST(ActivationBindHelperTest, BindsDefaultForSpecifiedUnsetField) { + TestMessage message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor()->FindFieldByName("int32_value")}; + + ASSERT_THAT(BindProtoToActivation(&message, &arena, &activation, fields, + ProtoUnsetFieldOptions::kBindDefault), + IsOk()); + + auto result = activation.FindValue("int32_value", &arena); + + ASSERT_TRUE(result.has_value()); + + CelValue value = result.value(); + + ASSERT_TRUE(value.IsInt64()); + EXPECT_EQ(value.Int64OrDie(), 0); +} + +TEST(ActivationBindHelperTest, RejectsNullArenaWithSpecifiedFields) { + TestMessage message; + message.set_bool_value(true); + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor()->FindFieldByName("bool_value")}; + + ASSERT_EQ( + BindProtoToActivation(&message, /*arena=*/nullptr, &activation, fields), + absl::InvalidArgumentError( + "arena must not be null for BindProtoToActivation.")); +} + +TEST(ActivationBindHelperTest, RejectsFieldOfOtherMessage) { + TestMessage message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestMessage::descriptor() + ->FindFieldByName("duration_value") + ->message_type() + ->FindFieldByName("seconds")}; + + ASSERT_EQ(BindProtoToActivation(&message, &arena, &activation, fields), + absl::InvalidArgumentError( + "fields must be non-null, non-extension fields of the message " + "for BindProtoToActivation.")); +} + +TEST(ActivationBindHelperTest, RejectsExtensionField) { + TestExtensions message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = { + TestExtensions::descriptor()->file()->FindExtensionByName("int32_ext")}; + + ASSERT_EQ(BindProtoToActivation(&message, &arena, &activation, fields), + absl::InvalidArgumentError( + "fields must be non-null, non-extension fields of the message " + "for BindProtoToActivation.")); +} + +TEST(ActivationBindHelperTest, RejectsNullField) { + TestMessage message; + + google::protobuf::Arena arena; + + Activation activation; + + const google::protobuf::FieldDescriptor* fields[] = {nullptr}; + + ASSERT_EQ(BindProtoToActivation(&message, &arena, &activation, fields), + absl::InvalidArgumentError( + "fields must be non-null, non-extension fields of the message " + "for BindProtoToActivation.")); +} + } // namespace } // namespace runtime