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

Side by Side Diff: lib/enum_generator.dart

Issue 1829573002: Fix all strong mode warnings in protoc-plugin (Closed) Base URL: git@github.com:dart-lang/dart-protoc-plugin.git@master
Patch Set: regenerate pb.dart files Created 4 years, 8 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/const_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 extends ProtobufContainer { 13 class EnumGenerator extends ProtobufContainer {
14 final ProtobufContainer _parent; 14 final ProtobufContainer _parent;
15 final String classname; 15 final String classname;
16 final String fqname; 16 final String fqname;
17 final EnumDescriptorProto _descriptor; 17 final EnumDescriptorProto _descriptor;
18 final List<EnumValueDescriptorProto> _canonicalValues = 18 final List<EnumValueDescriptorProto> _canonicalValues =
19 <EnumValueDescriptorProto>[]; 19 <EnumValueDescriptorProto>[];
20 final List<EnumAlias> _aliases = <EnumAlias>[]; 20 final List<EnumAlias> _aliases = <EnumAlias>[];
21 21
22 EnumGenerator( 22 EnumGenerator(EnumDescriptorProto descriptor, ProtobufContainer parent)
23 EnumDescriptorProto descriptor, 23 : _parent = parent,
24 ProtobufContainer parent) 24 classname = (parent == null || parent is FileGenerator)
25 : _parent = parent, 25 ? descriptor.name
26 classname = (parent == null || parent is FileGenerator) ? 26 : '${parent.classname}_${descriptor.name}',
27 descriptor.name : '${parent.classname}_${descriptor.name}', 27 fqname = (parent == null || parent.fqname == null)
28 fqname = (parent == null || parent.fqname == null) ? descriptor.name : 28 ? descriptor.name
29 (parent.fqname == '.' ? 29 : (parent.fqname == '.'
30 '.${descriptor.name}' : '${parent.fqname}.${descriptor.name}'), 30 ? '.${descriptor.name}'
31 _descriptor = descriptor { 31 : '${parent.fqname}.${descriptor.name}'),
32 _descriptor = descriptor {
32 for (EnumValueDescriptorProto value in descriptor.value) { 33 for (EnumValueDescriptorProto value in descriptor.value) {
33 EnumValueDescriptorProto canonicalValue = 34 EnumValueDescriptorProto canonicalValue =
34 descriptor.value.firstWhere((v) => v.number == value.number); 35 descriptor.value.firstWhere((v) => v.number == value.number);
35 if (value == canonicalValue) { 36 if (value == canonicalValue) {
36 _canonicalValues.add(value); 37 _canonicalValues.add(value);
37 } else { 38 } else {
38 _aliases.add(new EnumAlias(value, canonicalValue)); 39 _aliases.add(new EnumAlias(value, canonicalValue));
39 } 40 }
40 } 41 }
41 } 42 }
(...skipping 14 matching lines...) Expand all
56 return name; 57 return name;
57 } 58 }
58 return "$packageImportPrefix.$name"; 59 return "$packageImportPrefix.$name";
59 } 60 }
60 61
61 void generate(IndentingWriter out) { 62 void generate(IndentingWriter out) {
62 out.addBlock('class ${classname} extends ProtobufEnum {', '}\n', () { 63 out.addBlock('class ${classname} extends ProtobufEnum {', '}\n', () {
63 // ----------------------------------------------------------------- 64 // -----------------------------------------------------------------
64 // Define enum types. 65 // Define enum types.
65 for (EnumValueDescriptorProto val in _canonicalValues) { 66 for (EnumValueDescriptorProto val in _canonicalValues) {
66 out.println( 67 out.println('static const ${classname} ${val.name} = '
67 'static const ${classname} ${val.name} = ' 68 "const ${classname}._(${val.number}, '${val.name}');");
68 "const ${classname}._(${val.number}, '${val.name}');");
69 } 69 }
70 if (!_aliases.isEmpty) { 70 if (!_aliases.isEmpty) {
71 out.println(); 71 out.println();
72 for (EnumAlias alias in _aliases) { 72 for (EnumAlias alias in _aliases) {
73 out.println('static const ${classname} ${alias.value.name} =' 73 out.println('static const ${classname} ${alias.value.name} ='
74 ' ${alias.canonicalValue.name};'); 74 ' ${alias.canonicalValue.name};');
75 } 75 }
76 } 76 }
77 out.println(); 77 out.println();
78 78
79 out.println( 79 out.println('static const List<${classname}> values ='
80 'static const List<${classname}> values =' 80 ' const <${classname}> [');
81 ' const <${classname}> [');
82 for (EnumValueDescriptorProto val in _canonicalValues) { 81 for (EnumValueDescriptorProto val in _canonicalValues) {
83 out.println(' ${val.name},'); 82 out.println(' ${val.name},');
84 } 83 }
85 out.println('];'); 84 out.println('];');
86 out.println(); 85 out.println();
87 86
88 out.println('static final Map<int, ${classname}> _byValue =' 87 out.println('static final Map<int, dynamic> _byValue ='
89 ' ProtobufEnum.initByValue(values);'); 88 ' ProtobufEnum.initByValue(values);');
90 out.println('static ${classname} valueOf(int value) =>' 89 out.println('static ${classname} valueOf(int value) =>'
91 ' _byValue[value];'); 90 ' _byValue[value] as ${classname};');
92 out.addBlock('static void $checkItem($classname v) {', '}', () { 91 out.addBlock('static void $checkItem($classname v) {', '}', () {
93 out.println('if (v is !$classname)' 92 out.println('if (v is !$classname)'
94 " checkItemFailed(v, '$classname');"); 93 " checkItemFailed(v, '$classname');");
95 }); 94 });
96 out.println(); 95 out.println();
97 96
98 out.println('const ${classname}._(int v, String n) ' 97 out.println('const ${classname}._(int v, String n) '
99 ': super(v, n);'); 98 ': super(v, n);');
100 }); 99 });
101 } 100 }
102 101
103 /// Writes a Dart constant containing the JSON for the EnumProtoDescriptor. 102 /// Writes a Dart constant containing the JSON for the EnumProtoDescriptor.
104 void generateConstants(IndentingWriter out) { 103 void generateConstants(IndentingWriter out) {
105 var name = getJsonConstant(fileGen); 104 var name = getJsonConstant(fileGen);
106 var json = _descriptor.writeToJsonMap(); 105 var json = _descriptor.writeToJsonMap();
107 106
108 out.print("const $name = "); 107 out.print("const $name = ");
109 writeJsonConst(out, json); 108 writeJsonConst(out, json);
110 out.println(";"); 109 out.println(";");
111 out.println(); 110 out.println();
112 } 111 }
113 } 112 }
OLDNEW
« no previous file with comments | « lib/const_generator.dart ('k') | lib/extension_generator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698