OLD | NEW |
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 24 matching lines...) Expand all Loading... |
80 | 86 |
81 void generate(IndentingWriter out) { | 87 void generate(IndentingWriter out) { |
82 Uri filePath = new Uri.file(_fileDescriptor.name); | 88 Uri filePath = new Uri.file(_fileDescriptor.name); |
83 if (filePath.isAbsolute) { | 89 if (filePath.isAbsolute) { |
84 // protoc should never generate a file descriptor with an absolute path. | 90 // protoc should never generate a file descriptor with an absolute path. |
85 throw("FAILURE: File with an absolute path is not supported"); | 91 throw("FAILURE: File with an absolute path is not supported"); |
86 } | 92 } |
87 | 93 |
88 String libraryName = _generateLibraryName(filePath); | 94 String libraryName = _generateLibraryName(filePath); |
89 | 95 |
| 96 // Print header and imports. We only add the dart:async import if there |
| 97 // are services in the FileDescriptorProto. |
90 out.println( | 98 out.println( |
91 '///\n' | 99 '///\n' |
92 '// Generated code. Do not modify.\n' | 100 '// Generated code. Do not modify.\n' |
93 '///\n' | 101 '///\n' |
94 'library $libraryName;\n' | 102 'library $libraryName;\n'); |
95 '\n' | 103 if (_fileDescriptor.service.isNotEmpty) { |
| 104 out.println("import 'dart:async'\n;"); |
| 105 } |
| 106 out.println( |
96 "import 'package:fixnum/fixnum.dart';\n" | 107 "import 'package:fixnum/fixnum.dart';\n" |
97 "import 'package:protobuf/protobuf.dart';" | 108 "import 'package:protobuf/protobuf.dart';" |
98 ); | 109 ); |
99 | 110 |
100 if (needsMapMixinImport) { | 111 if (needsMapMixinImport) { |
101 out.println("import 'dart:collection' show MapMixin;"); | 112 out.println("import 'dart:collection' show MapMixin;"); |
102 } | 113 } |
103 | 114 |
104 for (String import in _fileDescriptor.dependency) { | 115 for (String import in _fileDescriptor.dependency) { |
105 Uri importPath = new Uri.file(import); | 116 Uri importPath = new Uri.file(import); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 x.generate(out); | 155 x.generate(out); |
145 } | 156 } |
146 out.println('static void registerAllExtensions(ExtensionRegistry ' | 157 out.println('static void registerAllExtensions(ExtensionRegistry ' |
147 'registry) {'); | 158 'registry) {'); |
148 for (ExtensionGenerator x in extensionGenerators) { | 159 for (ExtensionGenerator x in extensionGenerators) { |
149 out.println(' registry.add(${x.name});'); | 160 out.println(' registry.add(${x.name});'); |
150 } | 161 } |
151 out.println('}'); | 162 out.println('}'); |
152 }); | 163 }); |
153 } | 164 } |
| 165 |
| 166 for (ClientApiGenerator c in clientApiGenerators) { |
| 167 c.generate(out); |
| 168 } |
| 169 for (ServiceGenerator s in serviceGenerators) { |
| 170 s.generate(out); |
| 171 } |
154 } | 172 } |
155 | 173 |
156 bool get needsMapMixinImport { | 174 bool get needsMapMixinImport { |
157 for (var m in messageGenerators) { | 175 for (var m in messageGenerators) { |
158 if (m.needsMapMixinImport) { | 176 if (m.needsMapMixinImport) { |
159 return true; | 177 return true; |
160 } | 178 } |
161 } | 179 } |
162 return false; | 180 return false; |
163 } | 181 } |
(...skipping 13 matching lines...) Expand all Loading... |
177 _registry[container.fqname] = container; | 195 _registry[container.fqname] = container; |
178 if (container is FileGenerator) { | 196 if (container is FileGenerator) { |
179 _files[container._fileDescriptor.name] = container; | 197 _files[container._fileDescriptor.name] = container; |
180 } | 198 } |
181 } | 199 } |
182 | 200 |
183 ProtobufContainer operator [](String fqname) => _registry[fqname]; | 201 ProtobufContainer operator [](String fqname) => _registry[fqname]; |
184 | 202 |
185 FileGenerator lookupFile(String name) => _files[name]; | 203 FileGenerator lookupFile(String name) => _files[name]; |
186 } | 204 } |
OLD | NEW |