11
11
#include " td/utils/buffer.h"
12
12
#include " td/utils/common.h"
13
13
#include " td/utils/filesystem.h"
14
+ #include " td/utils/misc.h"
14
15
#include " td/utils/Slice.h"
15
16
#include " td/utils/SliceBuilder.h"
16
17
#include " td/utils/StringBuilder.h"
@@ -26,6 +27,14 @@ static bool need_bytes(const tl::simple::Type *type) {
26
27
(type->type == tl::simple::Type::Vector && need_bytes (type->vector_value_type ));
27
28
}
28
29
30
+ static bool is_suitable (int file_number, int file_count, int &counter) {
31
+ if (file_count <= 1 ) {
32
+ return true ;
33
+ }
34
+ counter++;
35
+ return counter % (file_count - 1 ) == file_number - 1 ;
36
+ }
37
+
29
38
template <class T >
30
39
void gen_to_json_constructor (StringBuilder &sb, const T *constructor, bool is_header) {
31
40
sb << " void to_json(JsonValueScope &jv, "
@@ -71,11 +80,15 @@ void gen_to_json_constructor(StringBuilder &sb, const T *constructor, bool is_he
71
80
sb << " }\n\n " ;
72
81
}
73
82
74
- void gen_to_json (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) {
83
+ void gen_to_json (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode, int file_number,
84
+ int file_count, int &counter) {
75
85
for (auto *custom_type : schema.custom_types ) {
76
86
if (!((custom_type->is_query_ && mode != Mode::Server) || (custom_type->is_result_ && mode != Mode::Client))) {
77
87
continue ;
78
88
}
89
+ if (!is_suitable (file_number, file_count, counter)) {
90
+ continue ;
91
+ }
79
92
if (custom_type->constructors .size () > 1 ) {
80
93
auto type_name = tl::simple::gen_cpp_name (custom_type->name );
81
94
sb << " void to_json(JsonValueScope &jv, const td_api::" << type_name << " &object)" ;
@@ -97,7 +110,9 @@ void gen_to_json(StringBuilder &sb, const tl::simple::Schema &schema, bool is_he
97
110
return ;
98
111
}
99
112
for (auto *function : schema.functions ) {
100
- gen_to_json_constructor (sb, function, is_header);
113
+ if (is_suitable (file_number, file_count, counter)) {
114
+ gen_to_json_constructor (sb, function, is_header);
115
+ }
101
116
}
102
117
}
103
118
@@ -118,20 +133,25 @@ void gen_from_json_constructor(StringBuilder &sb, const T *constructor, bool is_
118
133
}
119
134
}
120
135
121
- void gen_from_json (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) {
136
+ void gen_from_json (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode, int file_number,
137
+ int file_count, int &counter) {
122
138
for (auto *custom_type : schema.custom_types ) {
123
139
if (!((custom_type->is_query_ && mode != Mode::Client) || (custom_type->is_result_ && mode != Mode::Server))) {
124
140
continue ;
125
141
}
126
142
for (auto *constructor : custom_type->constructors ) {
127
- gen_from_json_constructor (sb, constructor, is_header);
143
+ if (is_suitable (file_number, file_count, counter)) {
144
+ gen_from_json_constructor (sb, constructor, is_header);
145
+ }
128
146
}
129
147
}
130
148
if (mode == Mode::Client) {
131
149
return ;
132
150
}
133
151
for (auto *function : schema.functions ) {
134
- gen_from_json_constructor (sb, function, is_header);
152
+ if (is_suitable (file_number, file_count, counter)) {
153
+ gen_from_json_constructor (sb, function, is_header);
154
+ }
135
155
}
136
156
}
137
157
@@ -163,7 +183,8 @@ void gen_tl_constructor_from_string(StringBuilder &sb, Slice name, const Vec &ve
163
183
sb << " }\n\n " ;
164
184
}
165
185
166
- void gen_tl_constructor_from_string (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode) {
186
+ void gen_tl_constructor_from_string (StringBuilder &sb, const tl::simple::Schema &schema, bool is_header, Mode mode,
187
+ int file_number, int file_count, int &counter) {
167
188
Vec vec_for_nullary;
168
189
for (auto *custom_type : schema.custom_types ) {
169
190
if (!((custom_type->is_query_ && mode != Mode::Client) || (custom_type->is_result_ && mode != Mode::Server))) {
@@ -176,9 +197,14 @@ void gen_tl_constructor_from_string(StringBuilder &sb, const tl::simple::Schema
176
197
}
177
198
178
199
if (vec.size () > 1 ) {
179
- gen_tl_constructor_from_string (sb, tl::simple::gen_cpp_name (custom_type->name ), vec, is_header);
200
+ if (is_suitable (file_number, file_count, counter)) {
201
+ gen_tl_constructor_from_string (sb, tl::simple::gen_cpp_name (custom_type->name ), vec, is_header);
202
+ }
180
203
}
181
204
}
205
+ if (file_number != 1 % file_count) {
206
+ return ;
207
+ }
182
208
gen_tl_constructor_from_string (sb, " Object" , vec_for_nullary, is_header);
183
209
184
210
if (mode == Mode::Client) {
@@ -192,8 +218,12 @@ void gen_tl_constructor_from_string(StringBuilder &sb, const tl::simple::Schema
192
218
}
193
219
194
220
void gen_json_converter_file (const tl::simple::Schema &schema, const std::string &file_name_base, bool is_header,
195
- Mode mode) {
196
- auto file_name = is_header ? file_name_base + " .h" : file_name_base + " .cpp" ;
221
+ Mode mode, int file_number, int file_count) {
222
+ string file_name_suffix;
223
+ if (file_count > 1 ) {
224
+ file_name_suffix = " _" + td::to_string (file_number);
225
+ }
226
+ auto file_name = is_header ? file_name_base + file_name_suffix + " .h" : file_name_base + file_name_suffix + " .cpp" ;
197
227
auto old_file_content = [&] {
198
228
auto r_content = read_file (file_name);
199
229
if (r_content.is_error ()) {
@@ -234,7 +264,7 @@ void gen_json_converter_file(const tl::simple::Schema &schema, const std::string
234
264
sb << " \n Status from_json(td_api::object_ptr<Function> &to, td::JsonValue from);\n " ;
235
265
sb << " \n void to_json(JsonValueScope &jv, const Object &object);\n " ;
236
266
sb << " \n void to_json(JsonValueScope &jv, const Function &object);\n\n " ;
237
- } else {
267
+ } else if (file_number == 0 ) {
238
268
sb << R"ABCD(
239
269
void to_json(JsonValueScope &jv, const td_api::object_ptr<Object> &value) {
240
270
td::to_json(jv, value);
@@ -264,9 +294,10 @@ void to_json(JsonValueScope &jv, const Function &object) {
264
294
265
295
)ABCD" ;
266
296
}
267
- gen_tl_constructor_from_string (sb, schema, is_header, mode);
268
- gen_from_json (sb, schema, is_header, mode);
269
- gen_to_json (sb, schema, is_header, mode);
297
+ int counter = 0 ;
298
+ gen_tl_constructor_from_string (sb, schema, is_header, mode, file_number, file_count, counter);
299
+ gen_from_json (sb, schema, is_header, mode, file_number, file_count, counter);
300
+ gen_to_json (sb, schema, is_header, mode, file_number, file_count, counter);
270
301
sb << " } // namespace td_api\n " ;
271
302
sb << " } // namespace td\n " ;
272
303
@@ -288,10 +319,12 @@ void to_json(JsonValueScope &jv, const Function &object) {
288
319
}
289
320
}
290
321
291
- void gen_json_converter (const tl::tl_config &config, const std::string &file_name, Mode mode) {
322
+ void gen_json_converter (const tl::tl_config &config, const std::string &file_name, Mode mode, int source_file_count ) {
292
323
tl::simple::Schema schema (config);
293
- gen_json_converter_file (schema, file_name, true , mode);
294
- gen_json_converter_file (schema, file_name, false , mode);
324
+ gen_json_converter_file (schema, file_name, true , mode, 0 , 1 );
325
+ for (int i = 0 ; i < source_file_count; i++) {
326
+ gen_json_converter_file (schema, file_name, false , mode, i, source_file_count);
327
+ }
295
328
}
296
329
297
330
} // namespace td
0 commit comments