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

Side by Side Diff: lib/file_generator.dart

Issue 2086253002: Allow application of external mixins to generated dart protos. (Closed) Base URL: https://github.com/dart-lang/dart-protoc-plugin.git@master
Patch Set: Add support for mixins in protos. Created 4 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 final _dartIdentifier = new RegExp(r'^\w+$');
8
7 /// Generates the Dart output files for one .proto input file. 9 /// Generates the Dart output files for one .proto input file.
8 /// 10 ///
9 /// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart. 11 /// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart.
10 class FileGenerator extends ProtobufContainer { 12 class FileGenerator extends ProtobufContainer {
11 /// Returns the the mixin to use by default in this file, 13 /// Reads the declared mixins in the file, keyed by name.
12 /// or null for no mixin by default. 14 static Map<String, DartMixin> _getDeclaredMixins(FileDescriptorProto desc) {
13 static PbMixin _getDefaultMixin(FileDescriptorProto desc) { 15 final mixins = <String, DartMixin>{};
14 if (!desc.hasOptions()) return null; 16 if (!desc.hasOptions() || !desc.options.hasExtension(Dart_options.mixins)) {
15 if (!desc.options.hasExtension(Dart_options.defaultMixin)) { 17 return mixins;
16 return null;
17 } 18 }
18 var name = desc.options.getExtension(Dart_options.defaultMixin); 19 for (DartMixin mixin in desc.options.getExtension(Dart_options.mixins)) {
19 PbMixin mixin = findMixin(name); 20 if (mixins.containsKey(mixin.name)) {
20 if (mixin == null) { 21 throw ('Duplicate mixin name: "${mixin.name}"');
skybrian 2016/06/22 19:27:18 style nit: parentheses aren't needed.
skybrian 2016/06/22 20:29:52 When we throw we should give some indication of th
frederikmutzel 2016/06/23 12:30:16 Done.
frederikmutzel 2016/06/23 12:30:16 Done.
21 throw ("unknown mixin class: ${name}"); 22 }
23 if (!mixin.name.startsWith(_dartIdentifier)) {
24 throw ('Mixin name "${mixin.name}" is not a valid dart class '
25 'identifier');
26 }
27 if (mixin.hasParent() && !mixin.parent.startsWith(_dartIdentifier)) {
28 throw ('Mixin parent "${mixin.parent}" of ${mixin.name} is not a valid '
29 'dart class identifier');
30 }
31 mixins[mixin.name] = mixin;
22 } 32 }
skybrian 2016/06/22 19:27:18 Also check for cycles?
frederikmutzel 2016/06/23 12:30:16 Done.
23 return mixin; 33 return mixins;
24 } 34 }
25 35
26 final FileDescriptorProto _fileDescriptor; 36 final FileDescriptorProto _fileDescriptor;
27 37
28 // The relative path used to import the .proto file, as a URI. 38 // The relative path used to import the .proto file, as a URI.
29 final Uri protoFileUri; 39 final Uri protoFileUri;
30 40
31 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; 41 final List<EnumGenerator> enumGenerators = <EnumGenerator>[];
32 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; 42 final List<MessageGenerator> messageGenerators = <MessageGenerator>[];
33 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; 43 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[];
34 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[]; 44 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[];
35 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[]; 45 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[];
36 46
37 /// True if cross-references have been resolved. 47 /// True if cross-references have been resolved.
38 bool _linked = false; 48 bool _linked = false;
39 49
40 FileGenerator(FileDescriptorProto descriptor) 50 FileGenerator(FileDescriptorProto descriptor)
41 : _fileDescriptor = descriptor, 51 : _fileDescriptor = descriptor,
42 protoFileUri = new Uri.file(descriptor.name) { 52 protoFileUri = new Uri.file(descriptor.name) {
43 if (protoFileUri.isAbsolute) { 53 if (protoFileUri.isAbsolute) {
44 // protoc should never generate an import with an absolute path. 54 // protoc should never generate an import with an absolute path.
45 throw "FAILURE: Import with absolute path is not supported"; 55 throw "FAILURE: Import with absolute path is not supported";
46 } 56 }
47 57
48 var defaultMixin = _getDefaultMixin(_fileDescriptor); 58 var declaredMixins = _getDeclaredMixins(descriptor);
59 var defaultMixin =
skybrian 2016/06/22 20:29:52 should check that we can find defaultMixin. It wil
frederikmutzel 2016/06/23 12:30:16 Done.
60 descriptor.options?.getExtension(Dart_options.defaultMixin);
49 61
50 // Load and register all enum and message types. 62 // Load and register all enum and message types.
51 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) { 63 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) {
52 enumGenerators.add(new EnumGenerator(enumType, this)); 64 enumGenerators.add(new EnumGenerator(enumType, this));
53 } 65 }
54 for (DescriptorProto messageType in _fileDescriptor.messageType) { 66 for (DescriptorProto messageType in _fileDescriptor.messageType) {
55 messageGenerators 67 messageGenerators.add(new MessageGenerator(
56 .add(new MessageGenerator(messageType, this, defaultMixin)); 68 messageType, this, declaredMixins, defaultMixin));
57 } 69 }
58 for (FieldDescriptorProto extension in _fileDescriptor.extension) { 70 for (FieldDescriptorProto extension in _fileDescriptor.extension) {
59 extensionGenerators.add(new ExtensionGenerator(extension, this)); 71 extensionGenerators.add(new ExtensionGenerator(extension, this));
60 } 72 }
61 for (ServiceDescriptorProto service in _fileDescriptor.service) { 73 for (ServiceDescriptorProto service in _fileDescriptor.service) {
62 var serviceGen = new ServiceGenerator(service, this); 74 var serviceGen = new ServiceGenerator(service, this);
63 serviceGenerators.add(serviceGen); 75 serviceGenerators.add(serviceGen);
64 clientApiGenerators.add(new ClientApiGenerator(serviceGen)); 76 clientApiGenerators.add(new ClientApiGenerator(serviceGen));
65 } 77 }
66 } 78 }
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 FileGenerator target, String extension) { 427 FileGenerator target, String extension) {
416 Uri resolvedImport = 428 Uri resolvedImport =
417 config.resolveImport(target.protoFileUri, protoFileUri, extension); 429 config.resolveImport(target.protoFileUri, protoFileUri, extension);
418 out.print("import '$resolvedImport'"); 430 out.print("import '$resolvedImport'");
419 if (package != target.package && target.package.isNotEmpty) { 431 if (package != target.package && target.package.isNotEmpty) {
420 out.print(' as ${target.packageImportPrefix}'); 432 out.print(' as ${target.packageImportPrefix}');
421 } 433 }
422 out.println(';'); 434 out.println(';');
423 } 435 }
424 } 436 }
OLDNEW
« no previous file with comments | « Makefile ('k') | lib/message_generator.dart » ('j') | lib/message_generator.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698