Skip to content

Commit b80e12f

Browse files
jnthntatumcopybara-github
authored andcommitted
Add wrapping version of BindProtoToActivation.
PiperOrigin-RevId: 945320272
1 parent 6108479 commit b80e12f

9 files changed

Lines changed: 591 additions & 184 deletions

File tree

conformance/policy/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ cc_library(
3636
"//env:env_std_extensions",
3737
"//env:env_yaml",
3838
"//env:runtime_std_extensions",
39-
"//extensions/protobuf:bind_proto_to_activation",
4039
"//extensions/protobuf:enum_adapter",
4140
"//internal:runfiles",
4241
"//internal:status_macros",
@@ -50,6 +49,7 @@ cc_library(
5049
"//policy:yaml_policy_parser",
5150
"//runtime",
5251
"//runtime:activation",
52+
"//runtime:bind_proto_to_activation",
5353
"//runtime:function_adapter",
5454
"@com_google_absl//absl/flags:flag",
5555
"@com_google_absl//absl/log:absl_check",

conformance/policy/policy_conformance_test.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "env/env_std_extensions.h"
4141
#include "env/env_yaml.h"
4242
#include "env/runtime_std_extensions.h"
43-
#include "extensions/protobuf/bind_proto_to_activation.h"
4443
#include "extensions/protobuf/enum_adapter.h"
4544
#include "internal/runfiles.h"
4645
#include "internal/status_macros.h"
@@ -53,6 +52,7 @@
5352
#include "policy/test_util.h"
5453
#include "policy/yaml_policy_parser.h"
5554
#include "runtime/activation.h"
55+
#include "runtime/bind_proto_to_activation.h"
5656
#include "runtime/function_adapter.h"
5757
#include "runtime/runtime.h"
5858
#include "cel/expr/conformance/test/suite.pb.h"
@@ -384,9 +384,8 @@ absl::Status PopulateActivation(
384384
"Failed to resolve context message for test case");
385385
}
386386

387-
return cel::extensions::BindProtoToActivation(
388-
*context_message,
389-
cel::extensions::BindProtoUnsetFieldBehavior::kBindDefaultValue,
387+
return cel::BindProtoToActivation(
388+
*context_message, cel::BindProtoUnsetFieldBehavior::kBindDefaultValue,
390389
descriptor_pool, message_factory, arena, &activation);
391390
}
392391

extensions/protobuf/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ cc_library(
165165
srcs = ["bind_proto_to_activation.cc"],
166166
hdrs = ["bind_proto_to_activation.h"],
167167
deps = [
168-
":value",
169168
"//common:casting",
170169
"//common:value",
171170
"//internal:status_macros",
172171
"//runtime:activation",
172+
"//runtime:bind_proto_to_activation",
173+
"@com_google_absl//absl/base:core_headers",
173174
"@com_google_absl//absl/base:nullability",
174175
"@com_google_absl//absl/status",
175176
"@com_google_absl//absl/status:statusor",

extensions/protobuf/bind_proto_to_activation.cc

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,80 +13,3 @@
1313
// limitations under the License.
1414

1515
#include "extensions/protobuf/bind_proto_to_activation.h"
16-
17-
#include <utility>
18-
19-
#include "absl/base/nullability.h"
20-
#include "absl/status/status.h"
21-
#include "absl/status/statusor.h"
22-
#include "common/value.h"
23-
#include "internal/status_macros.h"
24-
#include "runtime/activation.h"
25-
#include "google/protobuf/arena.h"
26-
#include "google/protobuf/descriptor.h"
27-
#include "google/protobuf/message.h"
28-
29-
namespace cel::extensions::protobuf_internal {
30-
31-
namespace {
32-
33-
using ::google::protobuf::Descriptor;
34-
35-
absl::StatusOr<bool> ShouldBindField(
36-
const google::protobuf::FieldDescriptor* field_desc, const StructValue& struct_value,
37-
BindProtoUnsetFieldBehavior unset_field_behavior) {
38-
if (unset_field_behavior == BindProtoUnsetFieldBehavior::kBindDefaultValue ||
39-
field_desc->is_repeated()) {
40-
return true;
41-
}
42-
return struct_value.HasFieldByNumber(field_desc->number());
43-
}
44-
45-
absl::StatusOr<Value> GetFieldValue(
46-
const google::protobuf::FieldDescriptor* field_desc, const StructValue& struct_value,
47-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
48-
google::protobuf::MessageFactory* absl_nonnull message_factory,
49-
google::protobuf::Arena* absl_nonnull arena) {
50-
// Special case unset any.
51-
if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE &&
52-
field_desc->message_type()->well_known_type() ==
53-
Descriptor::WELLKNOWNTYPE_ANY) {
54-
CEL_ASSIGN_OR_RETURN(bool present,
55-
struct_value.HasFieldByNumber(field_desc->number()));
56-
if (!present) {
57-
return NullValue();
58-
}
59-
}
60-
61-
return struct_value.GetFieldByNumber(field_desc->number(), descriptor_pool,
62-
message_factory, arena);
63-
}
64-
65-
} // namespace
66-
67-
absl::Status BindProtoToActivation(
68-
const Descriptor& descriptor, const StructValue& struct_value,
69-
BindProtoUnsetFieldBehavior unset_field_behavior,
70-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
71-
google::protobuf::MessageFactory* absl_nonnull message_factory,
72-
google::protobuf::Arena* absl_nonnull arena, Activation* absl_nonnull activation) {
73-
for (int i = 0; i < descriptor.field_count(); i++) {
74-
const google::protobuf::FieldDescriptor* field_desc = descriptor.field(i);
75-
CEL_ASSIGN_OR_RETURN(
76-
bool should_bind,
77-
ShouldBindField(field_desc, struct_value, unset_field_behavior));
78-
if (!should_bind) {
79-
continue;
80-
}
81-
82-
CEL_ASSIGN_OR_RETURN(
83-
Value field, GetFieldValue(field_desc, struct_value, descriptor_pool,
84-
message_factory, arena));
85-
86-
activation->InsertOrAssignValue(field_desc->name(), std::move(field));
87-
}
88-
89-
return absl::OkStatus();
90-
}
91-
92-
} // namespace cel::extensions::protobuf_internal

extensions/protobuf/bind_proto_to_activation.h

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,116 +15,24 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_
1616
#define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_
1717

18-
#include <type_traits>
19-
20-
#include "absl/base/nullability.h"
21-
#include "absl/status/status.h"
22-
#include "common/casting.h"
23-
#include "common/value.h"
24-
#include "extensions/protobuf/value.h"
25-
#include "internal/status_macros.h"
26-
#include "runtime/activation.h"
27-
#include "google/protobuf/arena.h"
28-
#include "google/protobuf/descriptor.h"
29-
#include "google/protobuf/message.h"
18+
#include "absl/base/attributes.h"
19+
#include "runtime/bind_proto_to_activation.h"
3020

3121
namespace cel::extensions {
3222

33-
// Option for handling unset fields on the context proto.
34-
enum class BindProtoUnsetFieldBehavior {
35-
// Bind the message defined default or zero value.
36-
kBindDefaultValue,
37-
// Skip binding unset fields, no value is bound for the corresponding
38-
// variable.
39-
kSkip
40-
};
23+
using BindProtoUnsetFieldBehavior ABSL_DEPRECATED(
24+
"Use cel::BindProtoUnsetFieldBehavior instead") =
25+
::cel::BindProtoUnsetFieldBehavior;
26+
27+
using ::cel::BindProtoToActivation;
28+
using ::cel::BindProtoViewToActivation;
4129

4230
namespace protobuf_internal {
4331

44-
// Implements binding provided the context message has already
45-
// been adapted to a suitable struct value.
46-
absl::Status BindProtoToActivation(
47-
const google::protobuf::Descriptor& descriptor, const StructValue& struct_value,
48-
BindProtoUnsetFieldBehavior unset_field_behavior,
49-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
50-
google::protobuf::MessageFactory* absl_nonnull message_factory,
51-
google::protobuf::Arena* absl_nonnull arena, Activation* absl_nonnull activation);
32+
using ::cel::runtime_internal::BindProtoToActivation;
5233

5334
} // namespace protobuf_internal
5435

55-
// Utility method, that takes a protobuf Message and interprets it as a
56-
// namespace, binding its fields to Activation. This is often referred to as a
57-
// context message.
58-
//
59-
// Field names and values become respective names and values of parameters
60-
// bound to the Activation object.
61-
// Example:
62-
// Assume we have a protobuf message of type:
63-
// message Person {
64-
// int age = 1;
65-
// string name = 2;
66-
// }
67-
//
68-
// The sample code snippet will look as follows:
69-
//
70-
// Person person;
71-
// person.set_name("John Doe");
72-
// person.age(42);
73-
//
74-
// CEL_RETURN_IF_ERROR(BindProtoToActivation(person, value_factory,
75-
// activation));
76-
//
77-
// After this snippet, activation will have two parameters bound:
78-
// "name", with string value of "John Doe"
79-
// "age", with int value of 42.
80-
//
81-
// The default behavior for unset fields is to skip them. E.g. if the name field
82-
// is not set on the Person message, it will not be bound in to the activation.
83-
// BindProtoUnsetFieldBehavior::kBindDefault, will bind the cc proto api default
84-
// for the field (either an explicit default value or a type specific default).
85-
//
86-
// For repeated fields, an unset field is bound as an empty list.
87-
template <typename T>
88-
absl::Status BindProtoToActivation(
89-
const T& context, BindProtoUnsetFieldBehavior unset_field_behavior,
90-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
91-
google::protobuf::MessageFactory* absl_nonnull message_factory,
92-
google::protobuf::Arena* absl_nonnull arena, Activation* absl_nonnull activation) {
93-
static_assert(std::is_base_of_v<google::protobuf::Message, T>);
94-
// TODO(uncreated-issue/68): for simplicity, just convert the whole message to a
95-
// struct value. For performance, may be better to convert members as needed.
96-
CEL_ASSIGN_OR_RETURN(
97-
Value parent,
98-
ProtoMessageToValue(context, descriptor_pool, message_factory, arena));
99-
100-
if (!InstanceOf<StructValue>(parent)) {
101-
return absl::InvalidArgumentError(
102-
absl::StrCat("context is a well-known type: ", context.GetTypeName()));
103-
}
104-
const StructValue& struct_value = Cast<StructValue>(parent);
105-
106-
const google::protobuf::Descriptor* descriptor = context.GetDescriptor();
107-
108-
if (descriptor == nullptr) {
109-
return absl::InvalidArgumentError(
110-
absl::StrCat("context missing descriptor: ", context.GetTypeName()));
111-
}
112-
113-
return protobuf_internal::BindProtoToActivation(
114-
*descriptor, struct_value, unset_field_behavior, descriptor_pool,
115-
message_factory, arena, activation);
116-
}
117-
template <typename T>
118-
absl::Status BindProtoToActivation(
119-
const T& context,
120-
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
121-
google::protobuf::MessageFactory* absl_nonnull message_factory,
122-
google::protobuf::Arena* absl_nonnull arena, Activation* absl_nonnull activation) {
123-
return BindProtoToActivation(context, BindProtoUnsetFieldBehavior::kSkip,
124-
descriptor_pool, message_factory, arena,
125-
activation);
126-
}
127-
12836
} // namespace cel::extensions
12937

13038
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_

runtime/BUILD

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,42 @@ cc_test(
110110
],
111111
)
112112

113+
cc_library(
114+
name = "bind_proto_to_activation",
115+
srcs = ["bind_proto_to_activation.cc"],
116+
hdrs = ["bind_proto_to_activation.h"],
117+
deps = [
118+
":activation",
119+
"//common:casting",
120+
"//common:value",
121+
"//internal:status_macros",
122+
"@com_google_absl//absl/base:nullability",
123+
"@com_google_absl//absl/status",
124+
"@com_google_absl//absl/status:statusor",
125+
"@com_google_absl//absl/strings",
126+
"@com_google_protobuf//:protobuf",
127+
],
128+
)
129+
130+
cc_test(
131+
name = "bind_proto_to_activation_test",
132+
srcs = ["bind_proto_to_activation_test.cc"],
133+
deps = [
134+
":activation",
135+
":bind_proto_to_activation",
136+
"//common:casting",
137+
"//common:value",
138+
"//common:value_testing",
139+
"//internal:testing",
140+
"@com_google_absl//absl/status",
141+
"@com_google_absl//absl/status:status_matchers",
142+
"@com_google_absl//absl/types:optional",
143+
"@com_google_cel_spec//proto/cel/expr/conformance/proto2:test_all_types_cc_proto",
144+
"@com_google_protobuf//:protobuf",
145+
"@com_google_protobuf//:wrappers_cc_proto",
146+
],
147+
)
148+
113149
cc_library(
114150
name = "register_function_helper",
115151
hdrs = ["register_function_helper.h"],

0 commit comments

Comments
 (0)