This repository was archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfile_generator.dart
551 lines (473 loc) · 17.8 KB
/
file_generator.dart
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
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of protoc;
final _dartIdentifier = new RegExp(r'^\w+$');
final _formatter = new DartFormatter();
/// Generates the Dart output files for one .proto input file.
///
/// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart.
class FileGenerator extends ProtobufContainer {
/// Reads and the declared mixins in the file, keyed by name.
///
/// Performs some basic validation on declared mixins, e.g. whether names
/// are valid dart identifiers and whether there are cycles in the `parent`
/// hierarchy.
/// Does not check for existence of import files or classes.
static Map<String, PbMixin> _getDeclaredMixins(FileDescriptorProto desc) {
String mixinError(String error) =>
'Option "mixins" in ${desc.name}: $error';
if (!desc.hasOptions() ||
!desc.options.hasExtension(Dart_options.imports)) {
return <String, PbMixin>{};
}
var dartMixins = <String, DartMixin>{};
Imports importedMixins = desc.options.getExtension(Dart_options.imports);
for (DartMixin mixin in importedMixins.mixins) {
if (dartMixins.containsKey(mixin.name)) {
throw mixinError('Duplicate mixin name: "${mixin.name}"');
}
if (!mixin.name.startsWith(_dartIdentifier)) {
throw mixinError(
'"${mixin.name}" is not a valid dart class identifier');
}
if (mixin.hasParent() && !mixin.parent.startsWith(_dartIdentifier)) {
throw mixinError('Mixin parent "${mixin.parent}" of "${mixin.name}" is '
'not a valid dart class identifier');
}
dartMixins[mixin.name] = mixin;
}
// Detect cycles and unknown parents.
for (var mixin in dartMixins.values) {
if (!mixin.hasParent()) continue;
var currentMixin = mixin;
var parentChain = <String>[];
while (currentMixin.hasParent()) {
var parentName = currentMixin.parent;
bool declaredMixin = dartMixins.containsKey(parentName);
bool internalMixin = !declaredMixin && findMixin(parentName) != null;
if (internalMixin) break; // No further validation of parent chain.
if (!declaredMixin) {
throw mixinError('Unknown mixin parent "${mixin.parent}" of '
'"${currentMixin.name}"');
}
if (parentChain.contains(parentName)) {
var cycle = parentChain.join('->') + '->$parentName';
throw mixinError('Cycle in parent chain: $cycle');
}
parentChain.add(parentName);
currentMixin = dartMixins[parentName];
}
}
// Turn DartMixins into PbMixins.
final pbMixins = <String, PbMixin>{};
PbMixin resolveMixin(String name) {
if (pbMixins.containsKey(name)) return pbMixins[name];
if (dartMixins.containsKey(name)) {
var dartMixin = dartMixins[name];
var pbMixin = new PbMixin(dartMixin.name,
importFrom: dartMixin.importFrom,
parent: resolveMixin(dartMixin.parent));
pbMixins[name] = pbMixin;
return pbMixin;
}
return findMixin(name);
}
for (var mixin in dartMixins.values) {
resolveMixin(mixin.name);
}
return pbMixins;
}
final FileDescriptorProto descriptor;
final GenerationOptions options;
// The relative path used to import the .proto file, as a URI.
final Uri protoFileUri;
final enumGenerators = <EnumGenerator>[];
final messageGenerators = <MessageGenerator>[];
final extensionGenerators = <ExtensionGenerator>[];
final clientApiGenerators = <ClientApiGenerator>[];
final serviceGenerators = <ServiceGenerator>[];
final grpcGenerators = <GrpcServiceGenerator>[];
/// True if cross-references have been resolved.
bool _linked = false;
FileGenerator(this.descriptor, this.options)
: protoFileUri = new Uri.file(descriptor.name) {
if (protoFileUri.isAbsolute) {
// protoc should never generate an import with an absolute path.
throw "FAILURE: Import with absolute path is not supported";
}
var declaredMixins = _getDeclaredMixins(descriptor);
var defaultMixinName =
descriptor.options?.getExtension(Dart_options.defaultMixin) ?? '';
var defaultMixin =
declaredMixins[defaultMixinName] ?? findMixin(defaultMixinName);
if (defaultMixin == null && defaultMixinName.isNotEmpty) {
throw ('Option default_mixin on file ${descriptor.name}: Unknown mixin '
'$defaultMixinName');
}
// Load and register all enum and message types.
for (EnumDescriptorProto enumType in descriptor.enumType) {
enumGenerators.add(new EnumGenerator(enumType, this));
}
for (DescriptorProto messageType in descriptor.messageType) {
messageGenerators.add(new MessageGenerator(
messageType, this, declaredMixins, defaultMixin));
}
for (FieldDescriptorProto extension in descriptor.extension) {
extensionGenerators.add(new ExtensionGenerator(extension, this));
}
for (ServiceDescriptorProto service in descriptor.service) {
if (options.useGrpc) {
grpcGenerators.add(new GrpcServiceGenerator(service, this));
} else {
var serviceGen = new ServiceGenerator(service, this);
serviceGenerators.add(serviceGen);
clientApiGenerators.add(new ClientApiGenerator(serviceGen));
}
}
}
/// Creates the fields in each message.
/// Resolves field types and extension targets using the supplied context.
void resolve(GenerationContext ctx) {
if (_linked) throw new StateError("cross references already resolved");
for (var m in messageGenerators) {
m.resolve(ctx);
}
for (var x in extensionGenerators) {
x.resolve(ctx);
}
_linked = true;
}
String get package => descriptor.package;
String get classname => '';
String get fqname => '.${descriptor.package}';
FileGenerator get fileGen => this;
// Extract the filename from a URI and remove the extension.
String _fileNameWithoutExtension(Uri filePath) {
String fileName = filePath.pathSegments.last;
int index = fileName.lastIndexOf(".");
return index == -1 ? fileName : fileName.substring(0, index);
}
/// Generates all the Dart files for this .proto file.
List<CodeGeneratorResponse_File> generateFiles(OutputConfiguration config) {
if (!_linked) throw new StateError("not linked");
makeFile(String extension, String content) {
Uri protoUrl = new Uri.file(descriptor.name);
Uri dartUrl = config.outputPathFor(protoUrl, extension);
return new CodeGeneratorResponse_File()
..name = dartUrl.path
..content = content;
}
final files = [
makeFile(".pb.dart", generateMainFile(config)),
makeFile(".pbenum.dart", generateEnumFile(config)),
makeFile(".pbjson.dart", generateJsonFile(config)),
];
if (options.useGrpc) {
if (grpcGenerators.isNotEmpty) {
files.add(makeFile(".pbgrpc.dart", generateGrpcFile(config)));
}
} else {
files.add(makeFile(".pbserver.dart", generateServerFile(config)));
}
return files;
}
/// Returns the contents of the .pb.dart file for this .proto file.
String generateMainFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
IndentingWriter out = new IndentingWriter();
writeMainHeader(out, config);
// Generate code.
for (MessageGenerator m in messageGenerators) {
m.generate(out);
}
// Generate code for extensions defined at top-level using a class
// name derived from the file name.
if (!extensionGenerators.isEmpty) {
// TODO(antonm): do not generate a class.
String className = extensionClassName(descriptor);
out.addBlock('class $className {', '}\n', () {
for (ExtensionGenerator x in extensionGenerators) {
x.generate(out);
}
out.println('static void registerAllExtensions(ExtensionRegistry '
'registry) {');
for (ExtensionGenerator x in extensionGenerators) {
out.println(' registry.add(${x.name});');
}
out.println('}');
});
}
for (ClientApiGenerator c in clientApiGenerators) {
c.generate(out);
}
return out.toString();
}
/// Writes the header and imports for the .pb.dart file.
void writeMainHeader(IndentingWriter out,
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
_writeLibraryHeading(out);
// Make sure any other symbols in dart:core don't cause name conflicts with
// protobuf classes that have the same name.
out.println("// ignore: UNUSED_SHOWN_NAME\n"
"import 'dart:core' show int, bool, double, String, List, override;");
// We only add the dart:async import if there are services in the
// FileDescriptorProto.
if (descriptor.service.isNotEmpty) {
out.println("import 'dart:async';\n");
}
if (_needsFixnumImport) {
out.println("import 'package:fixnum/fixnum.dart';");
}
if (_needsProtobufImport) {
out.println("import 'package:protobuf/protobuf.dart';");
out.println();
}
var mixinImports = findMixinsToImport();
var importNames = mixinImports.keys.toList();
importNames.sort();
for (var imp in importNames) {
var symbols = mixinImports[imp];
out.println("import '${imp}' show ${symbols.join(', ')};");
}
if (mixinImports.isNotEmpty) out.println();
// Import the .pb.dart files we depend on.
var imports = new Set<FileGenerator>.identity();
var enumImports = new Set<FileGenerator>.identity();
_findProtosToImport(imports, enumImports);
for (var target in imports) {
_writeImport(out, config, target, ".pb.dart");
}
if (imports.isNotEmpty) out.println();
for (var target in enumImports) {
_writeImport(out, config, target, ".pbenum.dart");
}
if (enumImports.isNotEmpty) out.println();
// Export enums in main file for backward compatibility.
if (enumCount > 0) {
Uri resolvedImport =
config.resolveImport(protoFileUri, protoFileUri, ".pbenum.dart");
out.println("export '$resolvedImport';");
out.println();
}
}
bool get _needsFixnumImport {
for (var m in messageGenerators) {
if (m.needsFixnumImport) return true;
}
for (var x in extensionGenerators) {
if (x.needsFixnumImport) return true;
}
return false;
}
bool get _needsProtobufImport =>
messageGenerators.isNotEmpty ||
extensionGenerators.isNotEmpty ||
clientApiGenerators.isNotEmpty;
/// Returns the generator for each .pb.dart file we need to import.
void _findProtosToImport(
Set<FileGenerator> imports, Set<FileGenerator> enumImports) {
for (var m in messageGenerators) {
m.addImportsTo(imports, enumImports);
}
for (var x in extensionGenerators) {
x.addImportsTo(imports, enumImports);
}
// Add imports needed for client-side services.
for (var x in serviceGenerators) {
x.addImportsTo(imports);
}
// Don't need to import self. (But we may need to import the enums.)
imports.remove(this);
}
/// Returns a map from import names to the Dart symbols to be imported.
Map<String, List<String>> findMixinsToImport() {
var mixins = new Set<PbMixin>();
for (MessageGenerator m in messageGenerators) {
m.addMixinsTo(mixins);
}
var imports = <String, List<String>>{};
for (var m in mixins) {
var imp = m.importFrom;
List<String> symbols = imports[imp];
if (symbols == null) {
symbols = [];
imports[imp] = symbols;
}
symbols.add(m.name);
}
for (var imp in imports.keys) {
imports[imp].sort();
}
return imports;
}
/// Returns the contents of the .pbenum.dart file for this .proto file.
String generateEnumFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
var out = new IndentingWriter();
_writeLibraryHeading(out, "pbenum");
if (enumCount > 0) {
// Make sure any other symbols in dart:core don't cause name conflicts
// with enums that have the same name.
out.println("// ignore: UNUSED_SHOWN_NAME\n"
"import 'dart:core' show int, dynamic, String, List, Map;");
out.println("import 'package:protobuf/protobuf.dart';");
out.println();
}
for (EnumGenerator e in enumGenerators) {
e.generate(out);
}
for (MessageGenerator m in messageGenerators) {
m.generateEnums(out);
}
return out.toString();
}
/// Returns the number of enum types generated in the .pbenum.dart file.
int get enumCount {
var count = enumGenerators.length;
for (MessageGenerator m in messageGenerators) {
count += m.enumCount;
}
return count;
}
/// Returns the contents of the .pbserver.dart file for this .proto file.
String generateServerFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
var out = new IndentingWriter();
_writeLibraryHeading(out, "pbserver");
if (serviceGenerators.isNotEmpty) {
out.println('''
import 'dart:async';
import 'package:protobuf/protobuf.dart';
''');
}
// Import .pb.dart files needed for requests and responses.
var imports = new Set<FileGenerator>();
for (var x in serviceGenerators) {
x.addImportsTo(imports);
}
for (var target in imports) {
_writeImport(out, config, target, ".pb.dart");
}
// Import .pbjson.dart file needed for $json and $messageJson.
if (serviceGenerators.isNotEmpty) {
_writeImport(out, config, this, ".pbjson.dart");
out.println();
}
Uri resolvedImport =
config.resolveImport(protoFileUri, protoFileUri, ".pb.dart");
out.println("export '$resolvedImport';");
out.println();
for (ServiceGenerator s in serviceGenerators) {
s.generate(out);
}
return out.toString();
}
/// Returns the contents of the .pbgrpc.dart file for this .proto file.
String generateGrpcFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
var out = new IndentingWriter();
_writeLibraryHeading(out, "pbgrpc");
out.println('''
import 'dart:async';
import 'package:grpc/grpc.dart';
''');
// Import .pb.dart files needed for requests and responses.
var imports = new Set<FileGenerator>();
for (var generator in grpcGenerators) {
generator.addImportsTo(imports);
}
for (var target in imports) {
_writeImport(out, config, target, ".pb.dart");
}
var resolvedImport =
config.resolveImport(protoFileUri, protoFileUri, ".pb.dart");
out.println("export '$resolvedImport';");
out.println();
for (var generator in grpcGenerators) {
generator.generate(out);
}
return _formatter.format(out.toString());
}
/// Returns the contents of the .pbjson.dart file for this .proto file.
String generateJsonFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
var out = new IndentingWriter();
_writeLibraryHeading(out, "pbjson");
// Import the .pbjson.dart files we depend on.
var imports = _findJsonProtosToImport();
for (var target in imports) {
_writeImport(out, config, target, ".pbjson.dart");
}
if (imports.isNotEmpty) out.println();
for (var e in enumGenerators) {
e.generateConstants(out);
}
for (MessageGenerator m in messageGenerators) {
m.generateConstants(out);
}
for (ServiceGenerator s in serviceGenerators) {
s.generateConstants(out);
}
return out.toString();
}
/// Returns the generator for each .pbjson.dart file the generated
/// .pbjson.dart needs to import.
Set<FileGenerator> _findJsonProtosToImport() {
var imports = new Set<FileGenerator>.identity();
for (var m in messageGenerators) {
m.addConstantImportsTo(imports);
}
for (var x in extensionGenerators) {
x.addConstantImportsTo(imports);
}
for (var x in serviceGenerators) {
x.addConstantImportsTo(imports);
}
imports.remove(this); // Don't need to import self.
return imports;
}
/// Writes the library name at the top of the dart file.
///
/// (This should be unique to avoid warnings about duplicate Dart libraries.)
void _writeLibraryHeading(IndentingWriter out, [String extension]) {
Uri filePath = new Uri.file(descriptor.name);
if (filePath.isAbsolute) {
// protoc should never generate a file descriptor with an absolute path.
throw "FAILURE: File with an absolute path is not supported";
}
var libraryName = _fileNameWithoutExtension(filePath).replaceAll('-', '_');
if (extension != null) libraryName += "_$extension";
if (descriptor.package != '') {
// Two .protos can be in the same proto package.
// It isn't unique enough to use as a Dart library name.
// But we can prepend it.
libraryName = descriptor.package + "_" + libraryName;
}
out.println('''
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names
// ignore_for_file: library_prefixes
library $libraryName;
''');
}
/// Writes an import of a .dart file corresponding to a .proto file.
/// (Possibly the same .proto file.)
void _writeImport(IndentingWriter out, OutputConfiguration config,
FileGenerator target, String extension) {
Uri resolvedImport =
config.resolveImport(target.protoFileUri, protoFileUri, extension);
out.print("import '$resolvedImport'");
if (package != target.package && target.package.isNotEmpty) {
out.print(' as ${target.packageImportPrefix}');
}
out.println(';');
}
}