-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcreate_struct_step.cc
More file actions
199 lines (161 loc) · 6.73 KB
/
Copy pathcreate_struct_step.cc
File metadata and controls
199 lines (161 loc) · 6.73 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
#include "eval/eval/create_struct_step.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "eval/eval/expression_step_base.h"
#include "eval/internal/interop.h"
#include "eval/public/cel_value.h"
#include "eval/public/containers/container_backed_map_impl.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/status_macros.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::Handle;
using ::cel::Value;
using ::cel::interop_internal::CreateErrorValueFromView;
using ::cel::interop_internal::CreateLegacyMapValue;
using ::cel::interop_internal::CreateUnknownValueFromView;
using ::cel::interop_internal::LegacyValueToModernValueOrDie;
class CreateStructStepForMessage final : public ExpressionStepBase {
public:
struct FieldEntry {
std::string field_name;
};
CreateStructStepForMessage(int64_t expr_id,
const LegacyTypeMutationApis* type_adapter,
std::vector<FieldEntry> entries)
: ExpressionStepBase(expr_id),
type_adapter_(type_adapter),
entries_(std::move(entries)) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
absl::StatusOr<Handle<Value>> DoEvaluate(ExecutionFrame* frame) const;
const LegacyTypeMutationApis* type_adapter_;
std::vector<FieldEntry> entries_;
};
class CreateStructStepForMap final : public ExpressionStepBase {
public:
CreateStructStepForMap(int64_t expr_id, size_t entry_count)
: ExpressionStepBase(expr_id), entry_count_(entry_count) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
absl::StatusOr<Handle<Value>> DoEvaluate(ExecutionFrame* frame) const;
size_t entry_count_;
};
absl::StatusOr<Handle<Value>> CreateStructStepForMessage::DoEvaluate(
ExecutionFrame* frame) const {
int entries_size = entries_.size();
auto args = frame->value_stack().GetSpan(entries_size);
if (frame->enable_unknowns()) {
auto unknown_set = frame->attribute_utility().MergeUnknowns(
args, frame->value_stack().GetAttributeSpan(entries_size),
/*initial_set=*/nullptr,
/*use_partial=*/true);
if (unknown_set != nullptr) {
return CreateUnknownValueFromView(unknown_set);
}
}
// TODO(issues/5): switch to new cel::StructValue in phase 2
CEL_ASSIGN_OR_RETURN(MessageWrapper::Builder instance,
type_adapter_->NewInstance(frame->memory_manager()));
int index = 0;
for (const auto& entry : entries_) {
const CelValue& arg = cel::interop_internal::ModernValueToLegacyValueOrDie(
frame->memory_manager(), args[index++]);
CEL_RETURN_IF_ERROR(type_adapter_->SetField(
entry.field_name, arg, frame->memory_manager(), instance));
}
CEL_ASSIGN_OR_RETURN(auto result, type_adapter_->AdaptFromWellKnownType(
frame->memory_manager(), instance));
return LegacyValueToModernValueOrDie(frame->memory_manager(), result);
}
absl::Status CreateStructStepForMessage::Evaluate(ExecutionFrame* frame) const {
if (frame->value_stack().size() < entries_.size()) {
return absl::InternalError("CreateStructStepForMessage: stack underflow");
}
Handle<Value> result;
auto status_or_result = DoEvaluate(frame);
if (status_or_result.ok()) {
result = std::move(status_or_result).value();
} else {
result = CreateErrorValueFromView(google::protobuf::Arena::Create<absl::Status>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(
frame->memory_manager()),
status_or_result.status()));
}
frame->value_stack().Pop(entries_.size());
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
absl::StatusOr<Handle<Value>> CreateStructStepForMap::DoEvaluate(
ExecutionFrame* frame) const {
auto args = frame->value_stack().GetSpan(2 * entry_count_);
if (frame->enable_unknowns()) {
const UnknownSet* unknown_set = frame->attribute_utility().MergeUnknowns(
args, frame->value_stack().GetAttributeSpan(args.size()),
/*initial_set=*/nullptr, true);
if (unknown_set != nullptr) {
return CreateUnknownValueFromView(unknown_set);
}
}
// TODO(issues/5): switch to new cel::MapValue in phase 2
auto* map_builder = google::protobuf::Arena::Create<CelMapBuilder>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(
frame->memory_manager()));
for (size_t i = 0; i < entry_count_; i += 1) {
int map_key_index = 2 * i;
int map_value_index = map_key_index + 1;
const CelValue& map_key =
cel::interop_internal::ModernValueToLegacyValueOrDie(
frame->memory_manager(), args[map_key_index]);
CEL_RETURN_IF_ERROR(CelValue::CheckMapKeyType(map_key));
auto key_status = map_builder->Add(
map_key, cel::interop_internal::ModernValueToLegacyValueOrDie(
frame->memory_manager(), args[map_value_index]));
if (!key_status.ok()) {
return CreateErrorValueFromView(google::protobuf::Arena::Create<absl::Status>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(
frame->memory_manager()),
key_status));
}
}
return CreateLegacyMapValue(map_builder);
}
absl::Status CreateStructStepForMap::Evaluate(ExecutionFrame* frame) const {
if (frame->value_stack().size() < 2 * entry_count_) {
return absl::InternalError("CreateStructStepForMap: stack underflow");
}
CEL_ASSIGN_OR_RETURN(auto result, DoEvaluate(frame));
frame->value_stack().Pop(2 * entry_count_);
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateCreateStructStep(
const cel::ast::internal::CreateStruct& create_struct_expr,
const LegacyTypeMutationApis* type_adapter, int64_t expr_id) {
if (type_adapter != nullptr) {
std::vector<CreateStructStepForMessage::FieldEntry> entries;
for (const auto& entry : create_struct_expr.entries()) {
if (!type_adapter->DefinesField(entry.field_key())) {
return absl::InvalidArgumentError(absl::StrCat(
"Invalid message creation: field '", entry.field_key(),
"' not found in '", create_struct_expr.message_name(), "'"));
}
entries.push_back({entry.field_key()});
}
return std::make_unique<CreateStructStepForMessage>(expr_id, type_adapter,
std::move(entries));
} else {
// Make map-creating step.
return std::make_unique<CreateStructStepForMap>(
expr_id, create_struct_expr.entries().size());
}
}
} // namespace google::api::expr::runtime