|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +#include <thrift/compiler/ast/t_exception.h> |
| 18 | +#include <thrift/compiler/ast/t_program.h> |
| 19 | +#include <thrift/compiler/ast/t_union.h> |
17 | 20 | #include <thrift/compiler/lib/schematizer.h>
|
18 | 21 |
|
19 | 22 | namespace apache {
|
20 | 23 | namespace thrift {
|
21 | 24 | namespace compiler {
|
22 |
| -std::unique_ptr<t_const_value> schematizer::gen_schema( |
23 |
| - const t_structured& node) { |
24 |
| - auto schema = std::make_unique<t_const_value>(); |
25 |
| - schema->set_map(); |
| 25 | +namespace { |
| 26 | +template <typename... Args> |
| 27 | +std::unique_ptr<t_const_value> val(Args... args) { |
| 28 | + return std::make_unique<t_const_value>(std::forward<Args>(args)...); |
| 29 | +} |
26 | 30 |
|
27 |
| - // Definition |
28 |
| - auto definition = std::make_unique<t_const_value>(); |
| 31 | +void add_definition(t_const_value& schema, const t_named& node) { |
| 32 | + auto definition = val(); |
29 | 33 | definition->set_map();
|
30 |
| - definition->add_map( |
31 |
| - std::make_unique<t_const_value>("name"), |
32 |
| - std::make_unique<t_const_value>(node.name())); |
33 |
| - definition->add_map( |
34 |
| - std::make_unique<t_const_value>("uri"), |
35 |
| - std::make_unique<t_const_value>(node.uri())); |
| 34 | + definition->add_map(val("name"), val(node.name())); |
| 35 | + if (!node.uri().empty()) { |
| 36 | + definition->add_map(val("uri"), val(node.uri())); |
| 37 | + } |
36 | 38 | // TODO: annotations
|
37 |
| - schema->add_map( |
38 |
| - std::make_unique<t_const_value>("definition"), std::move(definition)); |
| 39 | + schema.add_map(val("definition"), std::move(definition)); |
| 40 | +} |
| 41 | + |
| 42 | +std::unique_ptr<t_const_value> gen_type(const t_type& type) { |
| 43 | + auto schema = val(); |
| 44 | + schema->set_map(); |
| 45 | + auto type_name = val(); |
| 46 | + type_name->set_map(); |
| 47 | + std::unique_ptr<t_const_value> params; |
| 48 | + switch (type.get_type_value()) { |
| 49 | + case t_type::type::t_bool: |
| 50 | + type_name->add_map(val("boolType"), val(0)); |
| 51 | + break; |
| 52 | + case t_type::type::t_byte: |
| 53 | + type_name->add_map(val("byteType"), val(0)); |
| 54 | + break; |
| 55 | + case t_type::type::t_i16: |
| 56 | + type_name->add_map(val("i16Type"), val(0)); |
| 57 | + break; |
| 58 | + case t_type::type::t_i32: |
| 59 | + type_name->add_map(val("i32Type"), val(0)); |
| 60 | + break; |
| 61 | + case t_type::type::t_i64: |
| 62 | + type_name->add_map(val("i64Type"), val(0)); |
| 63 | + break; |
| 64 | + case t_type::type::t_double: |
| 65 | + type_name->add_map(val("doubleType"), val(0)); |
| 66 | + break; |
| 67 | + case t_type::type::t_float: |
| 68 | + type_name->add_map(val("floatType"), val(0)); |
| 69 | + break; |
| 70 | + case t_type::type::t_string: |
| 71 | + type_name->add_map(val("stringType"), val(0)); |
| 72 | + break; |
| 73 | + case t_type::type::t_binary: |
| 74 | + type_name->add_map(val("binaryType"), val(0)); |
| 75 | + break; |
| 76 | + case t_type::type::t_list: |
| 77 | + type_name->add_map(val("listType"), val(0)); |
| 78 | + params = val(); |
| 79 | + params->set_list(); |
| 80 | + params->add_list(gen_type( |
| 81 | + *static_cast<const t_list&>(type).elem_type()->get_true_type())); |
| 82 | + break; |
| 83 | + case t_type::type::t_set: |
| 84 | + type_name->add_map(val("setType"), val(0)); |
| 85 | + params = val(); |
| 86 | + params->set_list(); |
| 87 | + params->add_list(gen_type( |
| 88 | + *static_cast<const t_set&>(type).elem_type()->get_true_type())); |
| 89 | + break; |
| 90 | + case t_type::type::t_map: |
| 91 | + type_name->add_map(val("mapType"), val(0)); |
| 92 | + params = val(); |
| 93 | + params->set_list(); |
| 94 | + { |
| 95 | + const auto& map = static_cast<const t_map&>(type); |
| 96 | + params->add_list(gen_type(*map.key_type()->get_true_type())); |
| 97 | + params->add_list(gen_type(*map.val_type()->get_true_type())); |
| 98 | + } |
| 99 | + break; |
| 100 | + case t_type::type::t_enum: |
| 101 | + type_name->add_map(val("enumType"), val(type.uri())); |
| 102 | + break; |
| 103 | + case t_type::type::t_struct: |
| 104 | + type_name->add_map( |
| 105 | + val([&] { |
| 106 | + if (dynamic_cast<const t_union*>(&type)) { |
| 107 | + return "unionType"; |
| 108 | + } else if (dynamic_cast<const t_exception*>(&type)) { |
| 109 | + return "exceptionType"; |
| 110 | + } else { |
| 111 | + return "structType"; |
| 112 | + } |
| 113 | + }()), |
| 114 | + val(type.uri())); |
| 115 | + break; |
| 116 | + default: |
| 117 | + assert(false); |
| 118 | + } |
| 119 | + schema->add_map(val("name"), std::move(type_name)); |
| 120 | + if (params) { |
| 121 | + schema->add_map(val("params"), std::move(params)); |
| 122 | + } |
| 123 | + return schema; |
| 124 | +} |
| 125 | +} // namespace |
| 126 | + |
| 127 | +std::unique_ptr<t_const_value> schematizer::gen_schema( |
| 128 | + const t_structured& node) { |
| 129 | + auto schema = val(); |
| 130 | + schema->set_map(); |
| 131 | + add_definition(*schema, node); |
39 | 132 |
|
40 |
| - // Fields |
41 |
| - auto fields = std::make_unique<t_const_value>(); |
| 133 | + auto fields = val(); |
42 | 134 | fields->set_list();
|
| 135 | + // May be null in unit tests. |
| 136 | + const auto* program = node.program(); |
| 137 | + const auto* field_qualifier_enum = program |
| 138 | + ? dynamic_cast<const t_enum*>(program->scope()->find_def( |
| 139 | + "facebook.com/thrift/type/FieldQualifier")) |
| 140 | + : nullptr; |
43 | 141 |
|
44 |
| - // TODO(iahs): fill in fields |
| 142 | + for (const auto& field : node.fields()) { |
| 143 | + auto field_schema = val(); |
| 144 | + field_schema->set_map(); |
| 145 | + add_definition(*field_schema, field); |
| 146 | + field_schema->add_map(val("id"), val(field.id())); |
| 147 | + auto qualifierVal = [&] { |
| 148 | + switch (field.qualifier()) { |
| 149 | + case t_field_qualifier::none: |
| 150 | + case t_field_qualifier::required: |
| 151 | + return 3; // Fill |
| 152 | + case t_field_qualifier::optional: |
| 153 | + return 1; // Optional |
| 154 | + case t_field_qualifier::terse: |
| 155 | + return 2; // Terse |
| 156 | + } |
| 157 | + assert(false); |
| 158 | + return 0; // Default |
| 159 | + }(); |
| 160 | + auto qualifier = val(); |
| 161 | + if (field_qualifier_enum) { |
| 162 | + qualifier->set_is_enum(); |
| 163 | + qualifier->set_enum(field_qualifier_enum); |
| 164 | + qualifier->set_enum_value(field_qualifier_enum->find_value(qualifierVal)); |
| 165 | + } else { |
| 166 | + qualifier->set_integer(qualifierVal); |
| 167 | + } |
| 168 | + field_schema->add_map(val("qualifier"), std::move(qualifier)); |
| 169 | + field_schema->add_map( |
| 170 | + val("type"), gen_type(*field.type()->get_true_type())); |
| 171 | + // TODO: default |
| 172 | + fields->add_list(std::move(field_schema)); |
| 173 | + } |
45 | 174 |
|
46 |
| - schema->add_map(std::make_unique<t_const_value>("fields"), std::move(fields)); |
| 175 | + schema->add_map(val("fields"), std::move(fields)); |
47 | 176 |
|
48 | 177 | return schema;
|
49 | 178 | }
|
|
0 commit comments