forked from facebook/redex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurable.cpp
567 lines (504 loc) · 20.6 KB
/
Configurable.cpp
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Configurable.h"
#include <boost/optional.hpp>
#include "DexClass.h"
template <typename T>
using optional = boost::optional<T>;
#define error_or_warn(error, warn, msg, ...) \
always_assert_log(!(error), msg, ##__VA_ARGS__); \
if (warn) { \
fprintf(stderr, "WARNING: " msg, ##__VA_ARGS__); \
}
#define ASSERT_NO_BINDFLAGS(type) \
always_assert_log(!bindflags, "No bindflags may be specified for a " #type);
namespace {
// NOTE: "Leaf" parse functions return an `optional` return type to allow
// unified checking of the value in container parsing, without having
// to do tricks like SFINAE to have special handling for pointers
// vs empty strings vs empty containers etc.
template <bool kCheckType>
optional<std::string> parse_str(const Json::Value& str,
Configurable::bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(std::string);
if (kCheckType && !str.isString()) {
throw std::runtime_error("Expected string, got:" + str.asString());
}
return str.asString();
}
optional<DexType*> parse_type(const Json::Value& str,
Configurable::bindflags_t bindflags) {
assert_log(!(bindflags & ~Configurable::bindflags::types::mask),
"Only type bindflags may be specified for a DexType*");
if (!str.isString()) {
throw std::runtime_error("Expected string, got:" + str.asString());
}
auto type = DexType::get_type(str.asString());
if (type == nullptr) {
error_or_warn(
bindflags & Configurable::bindflags::types::error_if_unresolvable,
bindflags & Configurable::bindflags::types::warn_if_unresolvable,
"\"%s\" failed to resolve to a known type\n", str.asString().c_str());
return boost::none;
}
return type;
}
optional<DexClass*> parse_class(const Json::Value& value,
Configurable::bindflags_t bindflags) {
auto type_res = parse_type(value, bindflags);
auto cls = type_class(type_res ? *type_res : nullptr);
if (cls == nullptr) {
error_or_warn(
bindflags & Configurable::bindflags::classes::error_if_unresolvable,
bindflags & Configurable::bindflags::classes::warn_if_unresolvable,
"\"%s\" failed to resolve to a known class\n",
value.asString().c_str());
return boost::none;
}
return cls;
}
optional<DexMethodRef*> parse_method_ref(const Json::Value& str,
Configurable::bindflags_t bindflags) {
always_assert_log(!(bindflags & ~Configurable::bindflags::methods::mask),
"Only method bindflags may be specified for a "
"std::unordered_map<DexMethod*, DexMethod*>");
if (!str.isString()) {
throw std::runtime_error("Expected string, got:" + str.asString());
}
auto meth = DexMethod::get_method(str.asString());
if (meth == nullptr) {
error_or_warn(
bindflags & Configurable::bindflags::methods::error_if_unresolvable,
bindflags & Configurable::bindflags::methods::warn_if_unresolvable,
"\"%s\" failed to resolve to a known method\n",
str.asString().c_str());
return boost::none;
}
return meth;
}
optional<DexMethod*> parse_method(const Json::Value& str,
Configurable::bindflags_t bindflags) {
auto meth_ref_opt = parse_method_ref(str, bindflags);
if (!meth_ref_opt) {
return boost::none;
}
auto meth_ref = *meth_ref_opt;
if (!meth_ref->is_def()) {
error_or_warn(
bindflags & Configurable::bindflags::methods::error_if_not_def,
bindflags & Configurable::bindflags::methods::warn_if_not_def,
"\"%s\" resolved to a method reference\n", str.asString().c_str());
return boost::none;
}
return meth_ref->as_def();
}
// NOTE: For parsing into containers, `boost::none` values of the parsing
// function will be skipped.
// Infer the result of the parsing function.
template <typename ParseFn>
using parse_result = typename std::result_of<ParseFn(
const Json::Value&, Configurable::bindflags_t)>::type::value_type;
template <typename ParseFn>
optional<std::vector<parse_result<ParseFn>>> parse_vec(
const Json::Value& value,
ParseFn parse_fn,
Configurable::bindflags_t bindflags) {
std::vector<parse_result<ParseFn>> result;
for (auto& v : value) {
if (auto parsed = parse_fn(v, bindflags)) {
result.emplace_back(std::move(*parsed));
}
}
return result;
}
template <typename ParseFn>
optional<std::unordered_set<parse_result<ParseFn>>> parse_set(
const Json::Value& value,
ParseFn parse_fn,
Configurable::bindflags_t bindflags) {
std::unordered_set<parse_result<ParseFn>> result;
for (auto& v : value) {
if (auto parsed = parse_fn(v, bindflags)) {
result.emplace(std::move(*parsed));
}
}
return result;
}
template <typename KFn, typename VFn>
std::unordered_map<parse_result<KFn>, parse_result<VFn>> parse_map(
const Json::Value& value,
KFn k_parse,
Configurable::bindflags_t k_bindflags,
VFn v_parse,
Configurable::bindflags_t v_bindflags) {
if (!value.isObject()) {
throw std::runtime_error("Expected object, got:" + value.asString());
}
std::unordered_map<parse_result<KFn>, parse_result<VFn>> result;
for (auto it = value.begin(); it != value.end(); ++it) {
auto k = k_parse(it.key(), k_bindflags);
auto v = v_parse(*it, v_bindflags);
if (k && v) {
result.emplace(std::move(*k), std::move(*v));
}
}
return result;
}
template <bool kCheckType>
optional<std::vector<std::string>> parse_str_vec(
const Json::Value& value, Configurable::bindflags_t bindflags) {
return parse_vec(value, parse_str<kCheckType>, bindflags);
}
} // namespace
void Configurable::parse_config(const JsonWrapper& json) {
m_after_configuration = {};
m_reflecting = false;
m_param_reflector =
[](const std::string& param_name, const std::string& param_doc,
const bool param_is_required, const bindflags_t param_bindflags,
const Configurable::ReflectionParam::Type param_type_tag,
const std::tuple<std::string, Configurable::Reflection>& param_type,
const Json::Value& default_value) {};
m_trait_reflector = [](const std::string&, const Json::Value&) {};
m_parser = [&json](const std::string& name) {
// TODO: add std::string API for contains
if (json.contains(name.c_str())) {
return boost::optional<const Json::Value&>(json[name.c_str()]);
} else {
return boost::optional<const Json::Value&>{};
}
};
bind_config();
// m_after_configuration may have been set in bind_config()
if (m_after_configuration) {
m_after_configuration();
}
}
Configurable::Reflection Configurable::reflect() {
Configurable::Reflection cr;
cr.name = get_config_name();
cr.doc = get_config_doc();
m_after_configuration = {};
m_parser = [](const std::string&) {
return boost::optional<const Json::Value&>{};
};
// N.B. using std::tuple here, meant to evolve to use of std::variant w/ c++17
m_reflecting = true;
m_param_reflector =
[&cr](const std::string& param_name,
const std::string& param_doc,
const bool param_is_required,
const bindflags_t param_bindflags,
const Configurable::ReflectionParam::Type param_type_tag,
const std::tuple<std::string, Configurable::Reflection>& param_type,
const Json::Value& default_value) {
switch (param_type_tag) {
case Configurable::ReflectionParam::Type::PRIMITIVE:
cr.params[param_name] = Configurable::ReflectionParam(
param_name, param_doc, param_is_required, param_bindflags,
std::get<Configurable::ReflectionParam::Type::PRIMITIVE>(
param_type),
default_value);
break;
case Configurable::ReflectionParam::Type::COMPOSITE:
cr.params[param_name] = Configurable::ReflectionParam(
param_name, param_doc, param_is_required, param_bindflags,
std::get<Configurable::ReflectionParam::Type::COMPOSITE>(
param_type));
break;
default:
not_reached_log("Invalid Configurable::ReflectionParam::Type: %d",
param_type_tag);
break;
}
};
m_trait_reflector = [&cr](const std::string& name, const Json::Value& value) {
cr.traits[name] = Configurable::ReflectionTrait(name, value);
};
bind_config();
return cr;
}
template <>
float Configurable::as<float>(const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(float);
return value.asFloat();
}
template <>
int Configurable::as<int>(const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(int);
return value.asInt();
}
template <>
unsigned int Configurable::as<unsigned int>(const Json::Value& value,
bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned int);
return value.asUInt();
}
template <>
boost::optional<int> Configurable::as<boost::optional<int>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned int);
return value.asInt();
}
template <>
boost::optional<unsigned int> Configurable::as<boost::optional<unsigned int>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned int);
return value.asUInt();
}
template <>
long Configurable::as<long>(const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(long);
return value.asInt64();
}
template <>
unsigned long Configurable::as<unsigned long>(const Json::Value& value,
bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned long);
return value.asUInt64();
}
template <>
boost::optional<long> Configurable::as<boost::optional<long>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned long);
return value.asInt64();
}
template <>
boost::optional<unsigned long> Configurable::as<boost::optional<unsigned long>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned long);
return value.asUInt64();
}
template <>
long long Configurable::as<long long>(const Json::Value& value,
bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(long long);
return value.asInt64();
}
template <>
unsigned long long Configurable::as<unsigned long long>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(unsigned long long);
return value.asUInt64();
}
template <>
bool Configurable::as<bool>(const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(bool);
return value.asBool();
}
template <>
std::string Configurable::as<std::string>(const Json::Value& value,
bindflags_t bindflags) {
return std::move(*parse_str<false>(value, bindflags));
}
template <>
boost::optional<std::string> Configurable::as<boost::optional<std::string>>(
const Json::Value& value, bindflags_t bindflags) {
always_assert_log(
!(bindflags & ~Configurable::bindflags::optionals::skip_empty_string),
"Only bindflags::optionals::skip_empty_string may be specified for a "
"boost::optional<std::string>");
std::string str = value.asString();
if (str.empty() &&
(bindflags & Configurable::bindflags::optionals::skip_empty_string)) {
return boost::none;
} else {
return str;
}
}
template <>
std::vector<Json::Value> Configurable::as<std::vector<Json::Value>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(std::vector<Json::Value>);
std::vector<Json::Value> result;
for (auto& str : value) {
result.emplace_back(str);
}
return result;
}
template <>
std::vector<std::string> Configurable::as<std::vector<std::string>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_str_vec<false>(value, bindflags));
}
template <>
std::vector<unsigned int> Configurable::as<std::vector<unsigned int>>(
const Json::Value& value, bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(std::vector<unsigned int>);
std::vector<unsigned int> result;
for (const auto& str : value) {
result.push_back(str.asUInt());
}
return result;
}
template <>
std::unordered_set<std::string>
Configurable::as<std::unordered_set<std::string>>(const Json::Value& value,
bindflags_t bindflags) {
return std::move(*parse_set(value, parse_str<false>, bindflags));
}
template <>
DexType* Configurable::as<DexType*>(const Json::Value& value,
bindflags_t bindflags) {
if (auto type = parse_type(value, bindflags)) {
return *type;
}
return nullptr;
}
template <>
std::vector<DexType*> Configurable::as<std::vector<DexType*>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_vec(value, parse_type, bindflags));
}
template <>
std::vector<DexMethod*> Configurable::as<std::vector<DexMethod*>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_vec(value, parse_method, bindflags));
}
template <>
std::unordered_set<DexType*> Configurable::as<std::unordered_set<DexType*>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_set(value, parse_type, bindflags));
}
template <>
std::unordered_set<const DexType*>
Configurable::as<std::unordered_set<const DexType*>>(const Json::Value& value,
bindflags_t bindflags) {
return std::move(*parse_set(
value,
[](const Json::Value& v, bindflags_t b) {
return boost::optional<const DexType*>(parse_type(v, b));
},
bindflags));
}
using TypeMap = std::unordered_map<DexType*, DexType*>;
template <>
TypeMap Configurable::as<TypeMap>(const Json::Value& value,
bindflags_t bindflags) {
return parse_map(value, parse_type, bindflags, parse_type, bindflags);
}
template <>
std::unordered_set<DexClass*> Configurable::as<std::unordered_set<DexClass*>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_set(value, parse_class, bindflags));
}
template <>
std::unordered_set<DexMethod*> Configurable::as<std::unordered_set<DexMethod*>>(
const Json::Value& value, bindflags_t bindflags) {
return std::move(*parse_set(value, parse_method, bindflags));
}
using MethRefMap = std::unordered_map<DexMethodRef*, DexMethodRef*>;
template <>
MethRefMap Configurable::as<MethRefMap>(const Json::Value& value,
bindflags_t bindflags) {
return parse_map(value, parse_method_ref, bindflags, parse_method_ref,
bindflags);
}
template <>
Configurable::MapOfMethods Configurable::as<Configurable::MapOfMethods>(
const Json::Value& value, bindflags_t bindflags) {
return parse_map(value, parse_method, bindflags, parse_method, bindflags);
}
template <>
Configurable::MapOfVectorOfStrings
Configurable::as<Configurable::MapOfVectorOfStrings>(const Json::Value& value,
bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(Configurable::MapOfVectorOfStrings);
return parse_map(value, parse_str<true>, bindflags, parse_str_vec<true>,
bindflags);
}
template <>
Configurable::MapOfStrings Configurable::as<Configurable::MapOfStrings>(
const Json::Value& value, bindflags_t bindflags) {
return parse_map(value, parse_str<true>, bindflags, parse_str<true>,
bindflags);
}
template <>
Json::Value Configurable::as<Json::Value>(const Json::Value& value,
bindflags_t bindflags) {
ASSERT_NO_BINDFLAGS(Json::Value);
return value;
}
#define IMPLEMENT_REFLECTOR(type) \
template <> \
void Configurable::reflect( \
ReflectorParamFunc& reflector, const std::string& param_name, \
const std::string& param_doc, const bool param_is_required, \
const Configurable::bindflags_t param_bindflags, type& param, \
type default_value) { \
param = default_value; \
reflector(param_name, param_doc, param_is_required, param_bindflags, \
Configurable::ReflectionParam::PRIMITIVE, \
std::make_tuple(std::string{#type}, Configurable::Reflection()), \
Json::nullValue); \
}
#define IMPLEMENT_REFLECTOR_EX(T, type_name) \
template <> \
void Configurable::reflect( \
ReflectorParamFunc& reflector, const std::string& param_name, \
const std::string& param_doc, const bool param_is_required, \
const Configurable::bindflags_t param_bindflags, T& param, \
typename DefaultValueType<T>::type default_value) { \
param = default_value; \
reflector( \
param_name, param_doc, param_is_required, param_bindflags, \
Configurable::ReflectionParam::PRIMITIVE, \
std::make_tuple(std::string{type_name}, Configurable::Reflection()), \
Json::nullValue); \
}
#define IMPLEMENT_REFLECTOR_WITH_DFLT_VALUE(T) \
template <> \
void Configurable::reflect( \
ReflectorParamFunc& reflector, const std::string& param_name, \
const std::string& param_doc, const bool param_is_required, \
const Configurable::bindflags_t param_bindflags, T& param, \
typename DefaultValueType<T>::type default_value) { \
param = default_value; \
reflector(param_name, param_doc, param_is_required, param_bindflags, \
Configurable::ReflectionParam::PRIMITIVE, \
std::make_tuple(std::string{#T}, Configurable::Reflection()), \
Json::Value(default_value)); \
}
#define IMPLEMENT_TRAIT_REFLECTOR(type) \
template <> \
void Configurable::reflect_trait(ReflectorTraitFunc& reflector_trait, \
const std::string& name, type value) { \
reflector_trait(name, Json::Value(value)); \
}
IMPLEMENT_REFLECTOR(float)
IMPLEMENT_REFLECTOR_WITH_DFLT_VALUE(bool)
IMPLEMENT_REFLECTOR_EX(int, "int")
IMPLEMENT_REFLECTOR_EX(unsigned int, "int")
IMPLEMENT_REFLECTOR_EX(boost::optional<int>, "int")
IMPLEMENT_REFLECTOR_EX(boost::optional<unsigned int>, "int")
IMPLEMENT_REFLECTOR_EX(long, "long")
IMPLEMENT_REFLECTOR_EX(unsigned long, "long")
IMPLEMENT_REFLECTOR_EX(long long, "long")
IMPLEMENT_REFLECTOR_EX(unsigned long long, "long")
IMPLEMENT_REFLECTOR_EX(DexType*, "string")
IMPLEMENT_REFLECTOR_EX(std::string, "string")
IMPLEMENT_REFLECTOR_EX(Json::Value, "json")
IMPLEMENT_REFLECTOR_EX(std::vector<Json::Value>, "list")
IMPLEMENT_REFLECTOR_EX(boost::optional<std::string>, "string")
IMPLEMENT_REFLECTOR_EX(std::vector<std::string>, "list")
IMPLEMENT_REFLECTOR_EX(std::vector<unsigned int>, "list")
IMPLEMENT_REFLECTOR_EX(std::unordered_set<std::string>, "set")
IMPLEMENT_REFLECTOR_EX(std::vector<DexType*>, "list")
IMPLEMENT_REFLECTOR_EX(std::vector<DexMethod*>, "list")
IMPLEMENT_REFLECTOR_EX(std::unordered_set<const DexType*>, "set")
IMPLEMENT_REFLECTOR_EX(std::unordered_set<DexType*>, "set")
IMPLEMENT_REFLECTOR_EX(std::unordered_set<DexClass*>, "set")
IMPLEMENT_REFLECTOR_EX(std::unordered_set<DexMethod*>, "set")
IMPLEMENT_REFLECTOR_EX(Configurable::MapOfVectorOfStrings, "dict")
IMPLEMENT_REFLECTOR_EX(Configurable::MapOfMethods, "dict")
IMPLEMENT_REFLECTOR_EX(Configurable::MapOfStrings, "dict")
IMPLEMENT_REFLECTOR_EX(MethRefMap, "dict")
IMPLEMENT_REFLECTOR_EX(TypeMap, "dict")
IMPLEMENT_TRAIT_REFLECTOR(bool)
IMPLEMENT_TRAIT_REFLECTOR(int)
IMPLEMENT_TRAIT_REFLECTOR(std::string)
#undef error_or_warn