|
15 | 15 | #ifndef THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_ |
16 | 16 | #define THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_ |
17 | 17 |
|
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" |
30 | 20 |
|
31 | 21 | namespace cel::extensions { |
32 | 22 |
|
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; |
41 | 29 |
|
42 | 30 | namespace protobuf_internal { |
43 | 31 |
|
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; |
52 | 33 |
|
53 | 34 | } // namespace protobuf_internal |
54 | 35 |
|
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 | | - |
128 | 36 | } // namespace cel::extensions |
129 | 37 |
|
130 | 38 | #endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_PROTOBUF_BIND_PROTO_TO_ACTIVATION_H_ |
0 commit comments