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 ProtobufField { | 7 class ProtobufField { |
8 static final RegExp HEX_LITERAL_REGEX = | 8 static final RegExp HEX_LITERAL_REGEX = |
9 new RegExp(r'^0x[0-9a-f]+$', multiLine: false, caseSensitive: false); | 9 new RegExp(r'^0x[0-9a-f]+$', multiLine: false, caseSensitive: false); |
10 static final RegExp INTEGER_LITERAL_REGEX = new RegExp(r'^[+-]?[0-9]+$'); | 10 static final RegExp INTEGER_LITERAL_REGEX = new RegExp(r'^[+-]?[0-9]+$'); |
11 static final RegExp DECIMAL_LITERAL_REGEX_A = | 11 static final RegExp DECIMAL_LITERAL_REGEX_A = |
12 new RegExp(r'^[+-]?([0-9]*)\.[0-9]+(e[+-]?[0-9]+)?$', | 12 new RegExp(r'^[+-]?([0-9]*)\.[0-9]+(e[+-]?[0-9]+)?$', |
13 multiLine: false, caseSensitive: false); | 13 multiLine: false, caseSensitive: false); |
14 static final RegExp DECIMAL_LITERAL_REGEX_B = | 14 static final RegExp DECIMAL_LITERAL_REGEX_B = |
15 new RegExp(r'^[+-]?[0-9]+e[+-]?[0-9]+$', multiLine: false, | 15 new RegExp(r'^[+-]?[0-9]+e[+-]?[0-9]+$', multiLine: false, |
16 caseSensitive: false); | 16 caseSensitive: false); |
17 | 17 |
18 final FieldDescriptorProto _field; | 18 final FieldDescriptorProto _field; |
19 final ProtobufContainer parent; | 19 final ProtobufContainer parent; |
20 final GenerationContext context; | 20 final GenerationContext context; |
21 final String fqname; | 21 final String fqname; |
Siggi Cherem (dart-lang)
2013/12/20 18:27:36
I know this is unrelated, but what does 'fqname' s
Søren Gjesse
2014/01/02 08:52:33
fqname is the "fully qualified name", that is the
Siggi Cherem (dart-lang)
2014/01/02 18:29:46
I see, thanks for the clarification!
I think rena
| |
22 final String typePackage; | |
22 final String baseType; | 23 final String baseType; |
23 final String typeString; | 24 final String typeString; |
25 final String prefixedBaseType; | |
Siggi Cherem (dart-lang)
2013/12/20 18:27:36
should we make these getters instead?
Søren Gjesse
2014/01/02 08:52:33
I decided to keep the current structure of final f
| |
26 final String prefixedTypeString; | |
24 final String codedStreamType; | 27 final String codedStreamType; |
25 final bool repeats; | 28 final bool repeats; |
26 final String initialization; | 29 final String initialization; |
30 final String prefixedInitialization; | |
27 final bool required; | 31 final bool required; |
28 // True if the field is to be encoded with [packed=true] encoding. | 32 // True if the field is to be encoded with [packed=true] encoding. |
29 final bool packed; | 33 final bool packed; |
30 // True if the fields's type can handle [packed=true] encoding. | 34 // True if the fields's type can handle [packed=true] encoding. |
31 final bool packable; | 35 final bool packable; |
32 | 36 |
33 bool get single => !repeats; | 37 bool get single => !repeats; |
34 | 38 |
35 bool get group => type == FieldDescriptorProto_Type.TYPE_GROUP; | 39 bool get group => type == FieldDescriptorProto_Type.TYPE_GROUP; |
36 bool get message => type == FieldDescriptorProto_Type.TYPE_MESSAGE; | 40 bool get message => type == FieldDescriptorProto_Type.TYPE_MESSAGE; |
37 bool get enm => type == FieldDescriptorProto_Type.TYPE_ENUM; | 41 bool get enm => type == FieldDescriptorProto_Type.TYPE_ENUM; |
38 bool get primitive => !group && !message; | 42 bool get primitive => !group && !message; |
39 | 43 |
40 bool get hasInitialization => initialization != null; | 44 bool get hasInitialization => initialization != null; |
41 | 45 |
42 bool get optional => !required; // includes repeated | 46 bool get optional => !required; // includes repeated |
43 | 47 |
44 // Delegate methods. | 48 // Delegate methods. |
45 String get name => _field.name; | 49 String get name => _field.name; |
46 int get number => _field.number; | 50 int get number => _field.number; |
47 FieldDescriptorProto_Label get label => _field.label; | 51 FieldDescriptorProto_Label get label => _field.label; |
48 FieldDescriptorProto_Type get type => _field.type; | 52 FieldDescriptorProto_Type get type => _field.type; |
49 FieldOptions get options => _field.options; | 53 FieldOptions get options => _field.options; |
50 String get typeName => _field.typeName; | 54 String get typeName => _field.typeName; |
51 | 55 |
56 String baseTypeForPackage(String package) => | |
57 package == typePackage ? baseType : prefixedBaseType; | |
58 String typeStringForPackage(String package) => | |
59 package == typePackage ? typeString : prefixedTypeString; | |
60 String initializationForPackage(String package) => | |
61 package == typePackage ? initialization : prefixedInitialization; | |
62 | |
52 String get shortTypeName { | 63 String get shortTypeName { |
53 String prefix; | 64 String prefix; |
54 if (required) { | 65 if (required) { |
55 prefix = 'Q'; | 66 prefix = 'Q'; |
56 } else if (packed) { | 67 } else if (packed) { |
57 prefix = 'K'; | 68 prefix = 'K'; |
58 } else if (repeats) { | 69 } else if (repeats) { |
59 prefix = 'P'; | 70 prefix = 'P'; |
60 } else if (optional) { | 71 } else if (optional) { |
61 prefix = 'O'; | 72 prefix = 'O'; |
(...skipping 17 matching lines...) Expand all Loading... | |
79 case 'FIXED32': return '${prefix}F3'; | 90 case 'FIXED32': return '${prefix}F3'; |
80 case 'FIXED64': return '${prefix}F6'; | 91 case 'FIXED64': return '${prefix}F6'; |
81 case 'SFIXED32': return '${prefix}SF3'; | 92 case 'SFIXED32': return '${prefix}SF3'; |
82 case 'SFIXED64': return '${prefix}SF6'; | 93 case 'SFIXED64': return '${prefix}SF6'; |
83 case 'MESSAGE': return '${prefix}M'; | 94 case 'MESSAGE': return '${prefix}M'; |
84 } | 95 } |
85 throw 'Unknown type'; | 96 throw 'Unknown type'; |
86 } | 97 } |
87 | 98 |
88 ProtobufField._( | 99 ProtobufField._( |
89 field, parent, this.context, this.baseType, this.typeString, | 100 field, parent, this.context, this.typePackage, this.baseType, |
90 this.codedStreamType, this.repeats, this.initialization, this.required, | 101 this.typeString, this.prefixedBaseType, this.prefixedTypeString, |
102 this.codedStreamType, this.repeats, | |
103 this.initialization, this.prefixedInitialization, this.required, | |
91 this.packed, this.packable) : | 104 this.packed, this.packable) : |
92 this._field = field, | 105 this._field = field, |
93 this.parent = parent, | 106 this.parent = parent, |
94 fqname = '${parent.fqname}.${field.name}'; | 107 fqname = '${parent.fqname}.${field.name}'; |
95 | 108 |
96 | 109 |
97 factory ProtobufField(FieldDescriptorProto field, | 110 factory ProtobufField(FieldDescriptorProto field, |
98 MessageGenerator parent, | 111 MessageGenerator parent, |
99 GenerationContext context) { | 112 GenerationContext context) { |
100 bool required = field.label == FieldDescriptorProto_Label.LABEL_REQUIRED; | 113 bool required = field.label == FieldDescriptorProto_Label.LABEL_REQUIRED; |
101 bool repeats = field.label == FieldDescriptorProto_Label.LABEL_REPEATED; | 114 bool repeats = field.label == FieldDescriptorProto_Label.LABEL_REPEATED; |
102 bool packed = false; | 115 bool packed = false; |
103 | 116 |
104 var write; | 117 var write; |
105 if (repeats) { | 118 if (repeats) { |
106 packed = field.options == null ? false : field.options.packed; | 119 packed = field.options == null ? false : field.options.packed; |
107 write = (String typeString) => 'List<$typeString>'; | 120 write = (String typeString) => 'List<$typeString>'; |
108 } else { | 121 } else { |
109 write = (String typeString) => typeString; | 122 write = (String typeString) => typeString; |
110 } | 123 } |
111 | 124 |
125 String typePackage = ''; | |
112 String baseType; | 126 String baseType; |
113 String typeString; | 127 String typeString; |
128 String prefixedBaseType; | |
129 String prefixedTypeString; | |
114 bool packable = false; | 130 bool packable = false; |
115 String codedStreamType; | 131 String codedStreamType; |
116 String initialization; | 132 String initialization; |
133 String prefixedInitialization; | |
117 switch (field.type) { | 134 switch (field.type) { |
118 case FieldDescriptorProto_Type.TYPE_BOOL: | 135 case FieldDescriptorProto_Type.TYPE_BOOL: |
119 baseType = 'bool'; | 136 baseType = 'bool'; |
120 typeString = write('bool'); | 137 typeString = write('bool'); |
121 packable = true; | 138 packable = true; |
122 codedStreamType = 'Bool'; | 139 codedStreamType = 'Bool'; |
123 if (!repeats) { | 140 if (!repeats) { |
124 if (field.hasDefaultValue() && 'false' != field.defaultValue) { | 141 if (field.hasDefaultValue() && 'false' != field.defaultValue) { |
125 initialization = '()${SP}=>${SP}${field.defaultValue}'; | 142 initialization = '()${SP}=>${SP}${field.defaultValue}'; |
126 } | 143 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 String byteList = field.defaultValue.codeUnits | 257 String byteList = field.defaultValue.codeUnits |
241 .map((b) => '0x${b.toRadixString(16)}') | 258 .map((b) => '0x${b.toRadixString(16)}') |
242 .join(','); | 259 .join(','); |
243 initialization = '()${SP}=>${SP}<int>[$byteList]'; | 260 initialization = '()${SP}=>${SP}<int>[$byteList]'; |
244 } | 261 } |
245 } | 262 } |
246 break; | 263 break; |
247 case FieldDescriptorProto_Type.TYPE_GROUP: | 264 case FieldDescriptorProto_Type.TYPE_GROUP: |
248 ProtobufContainer groupType = context[field.typeName]; | 265 ProtobufContainer groupType = context[field.typeName]; |
249 if (groupType != null) { | 266 if (groupType != null) { |
267 typePackage = groupType.package; | |
250 baseType = groupType.classname; | 268 baseType = groupType.classname; |
251 typeString = write(groupType.classname); | 269 typeString = write(groupType.classname); |
270 prefixedBaseType = groupType.package + '.' + baseType; | |
271 prefixedTypeString = write(prefixedBaseType); | |
252 codedStreamType = 'Group'; | 272 codedStreamType = 'Group'; |
253 } else { | 273 } else { |
254 throw 'FAILURE: Unknown group type reference ${field.typeName}'; | 274 throw 'FAILURE: Unknown group type reference ${field.typeName}'; |
255 } | 275 } |
256 initialization = '()${SP}=>${SP}new ${baseType}()'; | 276 initialization = '()${SP}=>${SP}new ${baseType}()'; |
277 prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()'; | |
257 break; | 278 break; |
258 case FieldDescriptorProto_Type.TYPE_MESSAGE: | 279 case FieldDescriptorProto_Type.TYPE_MESSAGE: |
259 ProtobufContainer messageType = context[field.typeName]; | 280 ProtobufContainer messageType = context[field.typeName]; |
260 if (messageType != null) { | 281 if (messageType != null) { |
282 typePackage = messageType.package; | |
261 baseType = messageType.classname; | 283 baseType = messageType.classname; |
262 typeString = write(messageType.classname); | 284 typeString = write(baseType); |
285 prefixedBaseType = messageType.package + '.' + baseType; | |
286 prefixedTypeString = write(prefixedBaseType); | |
263 codedStreamType = 'Message'; | 287 codedStreamType = 'Message'; |
264 } else { | 288 } else { |
265 throw 'FAILURE: Unknown message type reference ${field.typeName}'; | 289 throw 'FAILURE: Unknown message type reference ${field.typeName}'; |
266 } | 290 } |
267 initialization = '()${SP}=>${SP}new ${baseType}()'; | 291 initialization = '()${SP}=>${SP}new ${baseType}()'; |
292 prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()'; | |
268 break; | 293 break; |
269 case FieldDescriptorProto_Type.TYPE_ENUM: | 294 case FieldDescriptorProto_Type.TYPE_ENUM: |
270 EnumGenerator enumType = context[field.typeName]; | 295 EnumGenerator enumType = context[field.typeName]; |
271 if (enumType != null) { | 296 if (enumType != null) { |
297 typePackage = enumType.package; | |
272 baseType = enumType.classname; | 298 baseType = enumType.classname; |
273 typeString = write(enumType.classname); | 299 typeString = write(enumType.classname); |
274 codedStreamType = 'Enum'; | 300 codedStreamType = 'Enum'; |
301 prefixedBaseType = enumType.package + '.' + baseType; | |
302 prefixedTypeString = write(prefixedBaseType); | |
275 packable = true; | 303 packable = true; |
276 if (!repeats) { | 304 if (!repeats) { |
277 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { | 305 if (field.hasDefaultValue() && !field.defaultValue.isEmpty) { |
278 initialization = | 306 initialization = |
279 '()${SP}=>${SP}${enumType.classname}.${field.defaultValue}'; | 307 '()${SP}=>${SP}${baseType}.${field.defaultValue}'; |
308 prefixedInitialization = | |
309 '()${SP}=>${SP}${prefixedBaseType}.${field.defaultValue}'; | |
280 } else if (!enumType._canonicalValues.isEmpty) { | 310 } else if (!enumType._canonicalValues.isEmpty) { |
281 initialization = | 311 initialization = |
282 '()${SP}=>${SP}${enumType.classname}.' | 312 '()${SP}=>${SP}${baseType}.' |
313 '${enumType._canonicalValues[0].name}'; | |
314 prefixedInitialization = | |
315 '()${SP}=>${SP}${prefixedBaseType}.' | |
283 '${enumType._canonicalValues[0].name}'; | 316 '${enumType._canonicalValues[0].name}'; |
284 } | 317 } |
285 } | 318 } |
286 } else { | 319 } else { |
287 throw 'FAILURE: Unknown enum type reference ${field.typeName}'; | 320 throw 'FAILURE: Unknown enum type reference ${field.typeName}'; |
288 } | 321 } |
289 break; | 322 break; |
290 default: | 323 default: |
291 throw 'Unknown type ${field.type.name}'; | 324 throw 'Unknown type ${field.type.name}'; |
292 // No default -- should be an error. | 325 // No default -- should be an error. |
293 } | 326 } |
294 | 327 |
295 if (repeats) { | 328 if (repeats) { |
296 initialization = '()${SP}=>${SP}new PbList()'; | 329 initialization = '()${SP}=>${SP}new PbList()'; |
297 } | 330 } |
298 | 331 |
332 if (prefixedBaseType == null) prefixedBaseType = baseType; | |
333 if (prefixedTypeString == null) prefixedTypeString = typeString; | |
334 if (prefixedInitialization == null) prefixedInitialization = initialization; | |
299 return new ProtobufField._( | 335 return new ProtobufField._( |
300 field, parent, context, baseType, typeString, codedStreamType, repeats, | 336 field, parent, context, typePackage, baseType, typeString, |
301 initialization, required, packed, packable); | 337 prefixedBaseType, prefixedTypeString, codedStreamType, repeats, |
338 initialization, prefixedInitialization, required, packed, packable); | |
302 } | 339 } |
303 | 340 |
304 // camelCase field name. | 341 // camelCase field name. |
305 String get externalFieldName { | 342 String get externalFieldName { |
306 String name = titlecaseFieldName; | 343 String name = titlecaseFieldName; |
307 return '${name[0].toLowerCase()}${name.substring(1)}'; | 344 return '${name[0].toLowerCase()}${name.substring(1)}'; |
308 } | 345 } |
309 | 346 |
310 // TitleCase field name. | 347 // TitleCase field name. |
311 String get titlecaseFieldName { | 348 String get titlecaseFieldName { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
348 return 2; // Length-delimited | 385 return 2; // Length-delimited |
349 case FieldDescriptorProto_Type.TYPE_GROUP: | 386 case FieldDescriptorProto_Type.TYPE_GROUP: |
350 return 3; // Start group | 387 return 3; // Start group |
351 case FieldDescriptorProto_Type.TYPE_FLOAT: | 388 case FieldDescriptorProto_Type.TYPE_FLOAT: |
352 case FieldDescriptorProto_Type.TYPE_FIXED32: | 389 case FieldDescriptorProto_Type.TYPE_FIXED32: |
353 case FieldDescriptorProto_Type.TYPE_SFIXED32: | 390 case FieldDescriptorProto_Type.TYPE_SFIXED32: |
354 return 5; // 32-bit | 391 return 5; // 32-bit |
355 } | 392 } |
356 } | 393 } |
357 } | 394 } |
OLD | NEW |