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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eval/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down Expand Up @@ -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",
],
)

Expand Down
67 changes: 56 additions & 11 deletions eval/public/activation_bind_helper.cc
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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<const FieldDescriptor* const> 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();
Expand Down
10 changes: 10 additions & 0 deletions eval/public/activation_bind_helper.h
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<const google::protobuf::FieldDescriptor* const> fields,
ProtoUnsetFieldOptions options = ProtoUnsetFieldOptions::kSkip);

} // namespace runtime
} // namespace expr
} // namespace api
Expand Down
161 changes: 161 additions & 0 deletions eval/public/activation_bind_helper_test.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -14,6 +16,7 @@ namespace runtime {

namespace {

using ::absl_testing::IsOk;
using testutil::EqualsProto;

TEST(ActivationBindHelperTest, TestSingleBoolBind) {
Expand Down Expand Up @@ -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
Expand Down
Loading