-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmap_value.cc
More file actions
277 lines (231 loc) · 9.3 KB
/
Copy pathmap_value.cc
File metadata and controls
277 lines (231 loc) · 9.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
// Copyright 2022 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 "base/values/map_value.h"
#include <cstddef>
#include <string>
#include <utility>
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/optional.h"
#include "base/handle.h"
#include "base/internal/data.h"
#include "base/types/map_type.h"
#include "base/value.h"
#include "base/value_factory.h"
#include "base/values/list_value.h"
#include "internal/rtti.h"
#include "internal/status_macros.h"
namespace cel {
CEL_INTERNAL_VALUE_IMPL(MapValue);
#define CEL_INTERNAL_MAP_VALUE_DISPATCH(method, ...) \
base_internal::Metadata::IsStoredInline(*this) \
? static_cast<const base_internal::LegacyMapValue&>(*this).method( \
__VA_ARGS__) \
: static_cast<const base_internal::AbstractMapValue&>(*this).method( \
__VA_ARGS__)
Handle<MapType> MapValue::type() const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(type);
}
std::string MapValue::DebugString() const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(DebugString);
}
size_t MapValue::size() const { return CEL_INTERNAL_MAP_VALUE_DISPATCH(size); }
bool MapValue::empty() const { return CEL_INTERNAL_MAP_VALUE_DISPATCH(empty); }
absl::StatusOr<absl::optional<Handle<Value>>> MapValue::Get(
const GetContext& context, const Handle<Value>& key) const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(Get, context, key);
}
absl::StatusOr<bool> MapValue::Has(const HasContext& context,
const Handle<Value>& key) const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(Has, context, key);
}
absl::StatusOr<Handle<ListValue>> MapValue::ListKeys(
const ListKeysContext& context) const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(ListKeys, context);
}
absl::StatusOr<UniqueRef<MapValue::Iterator>> MapValue::NewIterator(
MemoryManager& memory_manager) const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(NewIterator, memory_manager);
}
internal::TypeInfo MapValue::TypeId() const {
return CEL_INTERNAL_MAP_VALUE_DISPATCH(TypeId);
}
#undef CEL_INTERNAL_MAP_VALUE_DISPATCH
absl::StatusOr<Handle<Value>> MapValue::Iterator::NextKey(
const MapValue::GetContext& context) {
CEL_ASSIGN_OR_RETURN(auto entry, Next(context));
return std::move(entry.key);
}
absl::StatusOr<Handle<Value>> MapValue::Iterator::NextValue(
const MapValue::GetContext& context) {
CEL_ASSIGN_OR_RETURN(auto entry, Next(context));
return std::move(entry.value);
}
namespace base_internal {
namespace {
class LegacyMapValueIterator final : public MapValue::Iterator {
public:
explicit LegacyMapValueIterator(uintptr_t impl) : impl_(impl) {}
bool HasNext() override {
if (ABSL_PREDICT_FALSE(!keys_iterator_.has_value())) {
// First call.
return !LegacyMapValueEmpty(impl_);
}
return (*keys_iterator_)->HasNext();
}
absl::StatusOr<Entry> Next(const MapValue::GetContext& context) override {
CEL_RETURN_IF_ERROR(OnNext(context.value_factory()));
CEL_ASSIGN_OR_RETURN(
auto key,
(*keys_iterator_)
->NextValue(ListValue::GetContext(context.value_factory())));
CEL_ASSIGN_OR_RETURN(
auto value, LegacyMapValueGet(impl_, context.value_factory(), key));
if (ABSL_PREDICT_FALSE(!value.has_value())) {
// Something is seriously wrong. The list of keys from the map is not
// consistent with what the map believes is set.
return absl::InternalError(
"inconsistency between list of map keys and map");
}
return Entry(std::move(key), std::move(value).value());
}
absl::StatusOr<Handle<Value>> NextKey(
const MapValue::GetContext& context) override {
CEL_RETURN_IF_ERROR(OnNext(context.value_factory()));
CEL_ASSIGN_OR_RETURN(
auto key,
(*keys_iterator_)
->NextValue(ListValue::GetContext(context.value_factory())));
return key;
}
absl::StatusOr<Handle<Value>> NextValue(
const MapValue::GetContext& context) override {
CEL_RETURN_IF_ERROR(OnNext(context.value_factory()));
CEL_ASSIGN_OR_RETURN(
auto key,
(*keys_iterator_)
->NextValue(ListValue::GetContext(context.value_factory())));
CEL_ASSIGN_OR_RETURN(
auto value, LegacyMapValueGet(impl_, context.value_factory(), key));
if (ABSL_PREDICT_FALSE(!value.has_value())) {
// Something is seriously wrong. The list of keys from the map is not
// consistent with what the map believes is set.
return absl::InternalError(
"inconsistency between list of map keys and map");
}
return std::move(value).value();
}
private:
absl::Status OnNext(ValueFactory& value_factory) {
if (ABSL_PREDICT_FALSE(!keys_iterator_.has_value())) {
CEL_ASSIGN_OR_RETURN(keys_, LegacyMapValueListKeys(impl_, value_factory));
CEL_ASSIGN_OR_RETURN(keys_iterator_,
keys_->NewIterator(value_factory.memory_manager()));
ABSL_CHECK((*keys_iterator_)->HasNext()); // Crash OK
}
return absl::OkStatus();
}
const uintptr_t impl_;
Handle<ListValue> keys_;
absl::optional<UniqueRef<ListValue::Iterator>> keys_iterator_;
};
class AbstractMapValueIterator final : public MapValue::Iterator {
public:
explicit AbstractMapValueIterator(const AbstractMapValue* value)
: value_(value) {}
bool HasNext() override {
if (ABSL_PREDICT_FALSE(!keys_iterator_.has_value())) {
// First call.
return !value_->empty();
}
return (*keys_iterator_)->HasNext();
}
absl::StatusOr<Entry> Next(const MapValue::GetContext& context) override {
CEL_RETURN_IF_ERROR(OnNext(context.value_factory()));
CEL_ASSIGN_OR_RETURN(
auto key,
(*keys_iterator_)
->NextValue(ListValue::GetContext(context.value_factory())));
CEL_ASSIGN_OR_RETURN(auto value, value_->Get(context, key));
if (ABSL_PREDICT_FALSE(!value.has_value())) {
// Something is seriously wrong. The list of keys from the map is not
// consistent with what the map believes is set.
return absl::InternalError(
"inconsistency between list of map keys and map");
}
return Entry(std::move(key), std::move(value).value());
}
absl::StatusOr<Handle<Value>> NextKey(
const MapValue::GetContext& context) override {
CEL_RETURN_IF_ERROR(OnNext(context.value_factory()));
CEL_ASSIGN_OR_RETURN(
auto key,
(*keys_iterator_)
->NextValue(ListValue::GetContext(context.value_factory())));
return key;
}
private:
absl::Status OnNext(ValueFactory& value_factory) {
if (ABSL_PREDICT_FALSE(!keys_iterator_.has_value())) {
CEL_ASSIGN_OR_RETURN(
keys_, value_->ListKeys(MapValue::ListKeysContext(value_factory)));
CEL_ASSIGN_OR_RETURN(keys_iterator_,
keys_->NewIterator(value_factory.memory_manager()));
ABSL_CHECK((*keys_iterator_)->HasNext()); // Crash OK
}
return absl::OkStatus();
}
const AbstractMapValue* const value_;
Handle<ListValue> keys_;
absl::optional<UniqueRef<ListValue::Iterator>> keys_iterator_;
};
} // namespace
Handle<MapType> LegacyMapValue::type() const {
return HandleFactory<MapType>::Make<LegacyMapType>();
}
std::string LegacyMapValue::DebugString() const { return "map"; }
size_t LegacyMapValue::size() const { return LegacyMapValueSize(impl_); }
bool LegacyMapValue::empty() const { return LegacyMapValueEmpty(impl_); }
absl::StatusOr<absl::optional<Handle<Value>>> LegacyMapValue::Get(
const GetContext& context, const Handle<Value>& key) const {
return LegacyMapValueGet(impl_, context.value_factory(), key);
}
absl::StatusOr<bool> LegacyMapValue::Has(const HasContext& context,
const Handle<Value>& key) const {
static_cast<void>(context);
return LegacyMapValueHas(impl_, key);
}
absl::StatusOr<Handle<ListValue>> LegacyMapValue::ListKeys(
const ListKeysContext& context) const {
return LegacyMapValueListKeys(impl_, context.value_factory());
}
absl::StatusOr<UniqueRef<MapValue::Iterator>> LegacyMapValue::NewIterator(
MemoryManager& memory_manager) const {
return MakeUnique<LegacyMapValueIterator>(memory_manager, impl_);
}
AbstractMapValue::AbstractMapValue(Handle<MapType> type)
: HeapData(kKind), type_(std::move(type)) {
// Ensure `Value*` and `HeapData*` are not thunked.
ABSL_ASSERT(reinterpret_cast<uintptr_t>(static_cast<Value*>(this)) ==
reinterpret_cast<uintptr_t>(static_cast<HeapData*>(this)));
}
absl::StatusOr<UniqueRef<MapValue::Iterator>> AbstractMapValue::NewIterator(
MemoryManager& memory_manager) const {
return MakeUnique<AbstractMapValueIterator>(memory_manager, this);
}
} // namespace base_internal
} // namespace cel