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

Side by Side Diff: lib/enum_generator.dart

Issue 93743006: Use package names as import prefixes when generating code (Closed) Base URL: https://github.com/dart-lang/dart-protoc-plugin.git@master
Patch Set: Addressed review commetns Created 6 years, 11 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
« no previous file with comments | « lib/code_generator.dart ('k') | lib/extension_generator.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 EnumAlias { 7 class EnumAlias {
8 final EnumValueDescriptorProto value; 8 final EnumValueDescriptorProto value;
9 final EnumValueDescriptorProto canonicalValue; 9 final EnumValueDescriptorProto canonicalValue;
10 EnumAlias(this.value, this.canonicalValue); 10 EnumAlias(this.value, this.canonicalValue);
11 } 11 }
12 12
13 class EnumGenerator implements ProtobufContainer { 13 class EnumGenerator implements ProtobufContainer {
14 final ProtobufContainer _parent;
14 final String classname; 15 final String classname;
15 final String fqname; 16 final String fqname;
16 final List<EnumValueDescriptorProto> _canonicalValues = 17 final List<EnumValueDescriptorProto> _canonicalValues =
17 <EnumValueDescriptorProto>[]; 18 <EnumValueDescriptorProto>[];
18 final List<EnumAlias> _aliases = <EnumAlias>[]; 19 final List<EnumAlias> _aliases = <EnumAlias>[];
19 20
20 EnumGenerator( 21 EnumGenerator(
21 EnumDescriptorProto descriptor, 22 EnumDescriptorProto descriptor,
22 ProtobufContainer parent, 23 ProtobufContainer parent,
23 GenerationContext context) 24 GenerationContext context)
24 : classname = (parent == null || parent is FileGenerator) ? 25 : _parent = parent,
26 classname = (parent == null || parent is FileGenerator) ?
25 descriptor.name : '${parent.classname}_${descriptor.name}', 27 descriptor.name : '${parent.classname}_${descriptor.name}',
26 fqname = (parent == null || parent.fqname == null) ? descriptor.name : 28 fqname = (parent == null || parent.fqname == null) ? descriptor.name :
27 (parent.fqname == '.' ? 29 (parent.fqname == '.' ?
28 '.${descriptor.name}' : '${parent.fqname}.${descriptor.name}') { 30 '.${descriptor.name}' : '${parent.fqname}.${descriptor.name}') {
29 for (EnumValueDescriptorProto value in descriptor.value) { 31 for (EnumValueDescriptorProto value in descriptor.value) {
30 EnumValueDescriptorProto canonicalValue = 32 EnumValueDescriptorProto canonicalValue =
31 descriptor.value.firstWhere((v) => v.number == value.number); 33 descriptor.value.firstWhere((v) => v.number == value.number);
32 if (value == canonicalValue) { 34 if (value == canonicalValue) {
33 _canonicalValues.add(value); 35 _canonicalValues.add(value);
34 } else { 36 } else {
35 _aliases.add(new EnumAlias(value, canonicalValue)); 37 _aliases.add(new EnumAlias(value, canonicalValue));
36 } 38 }
37 } 39 }
38 context.register(this); 40 context.register(this);
39 } 41 }
40 42
43 String get package => _parent.package;
44
41 void generate(IndentingWriter out) { 45 void generate(IndentingWriter out) {
42 out.addBlock('class ${classname} extends ProtobufEnum {', '}\n', () { 46 out.addBlock('class ${classname} extends ProtobufEnum {', '}\n', () {
43 // ----------------------------------------------------------------- 47 // -----------------------------------------------------------------
44 // Define enum types. 48 // Define enum types.
45 for (EnumValueDescriptorProto val in _canonicalValues) { 49 for (EnumValueDescriptorProto val in _canonicalValues) {
46 out.println( 50 out.println(
47 'static const ${classname} ${val.name}${SP}=${SP}' 51 'static const ${classname} ${val.name}${SP}=${SP}'
48 "const ${classname}._(${val.number},${SP}'${val.name}');"); 52 "const ${classname}._(${val.number},${SP}'${val.name}');");
49 } 53 }
50 if (!_aliases.isEmpty) { 54 if (!_aliases.isEmpty) {
(...skipping 18 matching lines...) Expand all
69 '${SP}ProtobufEnum.initByValue(values);'); 73 '${SP}ProtobufEnum.initByValue(values);');
70 out.println('static ${classname} valueOf(int value)${SP}=>' 74 out.println('static ${classname} valueOf(int value)${SP}=>'
71 '${SP}_byValue[value];'); 75 '${SP}_byValue[value];');
72 out.println(); 76 out.println();
73 77
74 out.println('const ${classname}._(int v,${SP}String n)${SP}' 78 out.println('const ${classname}._(int v,${SP}String n)${SP}'
75 ':${SP}super(v,${SP}n);'); 79 ':${SP}super(v,${SP}n);');
76 }); 80 });
77 } 81 }
78 } 82 }
OLDNEW
« no previous file with comments | « lib/code_generator.dart ('k') | lib/extension_generator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698