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

Commit 2afec1a

Browse files
Merge changes from internal repository. (#98)
* 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.
1 parent dd76390 commit 2afec1a

11 files changed

+519
-462
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.7.9 - 2018-01-12
2+
3+
* Add fast getters for common types.
4+
* Only pass index instead of tag and index in generated code.
5+
* Fix uses-dynamic-as-bottom error in generated gRPC code.
6+
17
## 0.7.8 - 2017-10-25
28

39
* Added enumValues to FieldInfo.

lib/grpc_generator.dart

+17-14
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ class GrpcServiceGenerator {
9696
// TODO(jakobr): Throw more actionable error.
9797
throw 'FAILURE: Unknown type reference (${fqname}) for ${location}';
9898
}
99-
if (fileGen.package == mg.fileGen.package || mg.fileGen.package == "") {
99+
if (fileGen.package == mg.fileGen.package || mg.fileGen.package == '') {
100100
// It's either the same file, or another file with the same package.
101101
// (In the second case, we import it without using "as".)
102102
return mg.classname;
103103
}
104-
return mg.packageImportPrefix + "." + mg.classname;
104+
return mg.packageImportPrefix + '.' + mg.classname;
105105
}
106106

107107
void generate(IndentingWriter out) {
@@ -182,8 +182,8 @@ class _GrpcMethod {
182182
final clientStreaming = method.clientStreaming;
183183
final serverStreaming = method.serverStreaming;
184184

185-
service._addDependency(ctx, method.inputType, "input type of $grpcName");
186-
service._addDependency(ctx, method.outputType, "output type of $grpcName");
185+
service._addDependency(ctx, method.inputType, 'input type of $grpcName');
186+
service._addDependency(ctx, method.outputType, 'output type of $grpcName');
187187

188188
final requestType = service._getDartClassName(method.inputType);
189189
final responseType = service._getDartClassName(method.outputType);
@@ -211,9 +211,10 @@ class _GrpcMethod {
211211
void generateClientMethodDescriptor(IndentingWriter out) {
212212
out.println(
213213
'static final _\$$_dartName = new ClientMethod<$_requestType, $_responseType>(');
214-
out.println('\'/$_serviceName/$_grpcName\',');
215-
out.println('($_requestType value) => value.writeToBuffer(),');
216-
out.println('(List<int> value) => new $_responseType.fromBuffer(value));');
214+
out.println(' \'/$_serviceName/$_grpcName\',');
215+
out.println(' ($_requestType value) => value.writeToBuffer(),');
216+
out.println(
217+
' (List<int> value) => new $_responseType.fromBuffer(value));');
217218
}
218219

219220
void generateClientStub(IndentingWriter out) {
@@ -234,13 +235,15 @@ class _GrpcMethod {
234235
}
235236

236237
void generateServiceMethodRegistration(IndentingWriter out) {
237-
out.println('\$addMethod(new ServiceMethod(');
238-
out.println('\'$_grpcName\',');
239-
out.println('$_dartName${_clientStreaming ? '' : '_Pre'},');
240-
out.println('$_clientStreaming,');
241-
out.println('$_serverStreaming,');
242-
out.println('(List<int> value) => new $_requestType.fromBuffer(value),');
243-
out.println('($_responseType value) => value.writeToBuffer()));');
238+
out.println(
239+
'\$addMethod(new ServiceMethod<$_requestType, $_responseType>(');
240+
out.println(' \'$_grpcName\',');
241+
out.println(' $_dartName${_clientStreaming ? '' : '_Pre'},');
242+
out.println(' $_clientStreaming,');
243+
out.println(' $_serverStreaming,');
244+
out.println(
245+
' (List<int> value) => new $_requestType.fromBuffer(value),');
246+
out.println(' ($_responseType value) => value.writeToBuffer()));');
244247
}
245248

246249
void generateServiceMethodPreamble(IndentingWriter out) {

lib/message_generator.dart

+58-40
Original file line numberDiff line numberDiff line change
@@ -310,50 +310,68 @@ class MessageGenerator extends ProtobufContainer {
310310
void generateFieldsAccessorsMutators(IndentingWriter out) {
311311
for (ProtobufField field in _fieldList) {
312312
out.println();
313+
generateFieldAccessorsMutators(field, out);
314+
}
315+
}
313316

314-
var fieldTypeString = field.getDartType(package);
315-
var defaultExpr = field.getDefaultExpr();
316-
var names = field.memberNames;
317-
318-
_emitOverrideIf(field.overridesGetter, out);
319-
out.println('${fieldTypeString} get ${names.fieldName}'
320-
' => \$_get('
321-
'${field.index}, ${field.number}, $defaultExpr);');
322-
if (field.isRepeated) {
323-
if (field.overridesSetter) {
324-
throw 'Field ${field.fqname} cannot override a setter for '
325-
'${names.fieldName} because it is repeated.';
326-
}
327-
if (field.overridesHasMethod) {
328-
throw 'Field ${field.fqname} cannot override '
329-
'${names.hasMethodName}() because it is repeated.';
330-
}
331-
if (field.overridesClearMethod) {
332-
throw 'Field ${field.fqname} cannot override '
333-
'${names.clearMethodName}() because it is repeated.';
334-
}
317+
void generateFieldAccessorsMutators(
318+
ProtobufField field, IndentingWriter out) {
319+
var fieldTypeString = field.getDartType(package);
320+
var defaultExpr = field.getDefaultExpr();
321+
var names = field.memberNames;
322+
323+
_emitOverrideIf(field.overridesGetter, out);
324+
var getterExpr =
325+
_getterExpression(fieldTypeString, field.index, defaultExpr);
326+
out.println('${fieldTypeString} get ${names.fieldName} => ${getterExpr};');
327+
328+
if (field.isRepeated) {
329+
if (field.overridesSetter) {
330+
throw 'Field ${field.fqname} cannot override a setter for '
331+
'${names.fieldName} because it is repeated.';
332+
}
333+
if (field.overridesHasMethod) {
334+
throw 'Field ${field.fqname} cannot override '
335+
'${names.hasMethodName}() because it is repeated.';
336+
}
337+
if (field.overridesClearMethod) {
338+
throw 'Field ${field.fqname} cannot override '
339+
'${names.clearMethodName}() because it is repeated.';
340+
}
341+
} else {
342+
var fastSetter = field.baseType.setter;
343+
_emitOverrideIf(field.overridesSetter, out);
344+
if (fastSetter != null) {
345+
out.println('set ${names.fieldName}'
346+
'($fieldTypeString v) { '
347+
'$fastSetter(${field.index}, v);'
348+
' }');
335349
} else {
336-
var fastSetter = field.baseType.setter;
337-
_emitOverrideIf(field.overridesSetter, out);
338-
if (fastSetter != null) {
339-
out.println('set ${names.fieldName}'
340-
'($fieldTypeString v) { '
341-
'$fastSetter(${field.index}, ${field.number}, v);'
342-
' }');
343-
} else {
344-
out.println('set ${names.fieldName}'
345-
'($fieldTypeString v) { '
346-
'setField(${field.number}, v);'
347-
' }');
348-
}
349-
_emitOverrideIf(field.overridesHasMethod, out);
350-
out.println('bool ${names.hasMethodName}() =>'
351-
' \$_has(${field.index}, ${field.number});');
352-
_emitOverrideIf(field.overridesClearMethod, out);
353-
out.println('void ${names.clearMethodName}() =>'
354-
' clearField(${field.number});');
350+
out.println('set ${names.fieldName}'
351+
'($fieldTypeString v) { '
352+
'setField(${field.number}, v);'
353+
' }');
355354
}
355+
_emitOverrideIf(field.overridesHasMethod, out);
356+
out.println('bool ${names.hasMethodName}() =>'
357+
' \$_has(${field.index});');
358+
_emitOverrideIf(field.overridesClearMethod, out);
359+
out.println('void ${names.clearMethodName}() =>'
360+
' clearField(${field.number});');
361+
}
362+
}
363+
364+
String _getterExpression(String fieldType, int index, String defaultExpr) {
365+
if (fieldType == 'String') {
366+
return '\$_getS($index, $defaultExpr)';
367+
}
368+
if (fieldType == 'Int64' && defaultExpr == 'null') {
369+
return '\$_getI64($index)';
370+
}
371+
if (defaultExpr == 'null') {
372+
return '\$_getN($index)';
356373
}
374+
return '\$_get($index, $defaultExpr)';
357375
}
358376

359377
void _emitOverrideIf(bool condition, IndentingWriter out) {

lib/protobuf_field.dart

+27-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class ProtobufField {
111111
} else if (baseType.isEnum) {
112112
return '..pp<$type>($number, $quotedName, $typeConstant,'
113113
' $type.$checkItem, null, $type.valueOf, $type.values)';
114+
} else if (typeConstant == 'PbFieldType.PS') {
115+
return '..pPS($number, $quotedName)';
114116
} else {
115117
return '..p<$type>($number, $quotedName, $typeConstant)';
116118
}
@@ -124,7 +126,31 @@ class ProtobufField {
124126
}
125127

126128
String prefix = '..a<$type>($number, $quotedName, $typeConstant';
127-
if (makeDefault == null) return prefix + ')';
129+
if (makeDefault == null) {
130+
switch (type) {
131+
case 'String':
132+
if (typeConstant == 'PbFieldType.OS') {
133+
return '..aOS($number, $quotedName)';
134+
} else if (typeConstant == 'PbFieldType.QS') {
135+
return '..aQS($number, $quotedName)';
136+
}
137+
break;
138+
case 'bool':
139+
if (typeConstant == 'PbFieldType.OB') {
140+
return '..aOB($number, $quotedName)';
141+
}
142+
break;
143+
default:
144+
break;
145+
}
146+
return prefix + ')';
147+
}
148+
149+
if (makeDefault == 'Int64.ZERO' &&
150+
type == 'Int64' &&
151+
typeConstant == 'PbFieldType.O6') {
152+
return '..aInt64($number, $quotedName)';
153+
}
128154

129155
if (baseType.isMessage || baseType.isGroup) {
130156
return prefix + ', $makeDefault, $type.create)';

lib/src/dart_options.pb.dart

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import 'package:protobuf/protobuf.dart';
1111

1212
class DartMixin extends GeneratedMessage {
1313
static final BuilderInfo _i = new BuilderInfo('DartMixin')
14-
..a<String>(1, 'name', PbFieldType.OS)
15-
..a<String>(2, 'importFrom', PbFieldType.OS)
16-
..a<String>(3, 'parent', PbFieldType.OS)
14+
..aOS(1, 'name')
15+
..aOS(2, 'importFrom')
16+
..aOS(3, 'parent')
1717
..hasRequiredFields = false;
1818

1919
DartMixin() : super();
@@ -36,28 +36,28 @@ class DartMixin extends GeneratedMessage {
3636
if (v is! DartMixin) checkItemFailed(v, 'DartMixin');
3737
}
3838

39-
String get name => $_get(0, 1, '');
39+
String get name => $_getS(0, '');
4040
set name(String v) {
41-
$_setString(0, 1, v);
41+
$_setString(0, v);
4242
}
4343

44-
bool hasName() => $_has(0, 1);
44+
bool hasName() => $_has(0);
4545
void clearName() => clearField(1);
4646

47-
String get importFrom => $_get(1, 2, '');
47+
String get importFrom => $_getS(1, '');
4848
set importFrom(String v) {
49-
$_setString(1, 2, v);
49+
$_setString(1, v);
5050
}
5151

52-
bool hasImportFrom() => $_has(1, 2);
52+
bool hasImportFrom() => $_has(1);
5353
void clearImportFrom() => clearField(2);
5454

55-
String get parent => $_get(2, 3, '');
55+
String get parent => $_getS(2, '');
5656
set parent(String v) {
57-
$_setString(2, 3, v);
57+
$_setString(2, v);
5858
}
5959

60-
bool hasParent() => $_has(2, 3);
60+
bool hasParent() => $_has(2);
6161
void clearParent() => clearField(3);
6262
}
6363

@@ -89,7 +89,7 @@ class Imports extends GeneratedMessage {
8989
if (v is! Imports) checkItemFailed(v, 'Imports');
9090
}
9191

92-
List<DartMixin> get mixins => $_get(0, 1, null);
92+
List<DartMixin> get mixins => $_getN(0);
9393
}
9494

9595
class _ReadonlyImports extends Imports with ReadonlyMessageMixin {}

0 commit comments

Comments
 (0)