Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Merge changes from internal repository. (#98)
Browse files Browse the repository at this point in the history
* Add fast getters for common types.
* Only pass index instead of tag and index in generated code.
* Fix uses-dynamic-as-bottom error in generated gRPC code.
  • Loading branch information
jakobr-google authored Jan 12, 2018
1 parent dd76390 commit 2afec1a
Show file tree
Hide file tree
Showing 11 changed files with 519 additions and 462 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.7.9 - 2018-01-12

* Add fast getters for common types.
* Only pass index instead of tag and index in generated code.
* Fix uses-dynamic-as-bottom error in generated gRPC code.

## 0.7.8 - 2017-10-25

* Added enumValues to FieldInfo.
Expand Down
31 changes: 17 additions & 14 deletions lib/grpc_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ class GrpcServiceGenerator {
// TODO(jakobr): Throw more actionable error.
throw 'FAILURE: Unknown type reference (${fqname}) for ${location}';
}
if (fileGen.package == mg.fileGen.package || mg.fileGen.package == "") {
if (fileGen.package == mg.fileGen.package || mg.fileGen.package == '') {
// It's either the same file, or another file with the same package.
// (In the second case, we import it without using "as".)
return mg.classname;
}
return mg.packageImportPrefix + "." + mg.classname;
return mg.packageImportPrefix + '.' + mg.classname;
}

void generate(IndentingWriter out) {
Expand Down Expand Up @@ -182,8 +182,8 @@ class _GrpcMethod {
final clientStreaming = method.clientStreaming;
final serverStreaming = method.serverStreaming;

service._addDependency(ctx, method.inputType, "input type of $grpcName");
service._addDependency(ctx, method.outputType, "output type of $grpcName");
service._addDependency(ctx, method.inputType, 'input type of $grpcName');
service._addDependency(ctx, method.outputType, 'output type of $grpcName');

final requestType = service._getDartClassName(method.inputType);
final responseType = service._getDartClassName(method.outputType);
Expand Down Expand Up @@ -211,9 +211,10 @@ class _GrpcMethod {
void generateClientMethodDescriptor(IndentingWriter out) {
out.println(
'static final _\$$_dartName = new ClientMethod<$_requestType, $_responseType>(');
out.println('\'/$_serviceName/$_grpcName\',');
out.println('($_requestType value) => value.writeToBuffer(),');
out.println('(List<int> value) => new $_responseType.fromBuffer(value));');
out.println(' \'/$_serviceName/$_grpcName\',');
out.println(' ($_requestType value) => value.writeToBuffer(),');
out.println(
' (List<int> value) => new $_responseType.fromBuffer(value));');
}

void generateClientStub(IndentingWriter out) {
Expand All @@ -234,13 +235,15 @@ class _GrpcMethod {
}

void generateServiceMethodRegistration(IndentingWriter out) {
out.println('\$addMethod(new ServiceMethod(');
out.println('\'$_grpcName\',');
out.println('$_dartName${_clientStreaming ? '' : '_Pre'},');
out.println('$_clientStreaming,');
out.println('$_serverStreaming,');
out.println('(List<int> value) => new $_requestType.fromBuffer(value),');
out.println('($_responseType value) => value.writeToBuffer()));');
out.println(
'\$addMethod(new ServiceMethod<$_requestType, $_responseType>(');
out.println(' \'$_grpcName\',');
out.println(' $_dartName${_clientStreaming ? '' : '_Pre'},');
out.println(' $_clientStreaming,');
out.println(' $_serverStreaming,');
out.println(
' (List<int> value) => new $_requestType.fromBuffer(value),');
out.println(' ($_responseType value) => value.writeToBuffer()));');
}

void generateServiceMethodPreamble(IndentingWriter out) {
Expand Down
98 changes: 58 additions & 40 deletions lib/message_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,50 +310,68 @@ class MessageGenerator extends ProtobufContainer {
void generateFieldsAccessorsMutators(IndentingWriter out) {
for (ProtobufField field in _fieldList) {
out.println();
generateFieldAccessorsMutators(field, out);
}
}

var fieldTypeString = field.getDartType(package);
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.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.';
}
void generateFieldAccessorsMutators(
ProtobufField field, IndentingWriter out) {
var fieldTypeString = field.getDartType(package);
var defaultExpr = field.getDefaultExpr();
var names = field.memberNames;

_emitOverrideIf(field.overridesGetter, out);
var getterExpr =
_getterExpression(fieldTypeString, field.index, defaultExpr);
out.println('${fieldTypeString} get ${names.fieldName} => ${getterExpr};');

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('set ${names.fieldName}'
'($fieldTypeString v) { '
'$fastSetter(${field.index}, v);'
' }');
} else {
var fastSetter = field.baseType.setter;
_emitOverrideIf(field.overridesSetter, out);
if (fastSetter != null) {
out.println('set ${names.fieldName}'
'($fieldTypeString v) { '
'$fastSetter(${field.index}, ${field.number}, v);'
' }');
} else {
out.println('set ${names.fieldName}'
'($fieldTypeString v) { '
'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});');
out.println('set ${names.fieldName}'
'($fieldTypeString v) { '
'setField(${field.number}, v);'
' }');
}
_emitOverrideIf(field.overridesHasMethod, out);
out.println('bool ${names.hasMethodName}() =>'
' \$_has(${field.index});');
_emitOverrideIf(field.overridesClearMethod, out);
out.println('void ${names.clearMethodName}() =>'
' clearField(${field.number});');
}
}

String _getterExpression(String fieldType, int index, String defaultExpr) {
if (fieldType == 'String') {
return '\$_getS($index, $defaultExpr)';
}
if (fieldType == 'Int64' && defaultExpr == 'null') {
return '\$_getI64($index)';
}
if (defaultExpr == 'null') {
return '\$_getN($index)';
}
return '\$_get($index, $defaultExpr)';
}

void _emitOverrideIf(bool condition, IndentingWriter out) {
Expand Down
28 changes: 27 additions & 1 deletion lib/protobuf_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class ProtobufField {
} else if (baseType.isEnum) {
return '..pp<$type>($number, $quotedName, $typeConstant,'
' $type.$checkItem, null, $type.valueOf, $type.values)';
} else if (typeConstant == 'PbFieldType.PS') {
return '..pPS($number, $quotedName)';
} else {
return '..p<$type>($number, $quotedName, $typeConstant)';
}
Expand All @@ -124,7 +126,31 @@ class ProtobufField {
}

String prefix = '..a<$type>($number, $quotedName, $typeConstant';
if (makeDefault == null) return prefix + ')';
if (makeDefault == null) {
switch (type) {
case 'String':
if (typeConstant == 'PbFieldType.OS') {
return '..aOS($number, $quotedName)';
} else if (typeConstant == 'PbFieldType.QS') {
return '..aQS($number, $quotedName)';
}
break;
case 'bool':
if (typeConstant == 'PbFieldType.OB') {
return '..aOB($number, $quotedName)';
}
break;
default:
break;
}
return prefix + ')';
}

if (makeDefault == 'Int64.ZERO' &&
type == 'Int64' &&
typeConstant == 'PbFieldType.O6') {
return '..aInt64($number, $quotedName)';
}

if (baseType.isMessage || baseType.isGroup) {
return prefix + ', $makeDefault, $type.create)';
Expand Down
26 changes: 13 additions & 13 deletions lib/src/dart_options.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import 'package:protobuf/protobuf.dart';

class DartMixin extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('DartMixin')
..a<String>(1, 'name', PbFieldType.OS)
..a<String>(2, 'importFrom', PbFieldType.OS)
..a<String>(3, 'parent', PbFieldType.OS)
..aOS(1, 'name')
..aOS(2, 'importFrom')
..aOS(3, 'parent')
..hasRequiredFields = false;

DartMixin() : super();
Expand All @@ -36,28 +36,28 @@ class DartMixin extends GeneratedMessage {
if (v is! DartMixin) checkItemFailed(v, 'DartMixin');
}

String get name => $_get(0, 1, '');
String get name => $_getS(0, '');
set name(String v) {
$_setString(0, 1, v);
$_setString(0, v);
}

bool hasName() => $_has(0, 1);
bool hasName() => $_has(0);
void clearName() => clearField(1);

String get importFrom => $_get(1, 2, '');
String get importFrom => $_getS(1, '');
set importFrom(String v) {
$_setString(1, 2, v);
$_setString(1, v);
}

bool hasImportFrom() => $_has(1, 2);
bool hasImportFrom() => $_has(1);
void clearImportFrom() => clearField(2);

String get parent => $_get(2, 3, '');
String get parent => $_getS(2, '');
set parent(String v) {
$_setString(2, 3, v);
$_setString(2, v);
}

bool hasParent() => $_has(2, 3);
bool hasParent() => $_has(2);
void clearParent() => clearField(3);
}

Expand Down Expand Up @@ -89,7 +89,7 @@ class Imports extends GeneratedMessage {
if (v is! Imports) checkItemFailed(v, 'Imports');
}

List<DartMixin> get mixins => $_get(0, 1, null);
List<DartMixin> get mixins => $_getN(0);
}

class _ReadonlyImports extends Imports with ReadonlyMessageMixin {}
Expand Down
Loading

0 comments on commit 2afec1a

Please sign in to comment.