summaryrefslogtreecommitdiff
path: root/compiler/cpp/src/thrift/generate/validator_parser.cc
blob: a0aee4661890eb5a4e7160feff022105507d19bd (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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
 *
 *   http://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.
 */

/*
 * This file is programmatically sanitized for style:
 * astyle --style=1tbs -f -p -H -j -U t_validator_parser.cc
 *
 * The output of astyle should not be taken unquestioningly, but it is a good
 * guide for ensuring uniformity and readability.
 */

#include <fstream>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_map>
#include <vector>

#include "thrift/generate/t_generator.h"
#include "thrift/generate/validator_parser.h"
#include "thrift/platform.h"
#include "thrift/version.h"
#include <algorithm>
#include <clocale>
#include <sstream>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

const char* list_delimiter = "[], ";

std::vector<validation_rule*> validation_parser::parse_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  std::vector<validation_rule*> empty_rules;
  if (type->is_typedef()) {
    type = type->get_true_type();
  }
  if (type->is_enum()) {
    return parse_enum_field(type, annotations);
  } else if (type->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_UUID:
    case t_base_type::TYPE_VOID:
      return empty_rules;
    case t_base_type::TYPE_I8:
    case t_base_type::TYPE_I16:
    case t_base_type::TYPE_I32:
    case t_base_type::TYPE_I64:
      return parse_integer_field(type, annotations);
    case t_base_type::TYPE_DOUBLE:
      return parse_double_field(type, annotations);
    case t_base_type::TYPE_STRING:
      return parse_string_field(type, annotations);
    case t_base_type::TYPE_BOOL:
      return parse_bool_field(type, annotations);
    }
  } else if (type->is_list()) {
    return parse_list_field(type, annotations);
  } else if (type->is_set()) {
    return parse_set_field(type, annotations);
  } else if (type->is_map()) {
    return parse_map_field(type, annotations);
  } else if (type->is_struct()) {
    if (((t_struct*)type)->is_union()) {
      return parse_union_field(type, annotations);
    }
    return parse_struct_field(type, annotations);
  } else if (type->is_xception()) {
    return parse_xception_field(type, annotations);
  }
  throw "validator error: unsupported type: " + type->get_name();
}

std::vector<validation_rule*> validation_parser::parse_bool_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_bool_rule(rules, "vt.const", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_enum_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  std::vector<validation_rule*> rules;
  add_bool_rule(rules, "vt.defined_only", annotations);
  add_enum_list_rule(rules, (t_enum*)type, "vt.in", annotations);
  add_enum_list_rule(rules, (t_enum*)type, "vt.not_in", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_double_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_double_rule(rules, "vt.lt", annotations);
  add_double_rule(rules, "vt.le", annotations);
  add_double_rule(rules, "vt.gt", annotations);
  add_double_rule(rules, "vt.ge", annotations);
  add_double_list_rule(rules, "vt.in", annotations);
  add_double_list_rule(rules, "vt.not_in", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_integer_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_integer_rule(rules, "vt.lt", annotations);
  add_integer_rule(rules, "vt.le", annotations);
  add_integer_rule(rules, "vt.gt", annotations);
  add_integer_rule(rules, "vt.ge", annotations);
  add_integer_list_rule(rules, "vt.in", annotations);
  add_integer_list_rule(rules, "vt.not_in", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_string_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_string_rule(rules, "vt.const", annotations);
  add_integer_rule(rules, "vt.min_size", annotations);
  add_integer_rule(rules, "vt.max_size", annotations);
  add_string_rule(rules, "vt.pattern", annotations);
  add_string_rule(rules, "vt.prefix", annotations);
  add_string_rule(rules, "vt.suffix", annotations);
  add_string_rule(rules, "vt.contains", annotations);
  add_string_rule(rules, "vt.not_contains", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_set_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  return parse_list_field(type, annotations);
}

std::vector<validation_rule*> validation_parser::parse_list_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_integer_rule(rules, "vt.min_size", annotations);
  add_integer_rule(rules, "vt.max_size", annotations);
  std::string elem_prefix("vt.elem");
  std::map<std::string, std::vector<std::string>> elem_annotations;
  for (auto it = annotations.begin(); it != annotations.end(); it++) {
    if (it->first.compare(0, elem_prefix.size(), elem_prefix) == 0) {
      std::string elem_key = "vt" + it->first.substr(elem_prefix.size());
      elem_annotations[elem_key] = it->second;
    }
  }
  std::vector<validation_rule*> elem_rules;
  if (type->is_list()) {
    elem_rules = parse_field(((t_list*)type)->get_elem_type(), elem_annotations);
  } else if (type->is_set()) {
    elem_rules = parse_field(((t_set*)type)->get_elem_type(), elem_annotations);
  }
  for (auto it = elem_rules.begin(); it != elem_rules.end(); it++) {
    validation_rule* rule = new validation_rule(elem_prefix, *it);
    rules.push_back(rule);
  }
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_map_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  std::vector<validation_rule*> rules;
  add_integer_rule(rules, "vt.min_size", annotations);
  add_integer_rule(rules, "vt.max_size", annotations);
  std::string key_prefix("vt.key");
  std::map<std::string, std::vector<std::string>> key_annotations;
  for (auto it = annotations.begin(); it != annotations.end(); it++) {
    if (it->first.compare(0, key_prefix.size(), key_prefix) == 0) {
      std::string key_key = "vt" + it->first.substr(key_prefix.size());
      key_annotations[key_key] = it->second;
    }
  }
  std::vector<validation_rule*> key_rules;
  key_rules = parse_field(((t_map*)type)->get_key_type(), key_annotations);
  for (auto it = key_rules.begin(); it != key_rules.end(); it++) {
    validation_rule* rule = new validation_rule(key_prefix, *it);
    rules.push_back(rule);
  }

  std::string value_prefix("vt.value");
  std::map<std::string, std::vector<std::string>> value_annotations;
  for (auto it = annotations.begin(); it != annotations.end(); it++) {
    if (it->first.compare(0, value_prefix.size(), value_prefix) == 0) {
      std::string value_key = "vt" + it->first.substr(value_prefix.size());
      value_annotations[value_key] = it->second;
    }
  }
  std::vector<validation_rule*> value_rules;
  value_rules = parse_field(((t_map*)type)->get_val_type(), value_annotations);
  for (auto it = value_rules.begin(); it != value_rules.end(); it++) {
    validation_rule* rule = new validation_rule(value_prefix, *it);
    rules.push_back(rule);
  }
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_struct_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  (void)type;
  std::vector<validation_rule*> rules;
  add_bool_rule(rules, "vt.skip", annotations);
  return rules;
}

std::vector<validation_rule*> validation_parser::parse_xception_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  return parse_struct_field(type, annotations);
}

std::vector<validation_rule*> validation_parser::parse_union_field(
    t_type* type,
    std::map<std::string, std::vector<std::string>>& annotations) {
  return parse_struct_field(type, annotations);
}

bool validation_parser::is_reference_field(std::string value) {
  if (value[0] != '$') {
    return false;
  }
  value.erase(value.begin());
  t_field* field = this->reference->get_field_by_name(value);
  return field != nullptr;
}

bool validation_parser::is_validation_function(std::string value) {
  if (value[0] != '@') {
    return false;
  }
  return true;
}

void validation_parser::add_bool_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      validation_rule* rule = new validation_rule(key);
      validation_value* value;
      if (is_reference_field(annotation_value)) {
        t_field* field = get_referenced_field(annotation_value);
        value = new validation_value(field);
      } else {
        bool constant;
        std::istringstream(it->second.back()) >> std::boolalpha >> constant;
        value = new validation_value(constant);
      }
      rule->append_value(value);
      rules.push_back(rule);
    }
  }
}

