-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjse.cpp
611 lines (516 loc) · 20.8 KB
/
jse.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
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
////////////////////////////////////////////////////////////////////////////////
#include <jse/jse.h>
#include <iostream>
#include <filesystem> // C++17
#include <sstream>
#include <algorithm>
#include <fstream>
////////////////////////////////////////////////////////////////////////////////
namespace jse
{
//////////// PUBLIC
bool JSE::verify_json(const json &input, const json &rules)
{
log.clear();
json input_copy = input;
return verify_json("/", input_copy, rules);
};
json JSE::inject_defaults(const json &input, const json &rules)
{
log.clear();
json input_copy = input;
bool b = verify_json("/", input_copy, rules);
assert(b); // Make sure the original file is valid
// Check that the new file, after adding defaults, is also valid
// assert(verify_json(input_copy, rules));
return input_copy;
};
std::string JSE::log2str()
{
std::stringstream s;
for (log_item i : log)
{
s << i.first << ": " << i.second << std::endl;
}
return s.str();
};
//////////// PRIVATE
namespace
{
// adds key after the provided pointer, correctly handling the json special case that root has an ending /
std::string append_pointer(const std::string &pointer, const std::string &key)
{
if (pointer == "/")
return "/" + key;
else
return pointer + "/" + key;
}
// adds key before the provided pointer, correctly handling the json special case that root has an ending /
std::string prepend_pointer(const std::string &pointer, const std::string &key)
{
if (key == "/")
return pointer;
else if (pointer == "/")
return key;
else
return key + pointer;
}
} // namespace
// enriches a given json spec with included json specs
json JSE::inject_include(const json &rules)
{
std::vector<string> dirs = include_directories;
dirs.push_back(""); // adding default path
// max 10 levels of nesting to avoid infinite loops
json current = rules;
for (size_t x = 0; x < 10; x++)
{
// check if the rules have any include
bool include_present = false;
for (const auto &rule : current)
if (rule.at("type") == "include")
include_present = true;
// if there are no includes, return the current ones
if (!include_present)
return current;
json enriched;
// otherwise, do a round of replacement
for (const auto &rule : current)
{
// copy all rules that are not include
if (rule.at("type") != "include")
{
enriched.push_back(rule);
}
// if the rule is an include, expand the node with a copy of the included file
else
{
bool replaced = false;
// the include file could be in any of the include directories
for (const auto &dir : dirs)
{
string spec_file = rule.at("spec_file");
string f = dir + "/" + spec_file;
// check if the file exists
if (std::filesystem::is_regular_file(f))
{
std::ifstream ifs(f);
json include_rules = json::parse(ifs);
// loop over all rules to add the prefix
for (auto &i_rule : include_rules)
{
string prefix = rule.at("pointer");
string pointer = i_rule.at("pointer");
string new_pointer = prepend_pointer(pointer, prefix);
i_rule.at("pointer") = new_pointer;
}
// save modified rules
for (const auto &i_rule : include_rules)
enriched.push_back(i_rule);
// one substitution is enough, give up the search over include dirs
replaced = true;
break;
}
}
if (!replaced)
{
string pointer = rule.at("pointer");
throw std::runtime_error("Failed to replace the include rule: " + pointer);
assert(replaced == true);
}
}
}
// now that we replaced the include, copy it back to current
current = enriched;
}
throw std::runtime_error("Reached maximal 10 levels of include recursion.");
}
bool JSE::verify_json(const string &pointer, json &input, const json &rules)
{
// Find all rules that apply for the input node
// TODO: accelerate this
std::vector<json> matching_rules = collect_pointer(pointer, rules);
// There must be at least one, otherwise warning and return true if not strict
if (matching_rules.empty())
{
log.push_back(log_item("warning", "Unknown entry " + pointer));
return !strict;
}
// Test all rules, one and only one must pass, otherwise throw exception
std::vector<json> verified_matching_rules;
for (auto r : matching_rules)
{
if (verify_rule(input, r))
{
verified_matching_rules.push_back(r);
}
}
if (verified_matching_rules.size() == 0)
{
// Before giving up, try boxing a primitive type
if (boxing_primitive && !input.is_array())
{
string new_pointer = append_pointer(pointer, "*");
// Make sure there are some rules for the boxed version before recursively checking
if (collect_pointer(new_pointer, rules).size() > 0)
if (verify_json(new_pointer, input, rules))
return true;
}
std::stringstream s;
s << "No rule matched for \"" << pointer << "\": " << input.dump(/*indent=*/4) << std::endl;
s << "No valid rules in this list:\n";
for (int i = 0; i < matching_rules.size(); i++)
s << i << ": " << matching_rules[i].dump(/*indent=*/4) << "\n";
log.push_back(log_item("error", s.str()));
return false;
}
else if (verified_matching_rules.size() > 1)
{
std::stringstream s;
s << "Multiple rules matched for \"" << pointer << "\": " << input.dump(/*indent=*/4) << std::endl;
s << "Multiple valid rules in this list, only one should be valid:";
for (int i = 0; i < verified_matching_rules.size(); i++)
s << i << ": " << verified_matching_rules[i].dump(/*indent=*/4) << "\n";
log.push_back(log_item("error", s.str()));
return false; // if not strict, we do not check for extra keys to distinguish between rules
}
const json &single_matched_rule = verified_matching_rules.front();
// If it passes and if it is a dictionary, then test all childrens
if (input.is_object())
{
for (const auto &[key, value] : input.items())
{
const string new_pointer = append_pointer(pointer, key);
// first of all, let's check if the specs are correct
const json defaults = collect_default_rules(new_pointer, rules);
// if it is mandatory, make sure there are no defaults
if (single_matched_rule.contains("required") && contained_in_list(key, single_matched_rule["required"]))
{
if (defaults.size() != 0)
{
log.push_back(log_item("error", "Inconsistent specifications: " + new_pointer + " is a mandatory field with a default value."));
return false;
}
}
// if it is optional, there should be only one default in the specs
else if (single_matched_rule.contains("optional") && contained_in_list(key, single_matched_rule["optional"]))
{
if (defaults.size() != 1)
{
log.push_back(log_item("error", "Inconsistent specifications: " + new_pointer + " is an optional field with " + std::to_string(defaults.size()) + " default values."));
return false;
}
}
// if it is not mandatory and not optional, something is wrong
else
{
log.push_back(log_item("warning", "Inconsistent specifications: " + new_pointer + " is neither an optional or a mandatory field."));
if (strict)
return false;
}
// now let's make sure it can be validated
if (!verify_json(new_pointer, value, rules))
return false;
}
}
// If the dictionary is valid and has optional fields, add defaults for the optional fields
if ((input.is_object() || input.is_null()) // Note: null fields might be objects, and thus might have optional fields
&& single_matched_rule.contains("optional")) // <- the object has a list of optional
{
for (const std::string &key : single_matched_rule["optional"]) // for each optional field
{
if (input.contains(key)) // skip if already in the object
continue;
string new_pointer = append_pointer(pointer, key);
json defaults = collect_default_rules(new_pointer, rules); // Find the default
if (defaults.size() != 1)
{
log.push_back(log_item("error", "Inconsistent specifications: " + new_pointer + " is an optional field with " + std::to_string(defaults.size()) + " default values."));
return false;
}
if (defaults[0]["default"] != "skip")
{
input[key] = defaults[0]["default"];
// Let's validate/inject the default subtree
if (!verify_json(new_pointer, input[key], rules))
return false;
}
}
}
// In case of a list
// All the elements in the list must pass the test
if (input.is_array())
{
for (auto &i : input)
if (!verify_json(append_pointer(pointer, "*"), i, rules))
return false;
}
// If they all pass, return true
return true;
}
bool JSE::verify_rule(const json &input, const json &rule)
{
// std::cout << "Verifying " << input << std::endl;
string type = rule.at("type");
if (type == "list")
return verify_rule_list(input, rule);
else if (type == "float")
return verify_rule_float(input, rule);
else if (type == "int")
return verify_rule_int(input, rule);
else if (type == "file")
return verify_rule_file(input, rule);
else if (type == "folder")
return verify_rule_folder(input, rule);
else if (type == "string")
return verify_rule_string(input, rule);
else if (type == "object")
return verify_rule_object(input, rule);
else if (type == "bool")
return verify_rule_bool(input, rule);
else if (type == "include")
return verify_rule_include(input, rule);
else
{
log.push_back(log_item("error", "Unknown rule type " + type));
return false;
}
}
bool JSE::verify_rule_file(const json &input, const json &rule)
{
assert(rule.at("type") == "file");
if (!input.is_string())
return false;
std::string p_str = cwd + "/" + string(input);
std::filesystem::path p = std::filesystem::path(p_str);
if (!skip_file_check && !std::filesystem::is_regular_file(p))
{
log.push_back(log_item("warning", "File not found: " + p_str));
if (strict)
return false;
}
if (!skip_file_check && rule.contains("extensions"))
{
std::string ext = p.extension().string();
int count = 0;
for (auto e : rule["extensions"])
if (e == ext)
count++;
if (count != 1)
return false;
}
return true;
}
bool JSE::verify_rule_folder(const json &input, const json &rule)
{
assert(rule.at("type") == "folder");
std::string p_str = cwd + "/" + string(input);
std::filesystem::path p = std::filesystem::path(p_str);
if (!std::filesystem::is_directory(p))
{
log.push_back(log_item("warning", "Folder not found: " + p_str));
if (strict)
return false;
}
return true;
}
bool JSE::verify_rule_float(const json &input, const json &rule)
{
assert(rule.at("type") == "float");
if (!input.is_number() && !input.is_null())
return false;
if (rule.contains("min") && input < rule["min"])
return false;
if (rule.contains("max") && input > rule["max"])
return false;
return true;
}
bool JSE::verify_rule_int(const json &input, const json &rule)
{
assert(rule.at("type") == "int");
if (!input.is_number_integer() && !input.is_null())
return false;
if (rule.contains("min") && input < rule["min"])
return false;
if (rule.contains("max") && input > rule["max"])
return false;
return true;
}
bool JSE::verify_rule_string(const json &input, const json &rule)
{
assert(rule.at("type") == "string");
if (!input.is_string())
return false;
if (rule.contains("options"))
{
int count = 0;
for (auto e : rule["options"])
if (e == input)
count++;
if (count != 1)
return false;
}
return true;
}
bool JSE::verify_rule_object(const json &input, const json &rule)
{
assert(rule.at("type") == "object");
if (!input.is_object() && !input.is_null())
return false;
// Check that all required fields are present
if (rule.contains("required"))
for (auto e : rule["required"])
if (!input.contains(string(e)))
return false;
if (strict) // strict mode: check that no extra fields are present
{
std::vector<std::string> keys;
keys.reserve((rule.contains("required") ? rule["required"].size() : 0)
+ (rule.contains("optional") ? rule["optional"].size() : 0));
if (rule.contains("required"))
keys.insert(keys.end(), rule["required"].begin(), rule["required"].end());
if (rule.contains("optional"))
keys.insert(keys.end(), rule["optional"].begin(), rule["optional"].end());
for (const auto &[key, value] : input.items())
if (std::find(keys.begin(), keys.end(), key) == keys.end())
return false;
}
if (rule.contains("type_name")
&& (!input.contains("type") || input["type"] != rule["type_name"]))
return false;
return true;
}
bool JSE::verify_rule_bool(const json &input, const json &rule)
{
assert(rule.at("type") == "bool");
return input.is_boolean();
}
bool JSE::verify_rule_list(const json &input, const json &rule)
{
assert(rule.at("type") == "list");
if (!input.is_array())
return false;
if (rule.contains("min") && input.size() < rule["min"])
return false;
if (rule.contains("max") && input.size() > rule["max"])
return false;
return true;
}
bool JSE::verify_rule_include(const json &input, const json &rule)
{
assert(rule.at("type") == "include");
// An include rule always fails, they should be processed first by the inject_include function
return false;
}
json JSE::collect_default_rules(const json &rules)
{
// Find all rules that apply for the input node
// TODO: accelerate this
std::vector<json> matching_rules;
for (auto i : rules)
{
if (i.contains("default"))
matching_rules.push_back(i);
}
return matching_rules;
}
json JSE::collect_default_rules(const string &pointer, const json &rules)
{
// Find all rules that apply for the input node
// TODO: accelerate this
std::vector<json> matching_rules;
for (auto i : rules)
{
if (i.at("pointer") == pointer && i.contains("default"))
matching_rules.push_back(i);
}
return matching_rules;
}
bool JSE::contained_in_list(string item, const json &list)
{
return std::find(list.begin(), list.end(), item) != list.end();
}
std::vector<json> JSE::collect_pointer(const string &pointer, const json &rules)
{
std::vector<json> matching_rules;
for (auto i : rules)
{
if (i.at("pointer") == pointer)
matching_rules.push_back(i);
}
return matching_rules;
}
json JSE::find_valid_rule(const string &pointer, const json &input, const json &rules)
{
for (auto i : collect_pointer(pointer, rules))
if (verify_rule(input, i))
return i;
return json();
}
std::tuple<bool, string> JSE::is_subset_pointer(const string &json, const string &pointer)
{
// Splits a string into tokens using the deliminator delim
auto tokenize = [](std::string const &str, const char delim) {
size_t start;
size_t end = 0;
std::vector<string> out;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
return out;
};
// Replaces occurrences of a substring
auto replace_all = [](std::string str, const std::string &from, const std::string &to) {
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
};
// Check if a string is an integer
auto is_number = [](const string &str) {
for (char const &c : str)
if (std::isdigit(c) == 0)
return false;
return true;
};
// Tokenize json_pointer
std::vector<string> json_t = tokenize(json, '/');
std::vector<string> pointer_t = tokenize(pointer, '/');
// if the json is not shorter or if there are no tokens, give up
if ((json_t.size() >= pointer_t.size())
|| (pointer_t.size() == 0))
return {false, ""};
std::string buf = "";
// if it is shorter, match every entry
for (unsigned i = 0; i < pointer_t.size(); ++i)
{
// if there is no corresponding entry on json, copy and move on
if (json_t.size() <= i)
{
buf.append("/" + replace_all(pointer_t[i], "*", "0"));
}
// if there is an entry on json and it is the same, move on
else if (json_t[i] == pointer_t[i])
{
buf.append("/" + pointer_t[i]);
}
// if the pointer contains a star, then accept any integer on json
else if (pointer_t[i] == "*")
{
if (is_number(json_t[i]))
buf.append("/" + json_t[i]);
}
// if no rule matches it is not a match
else
{
return {false, ""};
}
}
return {true, buf};
}
} // namespace jse