forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatbuffers_backed_impl.cc
More file actions
310 lines (292 loc) · 11.5 KB
/
Copy pathflatbuffers_backed_impl.cc
File metadata and controls
310 lines (292 loc) · 11.5 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
#include "tools/flatbuffers_backed_impl.h"
#include <algorithm>
#include "flatbuffers/flatbuffers.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
CelValue CreateValue(int64_t value) { return CelValue::CreateInt64(value); }
CelValue CreateValue(uint64_t value) { return CelValue::CreateUint64(value); }
CelValue CreateValue(double value) { return CelValue::CreateDouble(value); }
CelValue CreateValue(bool value) { return CelValue::CreateBool(value); }
template <typename T, typename U>
class FlatBuffersListImpl : public CelList {
public:
FlatBuffersListImpl(const flatbuffers::Table& table,
const reflection::Field& field)
: list_(table.GetPointer<const flatbuffers::Vector<T>*>(field.offset())) {
}
int size() const override { return list_ ? list_->size() : 0; }
CelValue operator[](int index) const override {
return CreateValue(static_cast<U>(list_->Get(index)));
}
private:
const flatbuffers::Vector<T>* list_;
};
class StringListImpl : public CelList {
public:
StringListImpl(
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>* list)
: list_(list) {}
int size() const override { return list_ ? list_->size() : 0; }
CelValue operator[](int index) const override {
auto value = list_->Get(index);
return CelValue::CreateStringView(
absl::string_view(value->c_str(), value->size()));
}
private:
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>* list_;
};
class ObjectListImpl : public CelList {
public:
ObjectListImpl(
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::Table>>* list,
const reflection::Schema& schema, const reflection::Object& object,
google::protobuf::Arena* arena)
: arena_(arena), list_(list), schema_(schema), object_(object) {}
int size() const override { return list_ ? list_->size() : 0; }
CelValue operator[](int index) const override {
auto value = list_->Get(index);
return CelValue::CreateMap(google::protobuf::Arena::Create<FlatBuffersMapImpl>(
arena_, *value, schema_, object_, arena_));
}
private:
google::protobuf::Arena* arena_;
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::Table>>* list_;
const reflection::Schema& schema_;
const reflection::Object& object_;
};
class ObjectStringIndexedMapImpl : public CelMap {
public:
ObjectStringIndexedMapImpl(
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::Table>>* list,
const reflection::Schema& schema, const reflection::Object& object,
const reflection::Field& index, google::protobuf::Arena* arena)
: arena_(arena),
list_(list),
schema_(schema),
object_(object),
index_(index) {
keys_.parent_ = this;
}
int size() const override { return list_ ? list_->size() : 0; }
absl::optional<CelValue> operator[](CelValue cel_key) const override {
if (!cel_key.IsString()) {
return {};
}
const absl::string_view key = cel_key.StringOrDie().value();
const auto it = std::lower_bound(
list_->begin(), list_->end(), key,
[this](const flatbuffers::Table* t, const absl::string_view key) {
auto value = flatbuffers::GetFieldS(*t, index_);
auto sv = value ? absl::string_view(value->c_str(), value->size())
: absl::string_view();
return sv < key;
});
if (it != list_->end()) {
auto value = flatbuffers::GetFieldS(**it, index_);
auto sv = value ? absl::string_view(value->c_str(), value->size())
: absl::string_view();
if (sv == key) {
return CelValue::CreateMap(google::protobuf::Arena::Create<FlatBuffersMapImpl>(
arena_, **it, schema_, object_, arena_));
}
}
return {};
}
const CelList* ListKeys() const override { return &keys_; }
private:
struct KeyList : public CelList {
int size() const override { return parent_->size(); }
CelValue operator[](int index) const override {
auto value = flatbuffers::GetFieldS(*(parent_->list_->Get(index)),
parent_->index_);
if (value == nullptr) {
return CelValue::CreateStringView(absl::string_view());
}
return CelValue::CreateStringView(
absl::string_view(value->c_str(), value->size()));
}
ObjectStringIndexedMapImpl* parent_;
};
google::protobuf::Arena* arena_;
const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::Table>>* list_;
const reflection::Schema& schema_;
const reflection::Object& object_;
const reflection::Field& index_;
KeyList keys_;
};
// Detects a "key" field of the type string.
const reflection::Field* findStringKeyField(const reflection::Object& object) {
for (const auto field : *object.fields()) {
if (field->key() && field->type()->base_type() == reflection::String) {
return field;
}
}
return nullptr;
}
} // namespace
absl::optional<CelValue> FlatBuffersMapImpl::operator[](
CelValue cel_key) const {
if (!cel_key.IsString()) {
return {};
}
auto field = keys_.fields_->LookupByKey(cel_key.StringOrDie().value().data());
if (field == nullptr) {
return {};
}
switch (field->type()->base_type()) {
case reflection::Byte:
return CelValue::CreateInt64(
flatbuffers::GetFieldI<int8_t>(table_, *field));
case reflection::Short:
return CelValue::CreateInt64(
flatbuffers::GetFieldI<int16_t>(table_, *field));
case reflection::Int:
return CelValue::CreateInt64(
flatbuffers::GetFieldI<int32_t>(table_, *field));
case reflection::Long:
return CelValue::CreateInt64(
flatbuffers::GetFieldI<int64_t>(table_, *field));
case reflection::UByte:
return CelValue::CreateUint64(
flatbuffers::GetFieldI<uint8_t>(table_, *field));
case reflection::UShort:
return CelValue::CreateUint64(
flatbuffers::GetFieldI<uint16_t>(table_, *field));
case reflection::UInt:
return CelValue::CreateUint64(
flatbuffers::GetFieldI<uint32_t>(table_, *field));
case reflection::ULong:
return CelValue::CreateUint64(
flatbuffers::GetFieldI<uint64_t>(table_, *field));
case reflection::Float:
return CelValue::CreateDouble(
flatbuffers::GetFieldF<float>(table_, *field));
case reflection::Double:
return CelValue::CreateDouble(
flatbuffers::GetFieldF<double>(table_, *field));
case reflection::Bool:
return CelValue::CreateBool(
flatbuffers::GetFieldI<int8_t>(table_, *field));
case reflection::String: {
auto value = flatbuffers::GetFieldS(table_, *field);
if (value == nullptr) {
return CelValue::CreateStringView(absl::string_view());
}
return CelValue::CreateStringView(
absl::string_view(value->c_str(), value->size()));
}
case reflection::Obj: {
const auto* field_schema = schema_.objects()->Get(field->type()->index());
const auto* field_table = flatbuffers::GetFieldT(table_, *field);
if (field_table == nullptr) {
return CelValue::CreateNull();
}
if (field_schema) {
return CelValue::CreateMap(google::protobuf::Arena::Create<FlatBuffersMapImpl>(
arena_, *field_table, schema_, *field_schema, arena_));
}
break;
}
case reflection::Vector: {
switch (field->type()->element()) {
case reflection::Byte:
case reflection::UByte: {
const auto* field_table = flatbuffers::GetFieldAnyV(table_, *field);
if (field_table == nullptr) {
return CelValue::CreateBytesView(absl::string_view());
}
return CelValue::CreateBytesView(absl::string_view(
reinterpret_cast<const char*>(field_table->Data()),
field_table->size()));
}
case reflection::Short:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<int16_t, int64_t>>(
arena_, table_, *field));
case reflection::Int:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<int32_t, int64_t>>(
arena_, table_, *field));
case reflection::Long:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<int64_t, int64_t>>(
arena_, table_, *field));
case reflection::UShort:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<uint16_t, uint64_t>>(
arena_, table_, *field));
case reflection::UInt:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<uint32_t, uint64_t>>(
arena_, table_, *field));
case reflection::ULong:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<uint64_t, uint64_t>>(
arena_, table_, *field));
case reflection::Float:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<float, double>>(
arena_, table_, *field));
case reflection::Double:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<double, double>>(
arena_, table_, *field));
case reflection::Bool:
return CelValue::CreateList(
google::protobuf::Arena::Create<FlatBuffersListImpl<uint8_t, bool>>(
arena_, table_, *field));
case reflection::String:
return CelValue::CreateList(google::protobuf::Arena::Create<StringListImpl>(
arena_, table_.GetPointer<const flatbuffers::Vector<
flatbuffers::Offset<flatbuffers::String>>*>(
field->offset())));
case reflection::Obj: {
const auto* field_schema =
schema_.objects()->Get(field->type()->index());
if (field_schema) {
const auto* index = findStringKeyField(*field_schema);
if (index) {
return CelValue::CreateMap(
google::protobuf::Arena::Create<ObjectStringIndexedMapImpl>(
arena_,
table_.GetPointer<const flatbuffers::Vector<
flatbuffers::Offset<flatbuffers::Table>>*>(
field->offset()),
schema_, *field_schema, *index, arena_));
} else {
return CelValue::CreateList(google::protobuf::Arena::Create<ObjectListImpl>(
arena_,
table_.GetPointer<const flatbuffers::Vector<
flatbuffers::Offset<flatbuffers::Table>>*>(
field->offset()),
schema_, *field_schema, arena_));
}
}
break;
}
default:
// Unsupported vector base types
return {};
}
break;
}
default:
// Unsupported types: enums, unions, arrays
return {};
}
return {};
}
const CelMap* CreateFlatBuffersBackedObject(const uint8_t* flatbuf,
const reflection::Schema& schema,
google::protobuf::Arena* arena) {
return google::protobuf::Arena::Create<const FlatBuffersMapImpl>(
arena, *flatbuffers::GetAnyRoot(flatbuf), schema, *schema.root_table(),
arena);
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google