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 MessageGenerator extends ProtobufContainer { | 7 class MessageGenerator extends ProtobufContainer { |
8 // Returns the mixin for this message, or null if none. | 8 /// Returns the mixin for this message, or null if none. |
9 static PbMixin _getMixin(DescriptorProto desc, PbMixin defaultValue) { | 9 /// |
10 if (!desc.hasOptions()) return defaultValue; | 10 /// First searches [declaredMixins], then internal mixins declared by |
11 if (!desc.options.hasExtension(Dart_options.mixin)) return defaultValue; | 11 /// [findMixin]. |
12 static PbMixin _getMixin(DescriptorProto desc, | |
13 Map<String, DartMixin> declaredMixins, String defaultMixin) { | |
14 PbMixin resolveMixin(String name, {List<String> children: const []}) { | |
15 if (name.isEmpty) return null; // don't use a mixin (override any default) | |
16 | |
17 if (declaredMixins.containsKey(name)) { | |
18 var dartMixin = declaredMixins[name]; | |
19 | |
20 PbMixin parent; | |
21 if (dartMixin.hasParent()) { | |
22 var parentName = dartMixin.parent; | |
23 if (children.contains(parentName)) { | |
skybrian
2016/06/22 20:29:52
Seems better to check for cycles sooner? The error
frederikmutzel
2016/06/23 12:30:16
Moved to file_generator.
I also moved the convers
| |
24 var chain = children.join('->') + '->$name'; | |
25 throw ('cycle in mixin parents: $chain'); | |
26 } | |
27 children.add(name); | |
28 var parent = resolveMixin(dartMixin.parent, children: children); | |
29 if (parent == null) { | |
30 throw ('unknown mixin parent: $parent of $name'); | |
31 } | |
32 } | |
33 return new PbMixin(dartMixin.name, | |
34 importFrom: dartMixin.importFrom, parent: parent); | |
35 } | |
36 | |
37 var internalMixin = findMixin(name); | |
38 if (internalMixin == null) throw ("unknown mixin class: ${name}"); | |
39 return internalMixin; | |
40 } | |
41 | |
42 if (!desc.hasOptions() || !desc.options.hasExtension(Dart_options.mixin)) { | |
43 return resolveMixin(defaultMixin); | |
44 } | |
12 | 45 |
13 String name = desc.options.getExtension(Dart_options.mixin); | 46 String name = desc.options.getExtension(Dart_options.mixin); |
14 if (name.isEmpty) return null; // don't use a mixin (override any default) | 47 return resolveMixin(name); |
15 var mixin = findMixin(name); | |
16 if (mixin == null) { | |
17 throw ("unknown mixin class: ${name}"); | |
18 } | |
19 return mixin; | |
20 } | 48 } |
21 | 49 |
22 final String classname; | 50 final String classname; |
23 final String fqname; | 51 final String fqname; |
24 final PbMixin mixin; | 52 final PbMixin mixin; |
25 | 53 |
26 final ProtobufContainer _parent; | 54 final ProtobufContainer _parent; |
27 final DescriptorProto _descriptor; | 55 final DescriptorProto _descriptor; |
28 final List<EnumGenerator> _enumGenerators = <EnumGenerator>[]; | 56 final List<EnumGenerator> _enumGenerators = <EnumGenerator>[]; |
29 final List<MessageGenerator> _messageGenerators = <MessageGenerator>[]; | 57 final List<MessageGenerator> _messageGenerators = <MessageGenerator>[]; |
30 final List<ExtensionGenerator> _extensionGenerators = <ExtensionGenerator>[]; | 58 final List<ExtensionGenerator> _extensionGenerators = <ExtensionGenerator>[]; |
31 | 59 |
32 // populated by resolve() | 60 // populated by resolve() |
33 List<ProtobufField> _fieldList; | 61 List<ProtobufField> _fieldList; |
34 | 62 |
35 MessageGenerator(DescriptorProto descriptor, ProtobufContainer parent, | 63 MessageGenerator(DescriptorProto descriptor, ProtobufContainer parent, |
36 PbMixin defaultMixin) | 64 Map<String, DartMixin> declaredMixins, String defaultMixin) |
37 : _descriptor = descriptor, | 65 : _descriptor = descriptor, |
38 _parent = parent, | 66 _parent = parent, |
39 classname = (parent.classname == '') | 67 classname = (parent.classname == '') |
40 ? descriptor.name | 68 ? descriptor.name |
41 : '${parent.classname}_${descriptor.name}', | 69 : '${parent.classname}_${descriptor.name}', |
42 fqname = (parent == null || parent.fqname == null) | 70 fqname = (parent == null || parent.fqname == null) |
43 ? descriptor.name | 71 ? descriptor.name |
44 : (parent.fqname == '.' | 72 : (parent.fqname == '.' |
45 ? '.${descriptor.name}' | 73 ? '.${descriptor.name}' |
46 : '${parent.fqname}.${descriptor.name}'), | 74 : '${parent.fqname}.${descriptor.name}'), |
47 mixin = _getMixin(descriptor, defaultMixin) { | 75 mixin = _getMixin(descriptor, declaredMixins, defaultMixin ?? '') { |
48 for (EnumDescriptorProto e in _descriptor.enumType) { | 76 for (EnumDescriptorProto e in _descriptor.enumType) { |
49 _enumGenerators.add(new EnumGenerator(e, this)); | 77 _enumGenerators.add(new EnumGenerator(e, this)); |
50 } | 78 } |
51 | 79 |
52 for (DescriptorProto n in _descriptor.nestedType) { | 80 for (DescriptorProto n in _descriptor.nestedType) { |
53 _messageGenerators.add(new MessageGenerator(n, this, defaultMixin)); | 81 _messageGenerators |
82 .add(new MessageGenerator(n, this, declaredMixins, defaultMixin)); | |
54 } | 83 } |
55 | 84 |
56 for (FieldDescriptorProto x in _descriptor.extension) { | 85 for (FieldDescriptorProto x in _descriptor.extension) { |
57 _extensionGenerators.add(new ExtensionGenerator(x, this)); | 86 _extensionGenerators.add(new ExtensionGenerator(x, this)); |
58 } | 87 } |
59 } | 88 } |
60 | 89 |
61 String get package => _parent.package; | 90 String get package => _parent.package; |
62 | 91 |
63 /// The generator of the .pb.dart file that will declare this type. | 92 /// The generator of the .pb.dart file that will declare this type. |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 | 403 |
375 for (var m in _messageGenerators) { | 404 for (var m in _messageGenerators) { |
376 m.generateConstants(out); | 405 m.generateConstants(out); |
377 } | 406 } |
378 | 407 |
379 for (var e in _enumGenerators) { | 408 for (var e in _enumGenerators) { |
380 e.generateConstants(out); | 409 e.generateConstants(out); |
381 } | 410 } |
382 } | 411 } |
383 } | 412 } |
OLD | NEW |