Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: lib/file_generator.dart

Issue 1829573002: Fix all strong mode warnings in protoc-plugin (Closed) Base URL: git@github.com:dart-lang/dart-protoc-plugin.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of protoc; 5 part of protoc;
6 6
7 class FileGenerator extends ProtobufContainer { 7 class FileGenerator extends ProtobufContainer {
8
9 /// Returns the the mixin to use by default in this file, 8 /// Returns the the mixin to use by default in this file,
10 /// or null for no mixin by default. 9 /// or null for no mixin by default.
11 static PbMixin _getDefaultMixin(FileDescriptorProto desc) { 10 static PbMixin _getDefaultMixin(FileDescriptorProto desc) {
12 if (!desc.hasOptions()) return null; 11 if (!desc.hasOptions()) return null;
13 if (!desc.options.hasExtension(Dart_options.defaultMixin)) { 12 if (!desc.options.hasExtension(Dart_options.defaultMixin)) {
14 return null; 13 return null;
15 } 14 }
16 var name = desc.options.getExtension(Dart_options.defaultMixin); 15 var name = desc.options.getExtension(Dart_options.defaultMixin);
17 PbMixin mixin = findMixin(name); 16 PbMixin mixin = findMixin(name);
18 if (mixin == null) { 17 if (mixin == null) {
19 throw("unknown mixin class: ${name}"); 18 throw ("unknown mixin class: ${name}");
20 } 19 }
21 return mixin; 20 return mixin;
22 } 21 }
23 22
24 final FileDescriptorProto _fileDescriptor; 23 final FileDescriptorProto _fileDescriptor;
25 24
26 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; 25 final List<EnumGenerator> enumGenerators = <EnumGenerator>[];
27 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; 26 final List<MessageGenerator> messageGenerators = <MessageGenerator>[];
28 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; 27 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[];
29 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[]; 28 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[];
30 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[]; 29 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[];
31 30
32 /// True if cross-references have been resolved. 31 /// True if cross-references have been resolved.
33 bool _linked = false; 32 bool _linked = false;
34 33
35 FileGenerator(this._fileDescriptor) { 34 FileGenerator(this._fileDescriptor) {
36 var defaultMixin = _getDefaultMixin(_fileDescriptor); 35 var defaultMixin = _getDefaultMixin(_fileDescriptor);
37 36
38 // Load and register all enum and message types. 37 // Load and register all enum and message types.
39 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) { 38 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) {
40 enumGenerators.add(new EnumGenerator(enumType, this)); 39 enumGenerators.add(new EnumGenerator(enumType, this));
41 } 40 }
42 for (DescriptorProto messageType in _fileDescriptor.messageType) { 41 for (DescriptorProto messageType in _fileDescriptor.messageType) {
43 messageGenerators.add( 42 messageGenerators
44 new MessageGenerator(messageType, this, defaultMixin)); 43 .add(new MessageGenerator(messageType, this, defaultMixin));
45 } 44 }
46 for (FieldDescriptorProto extension in _fileDescriptor.extension) { 45 for (FieldDescriptorProto extension in _fileDescriptor.extension) {
47 extensionGenerators.add(new ExtensionGenerator(extension, this)); 46 extensionGenerators.add(new ExtensionGenerator(extension, this));
48 } 47 }
49 for (ServiceDescriptorProto service in _fileDescriptor.service) { 48 for (ServiceDescriptorProto service in _fileDescriptor.service) {
50 var serviceGen = new ServiceGenerator(service, this); 49 var serviceGen = new ServiceGenerator(service, this);
51 serviceGenerators.add(serviceGen); 50 serviceGenerators.add(serviceGen);
52 clientApiGenerators.add(new ClientApiGenerator(serviceGen)); 51 clientApiGenerators.add(new ClientApiGenerator(serviceGen));
53 } 52 }
54 } 53 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 return libraryName; 101 return libraryName;
103 } 102 }
104 103
105 CodeGeneratorResponse_File generateResponse(OutputConfiguration config) { 104 CodeGeneratorResponse_File generateResponse(OutputConfiguration config) {
106 IndentingWriter out = new IndentingWriter(); 105 IndentingWriter out = new IndentingWriter();
107 106
108 generate(out, config); 107 generate(out, config);
109 108
110 Uri filePath = new Uri.file(_fileDescriptor.name); 109 Uri filePath = new Uri.file(_fileDescriptor.name);
111 return new CodeGeneratorResponse_File() 110 return new CodeGeneratorResponse_File()
112 ..name = config.outputPathFor(filePath).path 111 ..name = config.outputPathFor(filePath).path
113 ..content = out.toString(); 112 ..content = out.toString();
114 } 113 }
115 114
116 /// Generates the Dart code for this .proto file. 115 /// Generates the Dart code for this .proto file.
117 void generate(IndentingWriter out, 116 void generate(IndentingWriter out,
118 [OutputConfiguration config = const DefaultOutputConfiguration()]) { 117 [OutputConfiguration config = const DefaultOutputConfiguration()]) {
119 if (!_linked) throw new StateError("not linked"); 118 if (!_linked) throw new StateError("not linked");
120 119
121 Uri filePath = new Uri.file(_fileDescriptor.name); 120 Uri filePath = new Uri.file(_fileDescriptor.name);
122 if (filePath.isAbsolute) { 121 if (filePath.isAbsolute) {
123 // protoc should never generate a file descriptor with an absolute path. 122 // protoc should never generate a file descriptor with an absolute path.
124 throw("FAILURE: File with an absolute path is not supported"); 123 throw ("FAILURE: File with an absolute path is not supported");
Søren Gjesse 2016/03/29 09:04:13 Remove the () here.
skybrian 2016/04/01 02:00:15 Done.
125 } 124 }
126 125
127 generateHeader(out, filePath, config); 126 generateHeader(out, filePath, config);
128 127
129 // Generate code. 128 // Generate code.
130 for (EnumGenerator e in enumGenerators) { 129 for (EnumGenerator e in enumGenerators) {
131 e.generate(out); 130 e.generate(out);
132 } 131 }
133 for (MessageGenerator m in messageGenerators) { 132 for (MessageGenerator m in messageGenerators) {
134 m.generate(out); 133 m.generate(out);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 m.generateConstants(out); 167 m.generateConstants(out);
169 } 168 }
170 for (ServiceGenerator s in serviceGenerators) { 169 for (ServiceGenerator s in serviceGenerators) {
171 s.generateConstants(out); 170 s.generateConstants(out);
172 } 171 }
173 } 172 }
174 173
175 /// Prints header and imports. 174 /// Prints header and imports.
176 void generateHeader(IndentingWriter out, Uri filePath, 175 void generateHeader(IndentingWriter out, Uri filePath,
177 [OutputConfiguration config = const DefaultOutputConfiguration()]) { 176 [OutputConfiguration config = const DefaultOutputConfiguration()]) {
178
179 String libraryName = _generateLibraryName(filePath); 177 String libraryName = _generateLibraryName(filePath);
180 out.println( 178 out.println('///\n'
181 '///\n'
182 '// Generated code. Do not modify.\n' 179 '// Generated code. Do not modify.\n'
183 '///\n' 180 '///\n'
184 'library $libraryName;\n'); 181 'library $libraryName;\n');
185 182
186 // We only add the dart:async import if there are services in the 183 // We only add the dart:async import if there are services in the
187 // FileDescriptorProto. 184 // FileDescriptorProto.
188 if (_fileDescriptor.service.isNotEmpty) { 185 if (_fileDescriptor.service.isNotEmpty) {
189 out.println("import 'dart:async';\n"); 186 out.println("import 'dart:async';\n");
190 } 187 }
191 188
192 if (_needsFixnumImport) { 189 if (_needsFixnumImport) {
193 out.println("import 'package:fixnum/fixnum.dart';"); 190 out.println("import 'package:fixnum/fixnum.dart';");
194 } 191 }
195 out.println("import 'package:protobuf/protobuf.dart';"); 192 out.println("import 'package:protobuf/protobuf.dart';");
196 193
197 var mixinImports = findMixinsToImport(); 194 var mixinImports = findMixinsToImport();
198 var importNames = mixinImports.keys.toList(); 195 var importNames = mixinImports.keys.toList();
199 importNames.sort(); 196 importNames.sort();
200 for (var imp in importNames) { 197 for (var imp in importNames) {
201 var symbols = mixinImports[imp]; 198 var symbols = mixinImports[imp];
202 out.println("import '${imp}' show ${symbols.join(', ')};"); 199 out.println("import '${imp}' show ${symbols.join(', ')};");
203 } 200 }
204 201
205 for (var imported in _findProtosToImport()) { 202 for (var imported in _findProtosToImport()) {
206 String filename = imported._fileDescriptor.name; 203 String filename = imported._fileDescriptor.name;
207 Uri importPath = new Uri.file(filename); 204 Uri importPath = new Uri.file(filename);
208 if (importPath.isAbsolute) { 205 if (importPath.isAbsolute) {
209 // protoc should never generate an import with an absolute path. 206 // protoc should never generate an import with an absolute path.
210 throw("FAILURE: Import with absolute path is not supported"); 207 throw ("FAILURE: Import with absolute path is not supported");
Søren Gjesse 2016/03/29 09:04:13 Remove the () here.
skybrian 2016/04/01 02:00:15 Done.
211 } 208 }
212 // Create a path from the current file to the imported proto. 209 // Create a path from the current file to the imported proto.
213 Uri resolvedImport = config.resolveImport(importPath, filePath); 210 Uri resolvedImport = config.resolveImport(importPath, filePath);
214 out.print("import '$resolvedImport'"); 211 out.print("import '$resolvedImport'");
215 if (package != imported.package && imported.package.isNotEmpty) { 212 if (package != imported.package && imported.package.isNotEmpty) {
216 out.print(' as ${imported.packageImportPrefix}'); 213 out.print(' as ${imported.packageImportPrefix}');
217 } 214 }
218 out.println(';'); 215 out.println(';');
219 } 216 }
220 out.println(); 217 out.println();
(...skipping 25 matching lines...) Expand all
246 return imports; 243 return imports;
247 } 244 }
248 245
249 /// Returns a map from import names to the Dart symbols to be imported. 246 /// Returns a map from import names to the Dart symbols to be imported.
250 Map<String, List<String>> findMixinsToImport() { 247 Map<String, List<String>> findMixinsToImport() {
251 var mixins = new Set<PbMixin>(); 248 var mixins = new Set<PbMixin>();
252 for (MessageGenerator m in messageGenerators) { 249 for (MessageGenerator m in messageGenerators) {
253 m.addMixinsTo(mixins); 250 m.addMixinsTo(mixins);
254 } 251 }
255 252
256 var imports = {}; 253 var imports = <String, List<String>>{};
257 for (var m in mixins) { 254 for (var m in mixins) {
258 var imp = m.importFrom; 255 var imp = m.importFrom;
259 List<String> symbols = imports[imp]; 256 List<String> symbols = imports[imp];
260 if (symbols == null) { 257 if (symbols == null) {
261 symbols = []; 258 symbols = [];
262 imports[imp] = symbols; 259 imports[imp] = symbols;
263 } 260 }
264 symbols.add(m.name); 261 symbols.add(m.name);
265 } 262 }
266 263
267 for (var imp in imports.keys) { 264 for (var imp in imports.keys) {
268 imports[imp].sort(); 265 imports[imp].sort();
269 } 266 }
270 267
271 return imports; 268 return imports;
272 } 269 }
273 } 270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698