void validation_parser::add_double_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      if (annotation_value.size() == 0) {
        continue;
      }
      validation_rule* rule = new validation_rule(key);
      validation_value* value;
      if (is_validation_function(annotation_value)) {
        validation_value::validation_function* function = get_validation_function(annotation_value);
        value = new validation_value(function);
      } else if (is_reference_field(annotation_value)) {
        t_field* field = get_referenced_field(annotation_value);
        value = new validation_value(field);
      } else {
        double constant = std::stod(annotation_value);
        value = new validation_value(constant);
      }
      rule->append_value(value);
      rules.push_back(rule);
    }
  }
}

void validation_parser::add_enum_list_rule(
    std::vector<validation_rule*>& rules,
    t_enum* enum_,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      if (annotation_value.size() == 0) {
        continue;
      }
      validation_rule* rule = new validation_rule(key);
      if (annotation_value[0] == '[') {
        validation_value* value;
        char* str = strdup(annotation_value.c_str());
        char* pch = strtok(str, list_delimiter);
        std::string val;
        while (pch != NULL) {
          std::string temp(pch);
          if (is_validation_function(temp)) {
            validation_value::validation_function* function = get_validation_function(temp);
            value = new validation_value(function);
          } else if (is_reference_field(temp)) {
            t_field* field = get_referenced_field(temp);
            value = new validation_value(field);
          } else if (std::stringstream(temp) >> val) {
            std::string::size_type dot = val.rfind('.');
            if (dot != std::string::npos) {
              val = val.substr(dot + 1);
            }
            t_enum_value* enum_val = enum_->get_constant_by_name(val);
            value = new validation_value(enum_val);
          } else {
            delete rule;
            throw "validator error: validation double list parse failed: " + temp;
          }
          rule->append_value(value);
          pch = strtok(NULL, list_delimiter);
        }
      } else {
        validation_value* value;
        std::string val = annotation_value;
        std::string::size_type dot = val.rfind('.');
        if (dot != std::string::npos) {
          val = val.substr(dot + 1);
        }
        t_enum_value* enum_val = enum_->get_constant_by_name(val);
        value = new validation_value(enum_val);
        rule->append_value(value);
      }
      rules.push_back(rule);
    }
  }
}

