-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathApiEmitter.cs
More file actions
509 lines (443 loc) · 21.3 KB
/
Copy pathApiEmitter.cs
File metadata and controls
509 lines (443 loc) · 21.3 KB
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
using System.Collections.Generic;
using System.Text;
using Telegram.Generators.Schema;
namespace Telegram.Generators.Emit
{
/// <summary>
/// Writes the C# for one schema type: the class, its ToJson when it can be sent, and its
/// FromJson when it can be received.
///
/// The output of this file is compiled by the app, so every change here should be checked by
/// regenerating and diffing against the previous output - see Telegram.Benchmarks/README.md.
/// </summary>
internal static class ApiEmitter
{
/// <summary>
/// updateFile and file are re-entered through the handler so the app can dedupe them:
/// they arrive constantly during downloads and are the one place object identity pays.
/// </summary>
private static bool IsHandledByClient(string className)
{
return className == "UpdateFile" || className == "File";
}
public static void WriteClass(StringBuilder builder, SchemaClass type, bool serializable)
{
var className = Naming.ToPascalCase(type.Name);
var baseName = Naming.ToPascalCase(type.IsFunction ? "Function" : type.ReturnType ?? "Object");
builder.AppendLine("/// <summary>");
builder.AppendLine("/// " + type.Description);
if (type.IsFunction)
{
builder.AppendLine("/// <para>Returns <see cref=\"T:Telegram.Td.Api." + Naming.ToPascalCase(type.ReturnType) + "\"/>.</para>");
}
builder.AppendLine("/// </summary>");
if (type.IsProxy)
{
builder.AppendLine("public abstract partial class " + className + " : " + baseName);
builder.AppendLine("{");
builder.AppendLine("}");
return;
}
builder.AppendLine("public partial class " + className + " : " + baseName);
builder.AppendLine("{");
foreach (var prop in type.Properties)
{
var fieldName = Naming.PropertyName(prop, className);
builder.AppendLine(" /// <summary>");
builder.AppendLine(" /// " + prop.Description);
builder.AppendLine(" /// </summary>");
// Strings default to empty rather than null, which is what lets TDLib omit them
// from the wire without every consumer having to null-check.
if (prop.Type == "string" && !prop.IsVector)
{
builder.AppendLine(" public " + Naming.PropertyType(prop) + " " + fieldName + " { get; set; } = string.Empty;");
}
else
{
builder.AppendLine(" public " + Naming.PropertyType(prop) + " " + fieldName + " { get; set; }");
}
}
if (!type.IsFunction || type.Properties.Count == 0)
{
builder.AppendLine();
builder.AppendLine(" /// <summary>");
builder.AppendLine(" /// " + type.Description);
if (type.IsFunction)
{
builder.AppendLine(" /// <para>Returns <see cref=\"T:Telegram.Td.Api." + Naming.ToPascalCase(type.ReturnType) + "\"/>.</para>");
}
builder.AppendLine(" /// </summary>");
builder.AppendLine(" public " + className + "()");
builder.AppendLine(" {");
builder.AppendLine(" }");
}
if (type.Properties.Count > 0)
{
WriteConstructor(builder, type, className);
}
if (serializable)
{
WriteToJson(builder, type, className);
}
builder.AppendLine("}");
}
private static void WriteConstructor(StringBuilder builder, SchemaClass type, string className)
{
builder.AppendLine();
builder.AppendLine(" /// <summary>");
builder.AppendLine(" /// " + type.Description);
if (type.IsFunction)
{
builder.AppendLine(" /// <para>Returns <see cref=\"T:Telegram.Td.Api." + Naming.ToPascalCase(type.ReturnType) + "\"/>.</para>");
}
builder.AppendLine(" /// </summary>");
foreach (var prop in type.Properties)
{
builder.AppendLine(" /// <param name=\"" + Naming.ParameterName(prop) + "\">" + prop.Description + "</param>");
}
builder.Append(" public " + className + "(");
for (int i = 0; i < type.Properties.Count; i++)
{
if (i > 0)
{
builder.Append(", ");
}
var property = type.Properties[i];
builder.Append(Naming.PropertyType(property) + " " + Naming.ParameterName(property));
}
builder.AppendLine(")");
builder.AppendLine(" {");
foreach (var property in type.Properties)
{
builder.AppendLine(" " + Naming.PropertyName(property, className) + " = " + Naming.ParameterName(property) + ";");
}
builder.AppendLine(" }");
}
private static void WriteToJson(StringBuilder builder, SchemaClass type, string className)
{
builder.AppendLine();
builder.AppendLine(" public override void ToJson(Utf8JsonWriter writer)");
builder.AppendLine(" {");
builder.AppendLine(" writer.WriteString(\"@type\"u8, \"" + type.Name + "\"u8);");
foreach (var prop in type.Properties)
{
var fieldName = Naming.PropertyName(prop, className);
if (prop.IsVector)
{
builder.AppendLine(" writer.WriteArray(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else if (prop.Type == "Bool")
{
builder.AppendLine(" writer.WriteBoolean(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else if (prop.Type == "int32" || prop.Type == "int53" || prop.Type == "double")
{
builder.AppendLine(" writer.WriteNumber(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else if (prop.Type == "int64")
{
// int64 goes over the wire quoted: it exceeds what JSON numbers represent
// exactly, and JavaScript clients would round it.
builder.AppendLine(" writer.WriteNumberString(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else if (prop.Type == "string")
{
builder.AppendLine(" writer.WriteString(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else if (prop.Type == "bytes")
{
builder.AppendLine(" writer.WriteBase64String(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
else
{
builder.AppendLine(" writer.WriteObject(\"" + prop.Name + "\"u8, " + fieldName + ");");
}
}
builder.AppendLine(" }");
}
/// <summary>
/// The dispatcher for an abstract type: reads @type and hands off to the constructor's own
/// parser.
/// </summary>
public static void WriteAbstractDispatcher(StringBuilder builder, string name, List<SchemaClass> classes)
{
var className = Naming.ToPascalCase(name);
builder.AppendLine("private static " + className + " FromJson_" + className + "(ref Utf8JsonReader reader, ClientResultHandler handler)");
builder.AppendLine("{");
builder.AppendLine(" return FromJson(ref reader, handler, Handler);");
builder.AppendLine();
builder.AppendLine(" static " + className + "? Handler(ref Utf8JsonReader reader, ClientResultHandler handler, uint hash)");
builder.AppendLine(" {");
builder.AppendLine(" switch(hash)");
builder.AppendLine(" {");
foreach (var clazz in classes)
{
WriteDispatchCase(builder, clazz);
}
builder.AppendLine(" default: return null;");
builder.AppendLine(" }");
builder.AppendLine(" }");
builder.AppendLine("}");
}
/// <summary>One case of a @type switch, keyed by the CRC32 of the constructor name.</summary>
public static void WriteDispatchCase(StringBuilder builder, SchemaClass type)
{
if (type.IsFunction || type.IsProxy)
{
return;
}
var crc32 = Crc32.Compute(type.Name);
builder.AppendLine(" case " + crc32 + ":");
builder.AppendLine(" return FromJson_" + Naming.ToPascalCase(type.Name) + "(ref reader, handler);");
}
/// <summary>The parser for one concrete type: a loop over its fields, keyed by CRC32.</summary>
public static void WriteFromJson(StringBuilder builder, SchemaClass type)
{
var className = Naming.ToPascalCase(type.Name);
builder.AppendLine("private static " + className + " FromJson_" + className + "(ref Utf8JsonReader reader, ClientResultHandler handler)");
builder.AppendLine("{");
if (IsHandledByClient(className))
{
builder.AppendLine(" return handler.Parse" + className + "(ref reader);");
}
else if (type.IsProxy)
{
builder.AppendLine(" return FromJson<" + className + ">(ref reader, handler);");
}
else if (type.Properties.Count > 0)
{
builder.AppendLine(" return ParseObject(ref reader, new " + className + "(), handler, Handler);");
builder.AppendLine();
builder.AppendLine(" static bool Handler(ref Utf8JsonReader reader, ClientResultHandler handler, " + className + " obj, uint hash)");
builder.AppendLine(" {");
builder.AppendLine(" switch (hash)");
builder.AppendLine(" {");
foreach (var prop in type.Properties)
{
builder.AppendLine(" case " + Crc32.Compute(prop.Name) + ":");
builder.AppendLine(" obj." + Naming.PropertyName(prop, className) + " = " + ReadExpression(prop) + ";");
builder.AppendLine(" return true;");
}
builder.AppendLine(" default: return false;");
builder.AppendLine(" }");
builder.AppendLine(" }");
}
else
{
builder.AppendLine(" reader.ReadStartObject();");
builder.AppendLine(" return new " + className + "();");
}
builder.AppendLine("}");
}
/// <summary>
/// The pointer-reader dispatcher for an abstract type. Same CRC32 cases as the
/// Utf8JsonReader version - TdJsonReader.ValueCrc32 computes the identical value.
/// </summary>
public static void WritePtrAbstractDispatcher(StringBuilder builder, string name, List<SchemaClass> classes)
{
var className = Naming.ToPascalCase(name);
builder.AppendLine("private static " + className + " FromPtr_" + className + "(ref TdJsonReader reader, ClientResultHandler handler)");
builder.AppendLine("{");
builder.AppendLine(" return FromPtr(ref reader, handler, Handler);");
builder.AppendLine();
builder.AppendLine(" static " + className + "? Handler(ref TdJsonReader reader, ClientResultHandler handler, uint hash)");
builder.AppendLine(" {");
builder.AppendLine(" switch(hash)");
builder.AppendLine(" {");
foreach (var clazz in classes)
{
if (clazz.IsFunction || clazz.IsProxy)
{
continue;
}
builder.AppendLine(" case " + Crc32.Compute(clazz.Name) + ":");
builder.AppendLine(" return FromPtr_" + Naming.ToPascalCase(clazz.Name) + "(ref reader, handler);");
}
builder.AppendLine(" default: return null;");
builder.AppendLine(" }");
builder.AppendLine(" }");
builder.AppendLine("}");
}
public static void WritePtrDispatchCase(StringBuilder builder, SchemaClass type)
{
if (type.IsFunction || type.IsProxy)
{
return;
}
builder.AppendLine(" case " + Crc32.Compute(type.Name) + ":");
builder.AppendLine(" return FromPtr_" + Naming.ToPascalCase(type.Name) + "(ref reader, handler);");
}
/// <summary>
/// The pointer parser for one concrete type. Fields dispatch on name length then an exact
/// compare rather than a hash: there are few enough per class for that to be cheap, it
/// cannot collide with an unknown field, and it keeps the comparison inside a library call
/// instead of a hand-written loop over a span.
/// </summary>
public static void WriteFromPtr(StringBuilder builder, SchemaClass type)
{
var className = Naming.ToPascalCase(type.Name);
builder.AppendLine("private static " + className + " FromPtr_" + className + "(ref TdJsonReader reader, ClientResultHandler handler)");
builder.AppendLine("{");
if (IsHandledByClient(className))
{
builder.AppendLine(" return handler.Parse" + className + "(ref reader);");
}
else if (type.IsProxy)
{
builder.AppendLine(" return FromPtr<" + className + ">(ref reader, handler, null);");
}
else if (type.Properties.Count > 0)
{
builder.AppendLine(" var obj = new " + className + "();");
builder.AppendLine(" ReadStartObjectPtr(ref reader);");
builder.AppendLine();
builder.AppendLine(" while (reader.TokenType == JsonTokenType.PropertyName)");
builder.AppendLine(" {");
builder.AppendLine(" var name = reader.ValueSpan;");
builder.AppendLine(" reader.Read();");
builder.AppendLine();
builder.AppendLine(" switch (name.Length)");
builder.AppendLine(" {");
foreach (var group in GroupByNameLength(type.Properties))
{
builder.AppendLine(" case " + group.Key + ":");
var first = true;
foreach (var prop in group.Value)
{
builder.AppendLine(" " + (first ? "if" : "else if") +
" (name.SequenceEqual(\"" + prop.Name + "\"u8)) obj." + Naming.PropertyName(prop, className) +
" = " + ReadPtrExpression(prop) + ";");
first = false;
}
builder.AppendLine(" break;");
}
builder.AppendLine(" }");
builder.AppendLine();
builder.AppendLine(" if (reader.TokenType == JsonTokenType.StartObject || reader.TokenType == JsonTokenType.StartArray)");
builder.AppendLine(" {");
builder.AppendLine(" reader.Skip();");
builder.AppendLine(" }");
builder.AppendLine();
builder.AppendLine(" reader.Read();");
builder.AppendLine(" }");
builder.AppendLine();
builder.AppendLine(" return obj;");
}
else
{
builder.AppendLine(" ReadStartObjectPtr(ref reader);");
builder.AppendLine(" return new " + className + "();");
}
builder.AppendLine("}");
}
/// <summary>Fields bucketed by the byte length of their name, in ascending order.</summary>
private static List<KeyValuePair<int, List<SchemaProperty>>> GroupByNameLength(List<SchemaProperty> properties)
{
var groups = new SortedDictionary<int, List<SchemaProperty>>();
foreach (var property in properties)
{
// Field names are ASCII throughout the scheme, so length in bytes is length in
// chars - which is what the reader compares against.
if (!groups.TryGetValue(property.Name.Length, out var group))
{
groups[property.Name.Length] = group = new List<SchemaProperty>();
}
group.Add(property);
}
return new List<KeyValuePair<int, List<SchemaProperty>>>(groups);
}
private static string ReadPtrExpression(SchemaProperty prop)
{
if (prop.IsVector)
{
switch (prop.Type)
{
case "Bool":
return "GetBooleanArrayPtr(ref reader)";
case "int32":
return "GetInt32ArrayPtr(ref reader)";
case "int53":
return "GetInt64ArrayPtr(ref reader)";
case "int64":
return "GetInt64StringArrayPtr(ref reader)";
case "double":
return "GetDoubleArrayPtr(ref reader)";
case "string":
return "GetStringArrayPtr(ref reader)";
case "bytes":
return "GetBase64StringArrayPtr(ref reader)";
default:
return prop.IsVectorOfVectors
? "GetObjectArrayArrayPtr(ref reader, handler, FromPtr_" + Naming.ToPascalCase(prop.Type) + ")"
: "GetObjectArrayPtr(ref reader, handler, FromPtr_" + Naming.ToPascalCase(prop.Type) + ")";
}
}
switch (prop.Type)
{
case "Bool":
return "reader.GetBoolean()";
case "int32":
return "reader.GetInt32()";
case "int53":
return "reader.GetInt64()";
case "int64":
return "reader.GetInt64String()";
case "double":
return "reader.GetDouble()";
case "string":
return "reader.GetString()";
case "bytes":
return "reader.GetBytesFromBase64()";
default:
return "FromPtr_" + Naming.ToPascalCase(prop.Type) + "(ref reader, handler)";
}
}
/// <summary>The reader call that produces one field's value.</summary>
private static string ReadExpression(SchemaProperty prop)
{
if (prop.IsVector)
{
switch (prop.Type)
{
case "Bool":
return "reader.GetBooleanArray()";
case "int32":
return "reader.GetInt32Array()";
case "int53":
return "reader.GetInt64Array()";
case "int64":
return "reader.GetInt64StringArray()";
case "double":
return "reader.GetDoubleArray()";
case "string":
return "reader.GetStringArray()";
case "bytes":
return "reader.GetBase64StringArray()";
default:
return prop.IsVectorOfVectors
? "reader.GetObjectArrayArray(handler, FromJson_" + Naming.ToPascalCase(prop.Type) + ")"
: "reader.GetObjectArray(handler, FromJson_" + Naming.ToPascalCase(prop.Type) + ")";
}
}
switch (prop.Type)
{
case "Bool":
return "reader.GetBoolean()";
case "int32":
return "reader.GetInt32()";
case "int53":
return "reader.GetInt64()";
case "int64":
return "reader.GetInt64String()";
case "double":
return "reader.GetDouble()";
case "string":
return "reader.GetString()";
case "bytes":
return "reader.GetBytesFromBase64()";
default:
return "FromJson_" + Naming.ToPascalCase(prop.Type) + "(ref reader, handler)";
}
}
}
}