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

Unified Diff: lib/protobuf_field.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: Created 7 years 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 side-by-side diff with in-line comments
Download patch
Index: lib/protobuf_field.dart
diff --git a/lib/protobuf_field.dart b/lib/protobuf_field.dart
index 2a2b8c56ef660133f54886ff555ca425d1f1f001..bb2f6cc49d21b7001f61085fc271042eba2a9b79 100644
--- a/lib/protobuf_field.dart
+++ b/lib/protobuf_field.dart
@@ -19,11 +19,15 @@ class ProtobufField {
final ProtobufContainer parent;
final GenerationContext context;
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
+ final String typePackage;
final String baseType;
final String typeString;
+ 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
+ final String prefixedTypeString;
final String codedStreamType;
final bool repeats;
final String initialization;
+ final String prefixedInitialization;
final bool required;
// True if the field is to be encoded with [packed=true] encoding.
final bool packed;
@@ -49,6 +53,13 @@ class ProtobufField {
FieldOptions get options => _field.options;
String get typeName => _field.typeName;
+ String baseTypeForPackage(String package) =>
+ package == typePackage ? baseType : prefixedBaseType;
+ String typeStringForPackage(String package) =>
+ package == typePackage ? typeString : prefixedTypeString;
+ String initializationForPackage(String package) =>
+ package == typePackage ? initialization : prefixedInitialization;
+
String get shortTypeName {
String prefix;
if (required) {
@@ -86,8 +97,10 @@ class ProtobufField {
}
ProtobufField._(
- field, parent, this.context, this.baseType, this.typeString,
- this.codedStreamType, this.repeats, this.initialization, this.required,
+ field, parent, this.context, this.typePackage, this.baseType,
+ this.typeString, this.prefixedBaseType, this.prefixedTypeString,
+ this.codedStreamType, this.repeats,
+ this.initialization, this.prefixedInitialization, this.required,
this.packed, this.packable) :
this._field = field,
this.parent = parent,
@@ -109,11 +122,15 @@ class ProtobufField {
write = (String typeString) => typeString;
}
+ String typePackage = '';
String baseType;
String typeString;
+ String prefixedBaseType;
+ String prefixedTypeString;
bool packable = false;
String codedStreamType;
String initialization;
+ String prefixedInitialization;
switch (field.type) {
case FieldDescriptorProto_Type.TYPE_BOOL:
baseType = 'bool';
@@ -247,39 +264,55 @@ class ProtobufField {
case FieldDescriptorProto_Type.TYPE_GROUP:
ProtobufContainer groupType = context[field.typeName];
if (groupType != null) {
+ typePackage = groupType.package;
baseType = groupType.classname;
typeString = write(groupType.classname);
+ prefixedBaseType = groupType.package + '.' + baseType;
+ prefixedTypeString = write(prefixedBaseType);
codedStreamType = 'Group';
} else {
throw 'FAILURE: Unknown group type reference ${field.typeName}';
}
initialization = '()${SP}=>${SP}new ${baseType}()';
+ prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()';
break;
case FieldDescriptorProto_Type.TYPE_MESSAGE:
ProtobufContainer messageType = context[field.typeName];
if (messageType != null) {
+ typePackage = messageType.package;
baseType = messageType.classname;
- typeString = write(messageType.classname);
+ typeString = write(baseType);
+ prefixedBaseType = messageType.package + '.' + baseType;
+ prefixedTypeString = write(prefixedBaseType);
codedStreamType = 'Message';
} else {
throw 'FAILURE: Unknown message type reference ${field.typeName}';
}
initialization = '()${SP}=>${SP}new ${baseType}()';
+ prefixedInitialization = '()${SP}=>${SP}new ${prefixedBaseType}()';
break;
case FieldDescriptorProto_Type.TYPE_ENUM:
EnumGenerator enumType = context[field.typeName];
if (enumType != null) {
+ typePackage = enumType.package;
baseType = enumType.classname;
typeString = write(enumType.classname);
codedStreamType = 'Enum';
+ prefixedBaseType = enumType.package + '.' + baseType;
+ prefixedTypeString = write(prefixedBaseType);
packable = true;
if (!repeats) {
if (field.hasDefaultValue() && !field.defaultValue.isEmpty) {
initialization =
- '()${SP}=>${SP}${enumType.classname}.${field.defaultValue}';
+ '()${SP}=>${SP}${baseType}.${field.defaultValue}';
+ prefixedInitialization =
+ '()${SP}=>${SP}${prefixedBaseType}.${field.defaultValue}';
} else if (!enumType._canonicalValues.isEmpty) {
initialization =
- '()${SP}=>${SP}${enumType.classname}.'
+ '()${SP}=>${SP}${baseType}.'
+ '${enumType._canonicalValues[0].name}';
+ prefixedInitialization =
+ '()${SP}=>${SP}${prefixedBaseType}.'
'${enumType._canonicalValues[0].name}';
}
}
@@ -296,9 +329,13 @@ class ProtobufField {
initialization = '()${SP}=>${SP}new PbList()';
}
+ if (prefixedBaseType == null) prefixedBaseType = baseType;
+ if (prefixedTypeString == null) prefixedTypeString = typeString;
+ if (prefixedInitialization == null) prefixedInitialization = initialization;
return new ProtobufField._(
- field, parent, context, baseType, typeString, codedStreamType, repeats,
- initialization, required, packed, packable);
+ field, parent, context, typePackage, baseType, typeString,
+ prefixedBaseType, prefixedTypeString, codedStreamType, repeats,
+ initialization, prefixedInitialization, required, packed, packable);
}
// camelCase field name.

Powered by Google App Engine
This is Rietveld 408576698