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 ExtensionGenerator implements ProtobufContainer { | 7 class ExtensionGenerator implements ProtobufContainer { |
8 final String fqname; | 8 final String fqname; |
9 final FieldDescriptorProto _descriptor; | 9 final FieldDescriptorProto _descriptor; |
10 final ProtobufContainer _parent; | 10 final ProtobufContainer _parent; |
11 final GenerationContext _context; | 11 final GenerationContext _context; |
12 | 12 |
13 ExtensionGenerator( | 13 ExtensionGenerator( |
14 FieldDescriptorProto descriptor, | 14 FieldDescriptorProto descriptor, |
15 ProtobufContainer parent, | 15 ProtobufContainer parent, |
16 GenerationContext context) | 16 GenerationContext context) |
17 : this._descriptor = descriptor, | 17 : this._descriptor = descriptor, |
18 this._parent = parent, | 18 this._parent = parent, |
19 this._context = context, | 19 this._context = context, |
20 fqname = parent.fqname == '.' ? | 20 fqname = parent.fqname == '.' ? |
21 '.${descriptor.name}' : | 21 '.${descriptor.name}' : |
22 '${parent.fqname}.${descriptor.name}'; | 22 '${parent.fqname}.${descriptor.name}'; |
23 | 23 |
| 24 String get package => _parent.package; |
| 25 |
24 String get classname { | 26 String get classname { |
25 String name = new ProtobufField( | 27 String name = new ProtobufField( |
26 _descriptor, _parent, _context).externalFieldName; | 28 _descriptor, _parent, _context).externalFieldName; |
27 return _parent is MessageGenerator ? '${_parent.classname}.$name' : name; | 29 return _parent is MessageGenerator ? '${_parent.classname}.$name' : name; |
28 } | 30 } |
29 | 31 |
30 void generate(IndentingWriter out) { | 32 void generate(IndentingWriter out) { |
31 ProtobufField field = new ProtobufField(_descriptor, _parent, _context); | 33 ProtobufField field = new ProtobufField(_descriptor, _parent, _context); |
32 String baseType = field.baseType; | 34 String baseType = field.baseTypeForPackage(package); |
33 | 35 |
34 String name = field.externalFieldName; | 36 String name = field.externalFieldName; |
35 String type = field.shortTypeName; | 37 String type = field.shortTypeName; |
36 | 38 |
37 String typeName = ''; | |
38 ProtobufContainer typeNameContainer = _context[_descriptor.typeName]; | |
39 if (typeNameContainer != null) { | |
40 typeName = typeNameContainer.classname; | |
41 } | |
42 | |
43 String extendee = ''; | 39 String extendee = ''; |
44 ProtobufContainer extendeeContainer = _context[_descriptor.extendee]; | 40 ProtobufContainer extendeeContainer = _context[_descriptor.extendee]; |
45 if (extendeeContainer != null) { | 41 if (extendeeContainer != null) { |
46 extendee = extendeeContainer.classname; | 42 extendee = extendeeContainer.classname; |
47 } | 43 } |
48 | 44 |
49 | 45 |
50 String initializer = ''; | 46 String initializer = ''; |
51 String builder = ''; | 47 String builder = ''; |
52 String valueOf = ''; | 48 String valueOf = ''; |
53 | 49 |
54 if (_descriptor.type == FieldDescriptorProto_Type.TYPE_MESSAGE || | 50 if (_descriptor.type == FieldDescriptorProto_Type.TYPE_MESSAGE || |
55 _descriptor.type == FieldDescriptorProto_Type.TYPE_GROUP) { | 51 _descriptor.type == FieldDescriptorProto_Type.TYPE_GROUP) { |
56 if (_descriptor.label == | 52 if (_descriptor.label == |
57 FieldDescriptorProto_Label.LABEL_REPEATED) { | 53 FieldDescriptorProto_Label.LABEL_REPEATED) { |
58 initializer = ',${SP}()${SP}=>${SP}new PbList<${typeName}>()'; | 54 initializer = ',${SP}()${SP}=>${SP}new PbList<${baseType}>()'; |
59 builder = ',${SP}()${SP}=>${SP}new ${typeName}()'; | 55 builder = ',${SP}()${SP}=>${SP}new ${baseType}()'; |
60 } else { | 56 } else { |
61 initializer = ',${SP}()${SP}=>${SP}new ${typeName}()'; | 57 initializer = ',${SP}()${SP}=>${SP}new ${baseType}()'; |
62 builder = ',${SP}()${SP}=>${SP}new ${typeName}()'; | 58 builder = ',${SP}()${SP}=>${SP}new ${baseType}()'; |
63 } | 59 } |
64 } else { | 60 } else { |
65 if (_descriptor.label == FieldDescriptorProto_Label.LABEL_REPEATED) { | 61 if (_descriptor.label == FieldDescriptorProto_Label.LABEL_REPEATED) { |
66 initializer = ',${SP}()${SP}=>${SP}new PbList<${baseType}>()'; | 62 initializer = ',${SP}()${SP}=>${SP}new PbList<${baseType}>()'; |
67 } else if (field.hasInitialization) { | 63 } else if (field.hasInitialization) { |
68 initializer = ',${SP}${field.initialization}'; | 64 var fieldInitialization = field.initializationForPackage(package); |
| 65 initializer = ',${SP}${fieldInitialization}'; |
69 } | 66 } |
70 } | 67 } |
71 | 68 |
72 if (field.enm) { | 69 if (field.enm) { |
73 if (initializer.isEmpty) { | 70 if (initializer.isEmpty) { |
74 initializer = ',${SP}null'; | 71 initializer = ',${SP}null'; |
75 } | 72 } |
76 if (builder.isEmpty) { | 73 if (builder.isEmpty) { |
77 builder = ',${SP}null'; | 74 builder = ',${SP}null'; |
78 } | 75 } |
79 valueOf = ',${SP}(var v)${SP}=>${SP}${field.baseType}.valueOf(v)'; | 76 var fieldType = field.baseTypeForPackage(package); |
| 77 valueOf = ',${SP}(var v)${SP}=>${SP}${fieldType}.valueOf(v)'; |
80 } | 78 } |
81 | 79 |
82 out.println('static final Extension $name${SP}=${SP}' | 80 out.println('static final Extension $name${SP}=${SP}' |
83 'new Extension(\'$extendee\',${SP}\'$name\',${SP}' | 81 'new Extension(\'$extendee\',${SP}\'$name\',${SP}' |
84 '${_descriptor.number},${SP}GeneratedMessage.$type' | 82 '${_descriptor.number},${SP}GeneratedMessage.$type' |
85 '${initializer}${builder}${valueOf}' | 83 '${initializer}${builder}${valueOf}' |
86 ');'); | 84 ');'); |
87 } | 85 } |
88 | 86 |
89 String get name => classname; | 87 String get name => classname; |
90 } | 88 } |
OLD | NEW |