-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfield_access_impl.cc
More file actions
746 lines (632 loc) · 23.3 KB
/
Copy pathfield_access_impl.cc
File metadata and controls
746 lines (632 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "eval/public/structs/field_access_impl.h"
#include <cstdint>
#include <string>
#include <type_traits>
#include <utility>
#include "google/protobuf/any.pb.h"
#include "google/protobuf/struct.pb.h"
#include "google/protobuf/wrappers.pb.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/map_field.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/strings/substitute.h"
#include "absl/types/optional.h"
#include "eval/public/structs/cel_proto_wrap_util.h"
#include "internal/casts.h"
#include "internal/overflow.h"
namespace google::api::expr::runtime::internal {
namespace {
using ::google::protobuf::Arena;
using ::google::protobuf::FieldDescriptor;
using ::google::protobuf::MapValueConstRef;
using ::google::protobuf::Message;
using ::google::protobuf::Reflection;
// Singular message fields and repeated message fields have similar access model
// To provide common approach, we implement accessor classes, based on CRTP.
// FieldAccessor is CRTP base class, specifying Get.. method family.
template <class Derived>
class FieldAccessor {
public:
bool GetBool() const { return static_cast<const Derived*>(this)->GetBool(); }
int64_t GetInt32() const {
return static_cast<const Derived*>(this)->GetInt32();
}
uint64_t GetUInt32() const {
return static_cast<const Derived*>(this)->GetUInt32();
}
int64_t GetInt64() const {
return static_cast<const Derived*>(this)->GetInt64();
}
uint64_t GetUInt64() const {
return static_cast<const Derived*>(this)->GetUInt64();
}
double GetFloat() const {
return static_cast<const Derived*>(this)->GetFloat();
}
double GetDouble() const {
return static_cast<const Derived*>(this)->GetDouble();
}
absl::string_view GetString(std::string* buffer) const {
return static_cast<const Derived*>(this)->GetString(buffer);
}
const Message* GetMessage() const {
return static_cast<const Derived*>(this)->GetMessage();
}
int64_t GetEnumValue() const {
return static_cast<const Derived*>(this)->GetEnumValue();
}
// This method provides message field content, wrapped in CelValue.
// If value provided successfully, return a CelValue, otherwise returns a
// status with non-ok status code.
//
// arena Arena to use for allocations if needed.
absl::StatusOr<CelValue> CreateValueFromFieldAccessor(Arena* arena) {
switch (field_desc_->cpp_type()) {
case FieldDescriptor::CPPTYPE_BOOL: {
bool value = GetBool();
return CelValue::CreateBool(value);
}
case FieldDescriptor::CPPTYPE_INT32: {
int64_t value = GetInt32();
return CelValue::CreateInt64(value);
}
case FieldDescriptor::CPPTYPE_INT64: {
int64_t value = GetInt64();
return CelValue::CreateInt64(value);
}
case FieldDescriptor::CPPTYPE_UINT32: {
uint64_t value = GetUInt32();
return CelValue::CreateUint64(value);
}
case FieldDescriptor::CPPTYPE_UINT64: {
uint64_t value = GetUInt64();
return CelValue::CreateUint64(value);
}
case FieldDescriptor::CPPTYPE_FLOAT: {
double value = GetFloat();
return CelValue::CreateDouble(value);
}
case FieldDescriptor::CPPTYPE_DOUBLE: {
double value = GetDouble();
return CelValue::CreateDouble(value);
}
case FieldDescriptor::CPPTYPE_STRING: {
std::string buffer;
absl::string_view value = GetString(&buffer);
if (value.data() == buffer.data() && value.size() == buffer.size()) {
value = absl::string_view(
*google::protobuf::Arena::Create<std::string>(arena, std::move(buffer)));
}
switch (field_desc_->type()) {
case FieldDescriptor::TYPE_STRING:
return CelValue::CreateStringView(value);
case FieldDescriptor::TYPE_BYTES:
return CelValue::CreateBytesView(value);
default:
return absl::Status(absl::StatusCode::kInvalidArgument,
"Error handling C++ string conversion");
}
break;
}
case FieldDescriptor::CPPTYPE_MESSAGE: {
const google::protobuf::Message* msg_value = GetMessage();
return UnwrapMessageToValue(msg_value, protobuf_value_factory_, arena);
}
case FieldDescriptor::CPPTYPE_ENUM: {
int enum_value = GetEnumValue();
return CelValue::CreateInt64(enum_value);
}
default:
return absl::Status(absl::StatusCode::kInvalidArgument,
"Unhandled C++ type conversion");
}
return absl::Status(absl::StatusCode::kInvalidArgument,
"Unhandled C++ type conversion");
}
protected:
FieldAccessor(const Message* msg, const FieldDescriptor* field_desc,
const ProtobufValueFactory& protobuf_value_factory)
: msg_(msg),
field_desc_(field_desc),
protobuf_value_factory_(protobuf_value_factory) {}
const Message* msg_;
const FieldDescriptor* field_desc_;
const ProtobufValueFactory& protobuf_value_factory_;
};
const absl::flat_hash_set<std::string>& WellKnownWrapperTypes() {
static auto* wrapper_types = new absl::flat_hash_set<std::string>{
"google.protobuf.BoolValue", "google.protobuf.DoubleValue",
"google.protobuf.FloatValue", "google.protobuf.Int64Value",
"google.protobuf.Int32Value", "google.protobuf.UInt64Value",
"google.protobuf.UInt32Value", "google.protobuf.StringValue",
"google.protobuf.BytesValue",
};
return *wrapper_types;
}
bool IsWrapperType(const FieldDescriptor* field_descriptor) {
return WellKnownWrapperTypes().find(
field_descriptor->message_type()->full_name()) !=
WellKnownWrapperTypes().end();
}
// Accessor class, to work with singular fields
class ScalarFieldAccessor : public FieldAccessor<ScalarFieldAccessor> {
public:
ScalarFieldAccessor(const Message* msg, const FieldDescriptor* field_desc,
bool unset_wrapper_as_null,
const ProtobufValueFactory& factory)
: FieldAccessor(msg, field_desc, factory),
unset_wrapper_as_null_(unset_wrapper_as_null) {}
bool GetBool() const { return GetReflection()->GetBool(*msg_, field_desc_); }
int64_t GetInt32() const {
return GetReflection()->GetInt32(*msg_, field_desc_);
}
uint64_t GetUInt32() const {
return GetReflection()->GetUInt32(*msg_, field_desc_);
}
int64_t GetInt64() const {
return GetReflection()->GetInt64(*msg_, field_desc_);
}
uint64_t GetUInt64() const {
return GetReflection()->GetUInt64(*msg_, field_desc_);
}
double GetFloat() const {
return GetReflection()->GetFloat(*msg_, field_desc_);
}
double GetDouble() const {
return GetReflection()->GetDouble(*msg_, field_desc_);
}
absl::string_view GetString(std::string* buffer) const {
return GetReflection()->GetStringReference(*msg_, field_desc_, buffer);
}
const Message* GetMessage() const {
// Unset wrapper types have special semantics.
// If set, return the unwrapped value, else return 'null'.
if (unset_wrapper_as_null_ &&
!GetReflection()->HasField(*msg_, field_desc_) &&
IsWrapperType(field_desc_)) {
return nullptr;
}
return &GetReflection()->GetMessage(*msg_, field_desc_);
}
int64_t GetEnumValue() const {
return GetReflection()->GetEnumValue(*msg_, field_desc_);
}
const Reflection* GetReflection() const { return msg_->GetReflection(); }
private:
bool unset_wrapper_as_null_;
};
// Accessor class, to work with repeated fields.
class RepeatedFieldAccessor : public FieldAccessor<RepeatedFieldAccessor> {
public:
RepeatedFieldAccessor(const Message* msg, const FieldDescriptor* field_desc,
int index, const ProtobufValueFactory& factory)
: FieldAccessor(msg, field_desc, factory), index_(index) {}
bool GetBool() const {
return GetReflection()->GetRepeatedBool(*msg_, field_desc_, index_);
}
int64_t GetInt32() const {
return GetReflection()->GetRepeatedInt32(*msg_, field_desc_, index_);
}
uint64_t GetUInt32() const {
return GetReflection()->GetRepeatedUInt32(*msg_, field_desc_, index_);
}
int64_t GetInt64() const {
return GetReflection()->GetRepeatedInt64(*msg_, field_desc_, index_);
}
uint64_t GetUInt64() const {
return GetReflection()->GetRepeatedUInt64(*msg_, field_desc_, index_);
}
double GetFloat() const {
return GetReflection()->GetRepeatedFloat(*msg_, field_desc_, index_);
}
double GetDouble() const {
return GetReflection()->GetRepeatedDouble(*msg_, field_desc_, index_);
}
absl::string_view GetString(std::string* buffer) const {
return GetReflection()->GetRepeatedStringReference(*msg_, field_desc_,
index_, buffer);
}
const Message* GetMessage() const {
return &GetReflection()->GetRepeatedMessage(*msg_, field_desc_, index_);
}
int64_t GetEnumValue() const {
return GetReflection()->GetRepeatedEnumValue(*msg_, field_desc_, index_);
}
const Reflection* GetReflection() const { return msg_->GetReflection(); }
private:
int index_;
};
// Accessor class, to work with map values
class MapValueAccessor : public FieldAccessor<MapValueAccessor> {
public:
MapValueAccessor(const Message* msg, const FieldDescriptor* field_desc,
const MapValueConstRef* value_ref,
const ProtobufValueFactory& factory)
: FieldAccessor(msg, field_desc, factory), value_ref_(value_ref) {}
bool GetBool() const { return value_ref_->GetBoolValue(); }
int64_t GetInt32() const { return value_ref_->GetInt32Value(); }
uint64_t GetUInt32() const { return value_ref_->GetUInt32Value(); }
int64_t GetInt64() const { return value_ref_->GetInt64Value(); }
uint64_t GetUInt64() const { return value_ref_->GetUInt64Value(); }
double GetFloat() const { return value_ref_->GetFloatValue(); }
double GetDouble() const { return value_ref_->GetDoubleValue(); }
absl::string_view GetString(std::string* /*buffer*/) const {
return value_ref_->GetStringValue();
}
const Message* GetMessage() const { return &value_ref_->GetMessageValue(); }
int64_t GetEnumValue() const { return value_ref_->GetEnumValue(); }
const Reflection* GetReflection() const { return msg_->GetReflection(); }
private:
const MapValueConstRef* value_ref_;
};
// Singular message fields and repeated message fields have similar access model
// To provide common approach, we implement field setter classes, based on CRTP.
// FieldAccessor is CRTP base class, specifying Get.. method family.
template <class Derived>
class FieldSetter {
public:
bool AssignBool(const CelValue& cel_value) const {
bool value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetBool(value);
return true;
}
bool AssignInt32(const CelValue& cel_value) const {
int64_t value;
if (!cel_value.GetValue(&value)) {
return false;
}
absl::optional<int32_t> checked_cast =
cel::internal::CheckedInt64ToInt32(value);
if (!checked_cast) {
return false;
}
static_cast<const Derived*>(this)->SetInt32(*checked_cast);
return true;
}
bool AssignUInt32(const CelValue& cel_value) const {
uint64_t value;
if (!cel_value.GetValue(&value)) {
return false;
}
if (!cel::internal::CheckedUint64ToUint32(value)) {
return false;
}
static_cast<const Derived*>(this)->SetUInt32(value);
return true;
}
bool AssignInt64(const CelValue& cel_value) const {
int64_t value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetInt64(value);
return true;
}
bool AssignUInt64(const CelValue& cel_value) const {
uint64_t value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetUInt64(value);
return true;
}
bool AssignFloat(const CelValue& cel_value) const {
double value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetFloat(value);
return true;
}
bool AssignDouble(const CelValue& cel_value) const {
double value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetDouble(value);
return true;
}
bool AssignString(const CelValue& cel_value) const {
CelValue::StringHolder value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetString(value);
return true;
}
bool AssignBytes(const CelValue& cel_value) const {
CelValue::BytesHolder value;
if (!cel_value.GetValue(&value)) {
return false;
}
static_cast<const Derived*>(this)->SetBytes(value);
return true;
}
bool AssignEnum(const CelValue& cel_value) const {
int64_t value;
if (!cel_value.GetValue(&value)) {
return false;
}
if (!cel::internal::CheckedInt64ToInt32(value)) {
return false;
}
static_cast<const Derived*>(this)->SetEnum(value);
return true;
}
bool AssignMessage(const google::protobuf::Message* message) const {
return static_cast<const Derived*>(this)->SetMessage(message);
}
// This method provides message field content, wrapped in CelValue.
// If value provided successfully, returns Ok.
// arena Arena to use for allocations if needed.
// result pointer to object to store value in.
bool SetFieldFromCelValue(const CelValue& value) {
switch (field_desc_->cpp_type()) {
case FieldDescriptor::CPPTYPE_BOOL: {
return AssignBool(value);
}
case FieldDescriptor::CPPTYPE_INT32: {
return AssignInt32(value);
}
case FieldDescriptor::CPPTYPE_INT64: {
return AssignInt64(value);
}
case FieldDescriptor::CPPTYPE_UINT32: {
return AssignUInt32(value);
}
case FieldDescriptor::CPPTYPE_UINT64: {
return AssignUInt64(value);
}
case FieldDescriptor::CPPTYPE_FLOAT: {
return AssignFloat(value);
}
case FieldDescriptor::CPPTYPE_DOUBLE: {
return AssignDouble(value);
}
case FieldDescriptor::CPPTYPE_STRING: {
switch (field_desc_->type()) {
case FieldDescriptor::TYPE_STRING:
return AssignString(value);
case FieldDescriptor::TYPE_BYTES:
return AssignBytes(value);
default:
return false;
}
break;
}
case FieldDescriptor::CPPTYPE_MESSAGE: {
// When the field is a message, it might be a well-known type with a
// non-proto representation that requires special handling before it
// can be set on the field.
const google::protobuf::Message* wrapped_value = MaybeWrapValueToMessage(
field_desc_->message_type(),
msg_->GetReflection()->GetMessageFactory(), value, arena_);
if (wrapped_value == nullptr) {
// It we aren't unboxing to a protobuf null representation, setting a
// field to null is a no-op.
if (value.IsNull()) {
return true;
}
if (CelValue::MessageWrapper wrapper;
value.GetValue(&wrapper) && wrapper.HasFullProto()) {
wrapped_value =
static_cast<const google::protobuf::Message*>(wrapper.message_ptr());
} else {
return false;
}
}
return AssignMessage(wrapped_value);
}
case FieldDescriptor::CPPTYPE_ENUM: {
return AssignEnum(value);
}
default:
return false;
}
return true;
}
protected:
FieldSetter(Message* msg, const FieldDescriptor* field_desc, Arena* arena)
: msg_(msg), field_desc_(field_desc), arena_(arena) {}
Message* msg_;
const FieldDescriptor* field_desc_;
Arena* arena_;
};
bool MergeFromWithSerializeFallback(const google::protobuf::Message& value,
google::protobuf::Message& field) {
if (field.GetDescriptor() == value.GetDescriptor()) {
field.MergeFrom(value);
return true;
}
// TODO: this indicates means we're mixing dynamic messages with
// generated messages. This is expected for WKTs where CEL explicitly requires
// wire format compatibility, but this may not be the expected behavior for
// other types.
return field.MergeFromString(value.SerializeAsString());
}
// Accessor class, to work with singular fields
class ScalarFieldSetter : public FieldSetter<ScalarFieldSetter> {
public:
ScalarFieldSetter(Message* msg, const FieldDescriptor* field_desc,
Arena* arena)
: FieldSetter(msg, field_desc, arena) {}
bool SetBool(bool value) const {
GetReflection()->SetBool(msg_, field_desc_, value);
return true;
}
bool SetInt32(int32_t value) const {
GetReflection()->SetInt32(msg_, field_desc_, value);
return true;
}
bool SetUInt32(uint32_t value) const {
GetReflection()->SetUInt32(msg_, field_desc_, value);
return true;
}
bool SetInt64(int64_t value) const {
GetReflection()->SetInt64(msg_, field_desc_, value);
return true;
}
bool SetUInt64(uint64_t value) const {
GetReflection()->SetUInt64(msg_, field_desc_, value);
return true;
}
bool SetFloat(float value) const {
GetReflection()->SetFloat(msg_, field_desc_, value);
return true;
}
bool SetDouble(double value) const {
GetReflection()->SetDouble(msg_, field_desc_, value);
return true;
}
bool SetString(CelValue::StringHolder value) const {
GetReflection()->SetString(msg_, field_desc_, std::string(value.value()));
return true;
}
bool SetBytes(CelValue::BytesHolder value) const {
GetReflection()->SetString(msg_, field_desc_, std::string(value.value()));
return true;
}
bool SetMessage(const Message* value) const {
if (!value) {
ABSL_LOG(ERROR) << "Message is NULL";
return true;
}
if (value->GetDescriptor()->full_name() ==
field_desc_->message_type()->full_name()) {
auto* assignable_field_msg =
GetReflection()->MutableMessage(msg_, field_desc_);
return MergeFromWithSerializeFallback(*value, *assignable_field_msg);
}
return false;
}
bool SetEnum(const int64_t value) const {
GetReflection()->SetEnumValue(msg_, field_desc_, value);
return true;
}
const Reflection* GetReflection() const { return msg_->GetReflection(); }
};
// Appender class, to work with repeated fields
class RepeatedFieldSetter : public FieldSetter<RepeatedFieldSetter> {
public:
RepeatedFieldSetter(Message* msg, const FieldDescriptor* field_desc,
Arena* arena)
: FieldSetter(msg, field_desc, arena) {}
bool SetBool(bool value) const {
GetReflection()->AddBool(msg_, field_desc_, value);
return true;
}
bool SetInt32(int32_t value) const {
GetReflection()->AddInt32(msg_, field_desc_, value);
return true;
}
bool SetUInt32(uint32_t value) const {
GetReflection()->AddUInt32(msg_, field_desc_, value);
return true;
}
bool SetInt64(int64_t value) const {
GetReflection()->AddInt64(msg_, field_desc_, value);
return true;
}
bool SetUInt64(uint64_t value) const {
GetReflection()->AddUInt64(msg_, field_desc_, value);
return true;
}
bool SetFloat(float value) const {
GetReflection()->AddFloat(msg_, field_desc_, value);
return true;
}
bool SetDouble(double value) const {
GetReflection()->AddDouble(msg_, field_desc_, value);
return true;
}
bool SetString(CelValue::StringHolder value) const {
GetReflection()->AddString(msg_, field_desc_, std::string(value.value()));
return true;
}
bool SetBytes(CelValue::BytesHolder value) const {
GetReflection()->AddString(msg_, field_desc_, std::string(value.value()));
return true;
}
bool SetMessage(const Message* value) const {
if (!value) return true;
if (value->GetDescriptor()->full_name() !=
field_desc_->message_type()->full_name()) {
return false;
}
auto* assignable_message = GetReflection()->AddMessage(msg_, field_desc_);
return MergeFromWithSerializeFallback(*value, *assignable_message);
}
bool SetEnum(const int64_t value) const {
GetReflection()->AddEnumValue(msg_, field_desc_, value);
return true;
}
private:
const Reflection* GetReflection() const { return msg_->GetReflection(); }
};
} // namespace
absl::StatusOr<CelValue> CreateValueFromSingleField(
const google::protobuf::Message* msg, const FieldDescriptor* desc,
ProtoWrapperTypeOptions options, const ProtobufValueFactory& factory,
google::protobuf::Arena* arena) {
ScalarFieldAccessor accessor(
msg, desc, (options == ProtoWrapperTypeOptions::kUnsetNull), factory);
return accessor.CreateValueFromFieldAccessor(arena);
}
absl::StatusOr<CelValue> CreateValueFromRepeatedField(
const google::protobuf::Message* msg, const FieldDescriptor* desc, int index,
const ProtobufValueFactory& factory, google::protobuf::Arena* arena) {
RepeatedFieldAccessor accessor(msg, desc, index, factory);
return accessor.CreateValueFromFieldAccessor(arena);
}
absl::StatusOr<CelValue> CreateValueFromMapValue(
const google::protobuf::Message* msg, const FieldDescriptor* desc,
const MapValueConstRef* value_ref, const ProtobufValueFactory& factory,
google::protobuf::Arena* arena) {
MapValueAccessor accessor(msg, desc, value_ref, factory);
return accessor.CreateValueFromFieldAccessor(arena);
}
absl::Status SetValueToSingleField(const CelValue& value,
const FieldDescriptor* desc, Message* msg,
Arena* arena) {
ScalarFieldSetter setter(msg, desc, arena);
return (setter.SetFieldFromCelValue(value))
? absl::OkStatus()
: absl::InvalidArgumentError(absl::Substitute(
"Could not assign supplied argument to message \"$0\" field "
"\"$1\" of type $2: value type \"$3\"",
msg->GetDescriptor()->name(), desc->name(),
desc->type_name(), CelValue::TypeName(value.type())));
}
absl::Status AddValueToRepeatedField(const CelValue& value,
const FieldDescriptor* desc, Message* msg,
Arena* arena) {
RepeatedFieldSetter setter(msg, desc, arena);
return (setter.SetFieldFromCelValue(value))
? absl::OkStatus()
: absl::InvalidArgumentError(absl::Substitute(
"Could not add supplied argument to message \"$0\" field "
"\"$1\" of type $2: value type \"$3\"",
msg->GetDescriptor()->name(), desc->name(),
desc->type_name(), CelValue::TypeName(value.type())));
}
} // namespace google::api::expr::runtime::internal