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

Side by Side Diff: lib/file_generator.dart

Issue 1192943003: changes for 0.3.9 (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.
9 static const int implementMapByDefaultOption = 95333044;
10 8
11 // Returns true if option implement_map_by_default is on for this file. 9 /// Returns the name of the mixin to use by default in this file,
12 static bool _shouldImplementMapByDefault(FileDescriptorProto desc) { 10 /// or null for no mixin by default.
13 if (!desc.hasOptions()) return false; 11 static String _getDefaultMixin(FileDescriptorProto desc) {
14 12 if (!desc.hasOptions()) return null;
15 var val = desc.options.unknownFields.getField(implementMapByDefaultOption); 13 if (!desc.options.hasExtension(Dart_options.defaultMixin)) {
16 if (val == null || val.length != 1) return false; 14 return null;
17 15 }
18 return val.values[0] == 1; 16 var name = desc.options.getExtension(Dart_options.defaultMixin);
17 if (!reservedNamesForMixins.containsKey(name)) {
18 throw("unknown mixin class: ${name}");
19 }
20 return name;
19 } 21 }
20 22
21 final FileDescriptorProto _fileDescriptor; 23 final FileDescriptorProto _fileDescriptor;
22 final ProtobufContainer _parent; 24 final ProtobufContainer _parent;
23 final GenerationContext _context; 25 final GenerationContext _context;
24 26
25 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; 27 final List<EnumGenerator> enumGenerators = <EnumGenerator>[];
26 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; 28 final List<MessageGenerator> messageGenerators = <MessageGenerator>[];
27 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; 29 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[];
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 String defaultMixin = _getDefaultMixin(_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, defaultMixin));
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 }
46 } 48 }
47 49
48 String get package => _fileDescriptor.package; 50 String get package => _fileDescriptor.package;
49 String get classname => ''; 51 String get classname => '';
50 String get fqname => '.${_fileDescriptor.package}'; 52 String get fqname => '.${_fileDescriptor.package}';
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 out.println( 92 out.println(
91 '///\n' 93 '///\n'
92 '// Generated code. Do not modify.\n' 94 '// Generated code. Do not modify.\n'
93 '///\n' 95 '///\n'
94 'library $libraryName;\n' 96 'library $libraryName;\n'
95 '\n' 97 '\n'
96 "import 'package:fixnum/fixnum.dart';\n" 98 "import 'package:fixnum/fixnum.dart';\n"
97 "import 'package:protobuf/protobuf.dart';" 99 "import 'package:protobuf/protobuf.dart';"
98 ); 100 );
99 101
100 if (needsMapMixinImport) {
101 out.println("import 'dart:collection' show MapMixin;");
102 }
103
104 for (String import in _fileDescriptor.dependency) { 102 for (String import in _fileDescriptor.dependency) {
105 Uri importPath = new Uri.file(import); 103 Uri importPath = new Uri.file(import);
106 if (importPath.isAbsolute) { 104 if (importPath.isAbsolute) {
107 // protoc should never generate an import with an absolute path. 105 // protoc should never generate an import with an absolute path.
108 throw("FAILURE: Import with absolute path is not supported"); 106 throw("FAILURE: Import with absolute path is not supported");
109 } 107 }
110 // Create a path from the current file to the imported proto. 108 // Create a path from the current file to the imported proto.
111 Uri resolvedImport = _context.outputConfiguration.resolveImport( 109 Uri resolvedImport = _context.outputConfiguration.resolveImport(
112 importPath, filePath); 110 importPath, filePath);
113 // Find the file generator for this import as it contains the 111 // Find the file generator for this import as it contains the
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 } 143 }
146 out.println('static void registerAllExtensions(ExtensionRegistry ' 144 out.println('static void registerAllExtensions(ExtensionRegistry '
147 'registry) {'); 145 'registry) {');
148 for (ExtensionGenerator x in extensionGenerators) { 146 for (ExtensionGenerator x in extensionGenerators) {
149 out.println(' registry.add(${x.name});'); 147 out.println(' registry.add(${x.name});');
150 } 148 }
151 out.println('}'); 149 out.println('}');
152 }); 150 });
153 } 151 }
154 } 152 }
155
156 bool get needsMapMixinImport {
157 for (var m in messageGenerators) {
158 if (m.needsMapMixinImport) {
159 return true;
160 }
161 }
162 return false;
163 }
164 } 153 }
165 154
166 class GenerationContext { 155 class GenerationContext {
167 final GenerationOptions options; 156 final GenerationOptions options;
168 final OutputConfiguration outputConfiguration; 157 final OutputConfiguration outputConfiguration;
169 final Map<String, ProtobufContainer> _registry = 158 final Map<String, ProtobufContainer> _registry =
170 <String, ProtobufContainer>{}; 159 <String, ProtobufContainer>{};
171 final Map<String, FileGenerator> _files = 160 final Map<String, FileGenerator> _files =
172 <String, FileGenerator>{}; 161 <String, FileGenerator>{};
173 162
174 GenerationContext(this.options, this.outputConfiguration); 163 GenerationContext(this.options, this.outputConfiguration);
175 164
176 void register(ProtobufContainer container) { 165 void register(ProtobufContainer container) {
177 _registry[container.fqname] = container; 166 _registry[container.fqname] = container;
178 if (container is FileGenerator) { 167 if (container is FileGenerator) {
179 _files[container._fileDescriptor.name] = container; 168 _files[container._fileDescriptor.name] = container;
180 } 169 }
181 } 170 }
182 171
183 ProtobufContainer operator [](String fqname) => _registry[fqname]; 172 ProtobufContainer operator [](String fqname) => _registry[fqname];
184 173
185 FileGenerator lookupFile(String name) => _files[name]; 174 FileGenerator lookupFile(String name) => _files[name];
186 } 175 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698