Skip to content

Commit

Permalink
Add dart options for override annotations.
Browse files Browse the repository at this point in the history
These make it possible to tag generated getters, setters, clearX()
and hasX() methods with "@OverRide".

[email protected]

Review URL: https://chromiumcodereview.appspot.com//2150323002 .
  • Loading branch information
Brian Slesinsky committed Jul 19, 2016
1 parent a047509 commit 18ab9c7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
25 changes: 24 additions & 1 deletion lib/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,26 @@ class MessageGenerator extends ProtobufContainer {
var defaultExpr = field.getDefaultExpr();
var names = field.memberNames;

_emitOverrideIf(field.overridesGetter, out);
out.println('${fieldTypeString} get ${names.fieldName}'
' => \$_get('
'${field.index}, ${field.number}, $defaultExpr);');
if (!field.isRepeated) {
if (field.isRepeated) {
if (field.overridesSetter) {
throw 'Field ${field.fqname} cannot override a setter for '
'${names.fieldName} because it is repeated.';
}
if (field.overridesHasMethod) {
throw 'Field ${field.fqname} cannot override '
'${names.hasMethodName}() because it is repeated.';
}
if (field.overridesClearMethod) {
throw 'Field ${field.fqname} cannot override '
'${names.clearMethodName}() because it is repeated.';
}
} else {
var fastSetter = field.baseType.setter;
_emitOverrideIf(field.overridesSetter, out);
if (fastSetter != null) {
out.println('void set ${names.fieldName}'
'($fieldTypeString v) { '
Expand All @@ -327,14 +342,22 @@ class MessageGenerator extends ProtobufContainer {
'setField(${field.number}, v);'
' }');
}
_emitOverrideIf(field.overridesHasMethod, out);
out.println('bool ${names.hasMethodName}() =>'
' \$_has(${field.index}, ${field.number});');
_emitOverrideIf(field.overridesClearMethod, out);
out.println('void ${names.clearMethodName}() =>'
' clearField(${field.number});');
}
}
}

void _emitOverrideIf(bool condition, IndentingWriter out) {
if (condition) {
out.println('@override');
}
}

void generateEnums(IndentingWriter out) {
for (EnumGenerator e in _enumGenerators) {
e.generate(out);
Expand Down
17 changes: 17 additions & 0 deletions lib/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class ProtobufField {
bool get isPacked =>
isRepeated && descriptor.options != null && descriptor.options.packed;

/// Whether the field has the `overrideGetter` annotation set to true.
bool get overridesGetter => _hasBooleanOption(Dart_options.overrideGetter);

/// Whether the field has the `overrideSetter` annotation set to true.
bool get overridesSetter => _hasBooleanOption(Dart_options.overrideSetter);

/// Whether the field has the `overrideHasMethod` annotation set to true.
bool get overridesHasMethod =>
_hasBooleanOption(Dart_options.overrideHasMethod);

/// Whether the field has the `overrideClearMethod` annotation set to true.
bool get overridesClearMethod =>
_hasBooleanOption(Dart_options.overrideClearMethod);

/// True if this field uses the Int64 from the fixnum package.
bool get needsFixnumImport => baseType.unprefixed == "Int64";

Expand Down Expand Up @@ -243,6 +257,9 @@ class ProtobufField {
return noDefault;
}

bool _hasBooleanOption(Extension extension) =>
descriptor?.options?.getExtension(extension) ?? false;

get _invalidDefaultValue => "dart-protoc-plugin:"
" invalid default value (${descriptor.defaultValue})"
" found in field $fqname";
Expand Down
8 changes: 8 additions & 0 deletions lib/src/dart_options.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@ class Dart_options {
static final Extension imports = new Extension<Imports>('FileOptions', 'imports', 28125061, PbFieldType.OM, Imports.getDefault, Imports.create);
static final Extension defaultMixin = new Extension<String>('FileOptions', 'defaultMixin', 96128839, PbFieldType.OS);
static final Extension mixin = new Extension<String>('MessageOptions', 'mixin', 96128839, PbFieldType.OS);
static final Extension overrideGetter = new Extension<bool>('FieldOptions', 'overrideGetter', 28205290, PbFieldType.OB);
static final Extension overrideSetter = new Extension<bool>('FieldOptions', 'overrideSetter', 28937366, PbFieldType.OB);
static final Extension overrideHasMethod = new Extension<bool>('FieldOptions', 'overrideHasMethod', 28937461, PbFieldType.OB);
static final Extension overrideClearMethod = new Extension<bool>('FieldOptions', 'overrideClearMethod', 28907907, PbFieldType.OB);
static final Extension dartName = new Extension<String>('FieldOptions', 'dartName', 28700919, PbFieldType.OS);
static void registerAllExtensions(ExtensionRegistry registry) {
registry.add(imports);
registry.add(defaultMixin);
registry.add(mixin);
registry.add(overrideGetter);
registry.add(overrideSetter);
registry.add(overrideHasMethod);
registry.add(overrideClearMethod);
registry.add(dartName);
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/protos/dart_options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ extend google.protobuf.MessageOptions {
}

extend google.protobuf.FieldOptions {
// Adds @override annotation to the field's getter (for use with mixins).
optional bool override_getter = 28205290;

// Adds @override annotation to the field's setter (for use with mixins).
optional bool override_setter = 28937366;

// Adds @override annotation to the field's hasX() method (for use with
// mixins).
optional bool override_has_method = 28937461;

// Adds @override annotation to the field's clearX() method (for use with
// mixins).
optional bool override_clear_method = 28907907;

// Uses the given name for getters, setters and as suffixes for has/clear
// methods in the generated Dart file. Should be lowerCamelCase.
Expand Down
20 changes: 15 additions & 5 deletions test/protos/mixins.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ option (dart_options.imports) = {
import_from: "package:protoc_plugin/testing/mixins.dart"
},
{
name: "Mixin2"
import_from: "package:protoc_plugin/testing/mixins.dart"
name: "Mixin2"
import_from: "package:protoc_plugin/testing/mixins.dart"
},
{
name: "Mixin3"
Expand All @@ -25,17 +25,27 @@ message NoMixinPB {
}

message Mixin1PB {
optional string interface_string = 1;
optional string interface_string = 1 [
(dart_options.override_getter) = true,
(dart_options.override_setter) = true,
(dart_options.override_has_method) = true
];
}

message Mixin2PB {
option (dart_options.mixin) = "Mixin2";

optional string overridden_has_method = 3;
optional string overridden_has_method = 3 [
(dart_options.override_has_method) = true
];
}

message Mixin3PB {
option (dart_options.mixin) = "Mixin3";

optional string interface_string = 1;
optional string interface_string = 1 [
(dart_options.override_getter) = true,
(dart_options.override_setter) = true,
(dart_options.override_has_method) = true
];
}

0 comments on commit 18ab9c7

Please sign in to comment.