| 36 | } // namespace |
| 37 | |
| 38 | absl::Status BindProtoToActivation(const Message* message, Arena* arena, |
| 39 | Activation* activation, |
| 40 | ProtoUnsetFieldOptions options) { |
| 41 | // If we need to bind any types that are backed by an arena allocation, we |
| 42 | // will cause a memory leak. |
| 43 | if (arena == nullptr) { |
| 44 | return absl::InvalidArgumentError( |
| 45 | "arena must not be null for BindProtoToActivation."); |
| 46 | } |
| 47 | |
| 48 | // TODO(issues/24): Improve the utilities to bind dynamic values as well. |
| 49 | const Descriptor* desc = message->GetDescriptor(); |
| 50 | const google::protobuf::Reflection* reflection = message->GetReflection(); |
| 51 | for (int i = 0; i < desc->field_count(); i++) { |
| 52 | CelValue value; |
| 53 | const FieldDescriptor* field_desc = desc->field(i); |
| 54 | |
| 55 | if (options == ProtoUnsetFieldOptions::kSkip) { |
| 56 | if (!field_desc->is_repeated() && |
| 57 | !reflection->HasField(*message, field_desc)) { |
| 58 | continue; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | auto status = CreateValueFromField(message, field_desc, arena, &value); |
| 63 | if (!status.ok()) { |
| 64 | return status; |
| 65 | } |
| 66 | |
| 67 | activation->InsertValue(field_desc->name(), value); |
| 68 | } |
| 69 | |
| 70 | return absl::OkStatus(); |
| 71 | } |
| 72 | |
| 73 | } // namespace runtime |
| 74 | } // namespace expr |