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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: lib/file_generator.dart
diff --git a/lib/file_generator.dart b/lib/file_generator.dart
index 65cba47953dcc0339f235c9dafccf6333952c871..fd8f0ab93c079fec3f8951628087f41fe603f8c1 100644
--- a/lib/file_generator.dart
+++ b/lib/file_generator.dart
@@ -4,23 +4,33 @@
part of protoc;
+final _dartIdentifier = new RegExp(r'^\w+$');
+
/// Generates the Dart output files for one .proto input file.
///
/// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart.
class FileGenerator extends ProtobufContainer {
- /// Returns the the mixin to use by default in this file,
- /// or null for no mixin by default.
- static PbMixin _getDefaultMixin(FileDescriptorProto desc) {
- if (!desc.hasOptions()) return null;
- if (!desc.options.hasExtension(Dart_options.defaultMixin)) {
- return null;
- }
- var name = desc.options.getExtension(Dart_options.defaultMixin);
- PbMixin mixin = findMixin(name);
- if (mixin == null) {
- throw ("unknown mixin class: ${name}");
- }
- return mixin;
+ /// Reads the declared mixins in the file, keyed by name.
+ static Map<String, DartMixin> _getDeclaredMixins(FileDescriptorProto desc) {
+ final mixins = <String, DartMixin>{};
+ if (!desc.hasOptions() || !desc.options.hasExtension(Dart_options.mixins)) {
+ return mixins;
+ }
+ for (DartMixin mixin in desc.options.getExtension(Dart_options.mixins)) {
+ if (mixins.containsKey(mixin.name)) {
+ 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.
+ }
+ if (!mixin.name.startsWith(_dartIdentifier)) {
+ throw ('Mixin name "${mixin.name}" is not a valid dart class '
+ 'identifier');
+ }
+ if (mixin.hasParent() && !mixin.parent.startsWith(_dartIdentifier)) {
+ throw ('Mixin parent "${mixin.parent}" of ${mixin.name} is not a valid '
+ 'dart class identifier');
+ }
+ mixins[mixin.name] = mixin;
+ }
skybrian 2016/06/22 19:27:18 Also check for cycles?
frederikmutzel 2016/06/23 12:30:16 Done.
+ return mixins;
}
final FileDescriptorProto _fileDescriptor;
@@ -45,15 +55,17 @@ class FileGenerator extends ProtobufContainer {
throw "FAILURE: Import with absolute path is not supported";
}
- var defaultMixin = _getDefaultMixin(_fileDescriptor);
+ var declaredMixins = _getDeclaredMixins(descriptor);
+ 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.
+ descriptor.options?.getExtension(Dart_options.defaultMixin);
// Load and register all enum and message types.
for (EnumDescriptorProto enumType in _fileDescriptor.enumType) {
enumGenerators.add(new EnumGenerator(enumType, this));
}
for (DescriptorProto messageType in _fileDescriptor.messageType) {
- messageGenerators
- .add(new MessageGenerator(messageType, this, defaultMixin));
+ messageGenerators.add(new MessageGenerator(
+ messageType, this, declaredMixins, defaultMixin));
}
for (FieldDescriptorProto extension in _fileDescriptor.extension) {
extensionGenerators.add(new ExtensionGenerator(extension, this));
« 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