void validation_parser::add_double_list_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  std::map<std::string, std::vector<std::string>> double_rules;
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      if (annotation_value.size() == 0) {
        continue;
      }
      if (annotation_value[0] == '[') {
        validation_rule* rule = new validation_rule(key);
        validation_value* value;
        char* str = strdup(annotation_value.c_str());
        char* pch = strtok(str, list_delimiter);
        double val;
        while (pch != NULL) {
          std::string temp(pch);
          if (is_validation_function(temp)) {
            validation_value::validation_function* function = get_validation_function(temp);
            value = new validation_value(function);
          } else if (is_reference_field(temp)) {
            t_field* field = get_referenced_field(temp);
            value = new validation_value(field);
          } else if (std::stringstream(temp) >> val) {
            value = new validation_value(val);
          } else {
            delete rule;
            throw "validator error: validation double list parse failed: " + temp;
          }
          rule->append_value(value);
          pch = strtok(NULL, list_delimiter);
        }
        rules.push_back(rule);
      } else {
        double_rules[key].push_back(annotation_value);
      }
    }
  }
  if (double_rules[key].size() > 0) {
    add_double_rule(rules, key, double_rules);
  }
}

void validation_parser::add_integer_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      if (annotation_value.size() == 0) {
        continue;
      }
      validation_rule* rule = new validation_rule(key);
      validation_value* value;
      if (is_reference_field(annotation_value)) {
        t_field* field = get_referenced_field(annotation_value);
        value = new validation_value(field);
      } else if (is_validation_function(annotation_value)) {
        validation_value::validation_function* function = get_validation_function(annotation_value);
        value = new validation_value(function);
      } else {
        int64_t constant = std::stoll(annotation_value);
        value = new validation_value(constant);
      }
      rule->append_value(value);
      rules.push_back(rule);
    }
  }
}

void validation_parser::add_integer_list_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  std::map<std::string, std::vector<std::string>> integer_rules;
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      if (annotation_value.size() == 0) {
        continue;
      }
      if (annotation_value[0] == '[') {
        validation_rule* rule = new validation_rule(key);
        validation_value* value;
        char* str = strdup(annotation_value.c_str());
        char* pch = strtok(str, list_delimiter);
        int64_t val;
        while (pch != NULL) {
          std::string temp(pch);
          if (is_validation_function(temp)) {
            validation_value::validation_function* function = get_validation_function(temp);
            value = new validation_value(function);
          } else if (is_reference_field(temp)) {
            t_field* field = get_referenced_field(temp);
            value = new validation_value(field);
          } else if (std::stringstream(temp) >> val) {
            value = new validation_value(val);
          } else {
            delete rule;
            throw "validator error: validation integer list parse failed: " + temp;
          }
          rule->append_value(value);
          pch = strtok(NULL, list_delimiter);
        }
        rules.push_back(rule);
      } else {
        integer_rules[key].push_back(annotation_value);
      }
    }
  }
  if (integer_rules[key].size() > 0) {
    add_integer_rule(rules, key, integer_rules);
  }
}

void validation_parser::add_string_rule(
    std::vector<validation_rule*>& rules,
    std::string key,
    std::map<std::string, std::vector<std::string>>& annotations) {
  auto it = annotations.find(key);
  if (it != annotations.end() && !it->second.empty()) {
    for (auto& annotation_value : it->second) {
      validation_rule* rule = new validation_rule(key);
      validation_value* value;
      if (is_reference_field(annotation_value)) {
        t_field* field = get_referenced_field(annotation_value);
        value = new validation_value(field);
      } else {
        value = new validation_value(annotation_value);
      }
      rule->append_value(value);
      rules.push_back(rule);
    }
  }
}

t_field* validation_parser::get_referenced_field(std::string annotation_value) {
  annotation_value.erase(annotation_value.begin());
  return reference->get_field_by_name(annotation_value);
}

validation_value::validation_function* validation_parser::get_validation_function(
    std::string annotation_value) {
  std::string value = annotation_value;
  value.erase(value.begin());
  validation_value::validation_function* function = new validation_value::validation_function;

  size_t name_end = value.find_first_of('(');
  if (name_end >= value.size()) {
    delete function;
    throw "validator error: validation function parse failed: " + annotation_value;
  }
  function->name = value.substr(0, name_end);
  value.erase(0, name_end + 1); // name(

  if (function->name == "len") {
    size_t argument_end = value.find_first_of(')');
    if (argument_end >= value.size()) {
      delete function;
      throw "validator error: validation function parse failed: " + annotation_value;
    }
    std::string argument = value.substr(0, argument_end);
    if (argument.size() > 0 && argument[0] == '$') {
      t_field* field = get_referenced_field(argument);
      validation_value* value = new validation_value(field);
      function->arguments.push_back(value);
    } else {
      delete function;
      throw "validator error: validation function parse failed, unrecognized argument: "
          + annotation_value;
    }
  } else {
    delete function;
    throw "validator error: validation function parse failed, function not supported: "
        + annotation_value;
  }
  return function;
}