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

Side by Side Diff: lib/file_generator.dart

Issue 1196293003: Initial support for generating client and server stubs for Dart. (Closed) Base URL: https://github.com/dart-lang/dart-protoc-plugin.git@master
Patch Set: Created 5 years, 6 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 // This should match the extension in dart_options.proto. 8 // This should match the extension in dart_options.proto.
9 static const int implementMapByDefaultOption = 95333044; 9 static const int implementMapByDefaultOption = 95333044;
10 10
11 // Returns true if option implement_map_by_default is on for this file. 11 // Returns true if option implement_map_by_default is on for this file.
12 static bool _shouldImplementMapByDefault(FileDescriptorProto desc) { 12 static bool _shouldImplementMapByDefault(FileDescriptorProto desc) {
13 if (!desc.hasOptions()) return false; 13 if (!desc.hasOptions()) return false;
14 14
15 var val = desc.options.unknownFields.getField(implementMapByDefaultOption); 15 var val = desc.options.unknownFields.getField(implementMapByDefaultOption);
16 if (val == null || val.length != 1) return false; 16 if (val == null || val.length != 1) return false;
17 17
18 return val.values[0] == 1; 18 return val.values[0] == 1;
19 } 19 }
20 20
21 final FileDescriptorProto _fileDescriptor; 21 final FileDescriptorProto _fileDescriptor;
22 final ProtobufContainer _parent; 22 final ProtobufContainer _parent;
23 final GenerationContext _context; 23 final GenerationContext _context;
24 24
25 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; 25 final List<EnumGenerator> enumGenerators = <EnumGenerator>[];
26 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; 26 final List<MessageGenerator> messageGenerators = <MessageGenerator>[];
27 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; 27 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[];
28 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[];
29 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[];
28 30
29 FileGenerator(this._fileDescriptor, this._parent, this._context) { 31 FileGenerator(this._fileDescriptor, this._parent, this._context) {
30 _context.register(this); 32 _context.register(this);
31 33
32 bool implementMap = _shouldImplementMapByDefault(_fileDescriptor); 34 bool implementMap = _shouldImplementMapByDefault(_fileDescriptor);
33 35
34 // Load and register all enum and message types. 36 // Load and register all enum and message types.
35 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) { 37 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) {
36 enumGenerators.add(new EnumGenerator(enumType, this, _context)); 38 enumGenerators.add(new EnumGenerator(enumType, this, _context));
37 } 39 }
38 for (DescriptorProto messageType in _fileDescriptor.messageType) { 40 for (DescriptorProto messageType in _fileDescriptor.messageType) {
39 messageGenerators.add( 41 messageGenerators.add(
40 new MessageGenerator(messageType, this, _context, implementMap)); 42 new MessageGenerator(messageType, this, _context, implementMap));
41 } 43 }
42 for (FieldDescriptorProto extension in _fileDescriptor.extension) { 44 for (FieldDescriptorProto extension in _fileDescriptor.extension) {
43 extensionGenerators.add( 45 extensionGenerators.add(
44 new ExtensionGenerator(extension, this, _context)); 46 new ExtensionGenerator(extension, this, _context));
45 } 47 }
48 for (ServiceDescriptorProto service in _fileDescriptor.service) {
49 serviceGenerators.add(new ServiceGenerator(service, this, _context));
50 clientApiGenerators.add(new ClientApiGenerator(service, this, _context));
51 }
46 } 52 }
47 53
48 String get package => _fileDescriptor.package; 54 String get package => _fileDescriptor.package;
49 String get classname => ''; 55 String get classname => '';
50 String get fqname => '.${_fileDescriptor.package}'; 56 String get fqname => '.${_fileDescriptor.package}';
51 57
52 // Extract the filename from a URI and remove the extension. 58 // Extract the filename from a URI and remove the extension.
53 String _fileNameWithoutExtension(Uri filePath) { 59 String _fileNameWithoutExtension(Uri filePath) {
54 String fileName = filePath.pathSegments.last; 60 String fileName = filePath.pathSegments.last;
55 int index = fileName.lastIndexOf("."); 61 int index = fileName.lastIndexOf(".");
(...skipping 30 matching lines...) Expand all
86 } 92 }
87 93
88 String libraryName = _generateLibraryName(filePath); 94 String libraryName = _generateLibraryName(filePath);
89 95
90 out.println( 96 out.println(
91 '///\n' 97 '///\n'
92 '// Generated code. Do not modify.\n' 98 '// Generated code. Do not modify.\n'
93 '///\n' 99 '///\n'
94 'library $libraryName;\n' 100 'library $libraryName;\n'
95 '\n' 101 '\n'
102 'import \'dart:async\';\n'
Søren Gjesse 2015/06/23 12:08:31 Is there any way to only import this if needed? W
wibling 2015/06/23 13:26:34 I will take a look and see what I can come up with
103 '\n'
96 "import 'package:fixnum/fixnum.dart';\n" 104 "import 'package:fixnum/fixnum.dart';\n"
97 "import 'package:protobuf/protobuf.dart';" 105 "import 'package:protobuf/protobuf.dart';"
98 ); 106 );
99 107
100 if (needsMapMixinImport) { 108 if (needsMapMixinImport) {
101 out.println("import 'dart:collection' show MapMixin;"); 109 out.println("import 'dart:collection' show MapMixin;");
102 } 110 }
103 111
104 for (String import in _fileDescriptor.dependency) { 112 for (String import in _fileDescriptor.dependency) {
105 Uri importPath = new Uri.file(import); 113 Uri importPath = new Uri.file(import);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 x.generate(out); 152 x.generate(out);
145 } 153 }
146 out.println('static void registerAllExtensions(ExtensionRegistry ' 154 out.println('static void registerAllExtensions(ExtensionRegistry '
147 'registry) {'); 155 'registry) {');
148 for (ExtensionGenerator x in extensionGenerators) { 156 for (ExtensionGenerator x in extensionGenerators) {
149 out.println(' registry.add(${x.name});'); 157 out.println(' registry.add(${x.name});');
150 } 158 }
151 out.println('}'); 159 out.println('}');
152 }); 160 });
153 } 161 }
162
163 for (ClientApiGenerator c in clientApiGenerators) {
164 c.generate(out);
165 }
166 for (ServiceGenerator s in serviceGenerators) {
167 s.generate(out);
168 }
154 } 169 }
155 170
156 bool get needsMapMixinImport { 171 bool get needsMapMixinImport {
157 for (var m in messageGenerators) { 172 for (var m in messageGenerators) {
158 if (m.needsMapMixinImport) { 173 if (m.needsMapMixinImport) {
159 return true; 174 return true;
160 } 175 }
161 } 176 }
162 return false; 177 return false;
163 } 178 }
(...skipping 13 matching lines...) Expand all
177 _registry[container.fqname] = container; 192 _registry[container.fqname] = container;
178 if (container is FileGenerator) { 193 if (container is FileGenerator) {
179 _files[container._fileDescriptor.name] = container; 194 _files[container._fileDescriptor.name] = container;
180 } 195 }
181 } 196 }
182 197
183 ProtobufContainer operator [](String fqname) => _registry[fqname]; 198 ProtobufContainer operator [](String fqname) => _registry[fqname];
184 199
185 FileGenerator lookupFile(String name) => _files[name]; 200 FileGenerator lookupFile(String name) => _files[name];
186 } 201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698