Skip to content

Commit 269e004

Browse files
authored
improve the readability of generated gRPC client files (#1021)
1 parent 48ba649 commit 269e004

File tree

5 files changed

+76
-73
lines changed

5 files changed

+76
-73
lines changed

protoc_plugin/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Ignore `unused_import` diagnostics for `*.pbjson.dart` files.
66
* Revert the change to not generate empty `*.pbenum.dart` files; these can be
77
exported from other enum files.
8+
* Improve the readablity of generated gRPC client files.
89

910
[#1010]: https://github.com/google/protobuf.dart/issues/1010
1011

protoc_plugin/analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include: ../analysis_options.yaml
22

33
analyzer:
44
errors:
5-
# The generated code in lib/src/gen trigger this lint.
5+
# The generated code in lib/src/gen triggers this lint.
66
unintended_html_in_doc_comment: ignore
77
exclude:
88
- test/goldens/**

protoc_plugin/lib/src/grpc_generator.dart

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,25 @@ class GrpcServiceGenerator {
145145
out.println();
146146
}
147147

148-
for (final method in _methods) {
149-
method.generateClientMethodDescriptor(out);
150-
}
151-
out.println();
148+
// generate the constructor
152149
out.println(
153150
'$_clientClassname(super.channel, {super.options, super.interceptors});');
151+
152+
// generate the service call methods
154153
for (var i = 0; i < _methods.length; i++) {
155154
_methods[i].generateClientStub(out, this, i);
156155
}
156+
157+
// generate the method descriptors
158+
out.println();
159+
if (_methods.isNotEmpty) {
160+
out.println(' // method descriptors');
161+
out.println();
162+
163+
for (final method in _methods) {
164+
method.generateClientMethodDescriptor(out);
165+
}
166+
}
157167
});
158168
}
159169

@@ -171,10 +181,12 @@ class GrpcServiceGenerator {
171181
});
172182
out.println();
173183
for (final method in _methods) {
174-
method.generateServiceMethodPreamble(out);
175-
}
176-
for (final method in _methods) {
184+
if (!method._clientStreaming) {
185+
method.generateServiceMethodPreamble(out);
186+
out.println();
187+
}
177188
method.generateServiceMethodStub(out);
189+
out.println();
178190
}
179191
});
180192
}
@@ -259,8 +271,7 @@ class _GrpcMethod {
259271
'static final _\$$_dartName = $_clientMethod<$_requestType, $_responseType>(');
260272
out.println(' \'/$_serviceName/$_grpcName\',');
261273
out.println(' ($_requestType value) => value.writeToBuffer(),');
262-
out.println(
263-
' ($coreImportPrefix.List<$coreImportPrefix.int> value) => $_responseType.fromBuffer(value));');
274+
out.println(' $_responseType.fromBuffer);');
264275
}
265276

266277
List<int> _methodDescriptorPath(GrpcServiceGenerator generator, int index) {
@@ -284,7 +295,7 @@ class _GrpcMethod {
284295
'@$coreImportPrefix.Deprecated(\'This method is deprecated\')');
285296
}
286297
out.addBlock(
287-
'$_clientReturnType $_dartName($_argumentType request, {${GrpcServiceGenerator._callOptions}? options}) {',
298+
'$_clientReturnType $_dartName($_argumentType request, {${GrpcServiceGenerator._callOptions}? options,}) {',
288299
'}', () {
289300
if (_clientStreaming && _serverStreaming) {
290301
out.println(
@@ -314,8 +325,6 @@ class _GrpcMethod {
314325
}
315326

316327
void generateServiceMethodPreamble(IndentingWriter out) {
317-
if (_clientStreaming) return;
318-
319328
out.addBlock(
320329
'$_serverReturnType ${_dartName}_Pre($_serviceCall \$call, $_future<$_requestType> \$request) async${_serverStreaming ? '*' : ''} {',
321330
'}', () {
@@ -325,7 +334,6 @@ class _GrpcMethod {
325334
out.println('return $_dartName(\$call, await \$request);');
326335
}
327336
});
328-
out.println();
329337
}
330338

331339
void generateServiceMethodStub(IndentingWriter out) {

protoc_plugin/test/file_generator_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,10 @@ void main() {
350350

351351
final writer = IndentingWriter(filename: '');
352352
fg.writeMainHeader(writer);
353-
expectGolden(fg.generateGrpcFile(), 'grpc_service.pbgrpc.dart');
353+
// We use a '.~dart' file extension here, insead of '.dart', so that
354+
// 'pub publish' won't try and validate that all the imports for this file
355+
// are listed in the pubspec.
356+
expectGolden(fg.generateGrpcFile(), 'grpc_service.pbgrpc.~dart');
354357
});
355358

356359
test('FileGenerator generates imports for .pb.dart files', () {

protoc_plugin/test/goldens/grpc_service.pbgrpc.dart renamed to protoc_plugin/test/goldens/grpc_service.pbgrpc.~dart

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,66 +31,58 @@ class TestClient extends $grpc.Client {
3131
'https://www.googleapis.com/auth/datastore',
3232
];
3333

34+
TestClient(super.channel, {super.options, super.interceptors});
35+
36+
$grpc.ResponseFuture<$0.Output> unary($0.Input request, {$grpc.CallOptions? options,}) {
37+
return $createUnaryCall(_$unary, request, options: options);
38+
}
39+
40+
$grpc.ResponseFuture<$0.Output> clientStreaming($async.Stream<$0.Input> request, {$grpc.CallOptions? options,}) {
41+
return $createStreamingCall(_$clientStreaming, request, options: options).single;
42+
}
43+
44+
$grpc.ResponseStream<$0.Output> serverStreaming($0.Input request, {$grpc.CallOptions? options,}) {
45+
return $createStreamingCall(_$serverStreaming, $async.Stream.fromIterable([request]), options: options);
46+
}
47+
48+
$grpc.ResponseStream<$0.Output> bidirectional($async.Stream<$0.Input> request, {$grpc.CallOptions? options,}) {
49+
return $createStreamingCall(_$bidirectional, request, options: options);
50+
}
51+
52+
$grpc.ResponseFuture<$0.Output> call($0.Input request, {$grpc.CallOptions? options,}) {
53+
return $createUnaryCall(_$call, request, options: options);
54+
}
55+
56+
$grpc.ResponseFuture<$0.Output> request($0.Input request, {$grpc.CallOptions? options,}) {
57+
return $createUnaryCall(_$request, request, options: options);
58+
}
59+
60+
// method descriptors
61+
3462
static final _$unary = $grpc.ClientMethod<$0.Input, $0.Output>(
3563
'/Test/Unary',
3664
($0.Input value) => value.writeToBuffer(),
37-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
65+
$0.Output.fromBuffer);
3866
static final _$clientStreaming = $grpc.ClientMethod<$0.Input, $0.Output>(
3967
'/Test/ClientStreaming',
4068
($0.Input value) => value.writeToBuffer(),
41-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
69+
$0.Output.fromBuffer);
4270
static final _$serverStreaming = $grpc.ClientMethod<$0.Input, $0.Output>(
4371
'/Test/ServerStreaming',
4472
($0.Input value) => value.writeToBuffer(),
45-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
73+
$0.Output.fromBuffer);
4674
static final _$bidirectional = $grpc.ClientMethod<$0.Input, $0.Output>(
4775
'/Test/Bidirectional',
4876
($0.Input value) => value.writeToBuffer(),
49-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
77+
$0.Output.fromBuffer);
5078
static final _$call = $grpc.ClientMethod<$0.Input, $0.Output>(
5179
'/Test/Call',
5280
($0.Input value) => value.writeToBuffer(),
53-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
81+
$0.Output.fromBuffer);
5482
static final _$request = $grpc.ClientMethod<$0.Input, $0.Output>(
5583
'/Test/Request',
5684
($0.Input value) => value.writeToBuffer(),
57-
($core.List<$core.int> value) => $0.Output.fromBuffer(value));
58-
59-
TestClient(super.channel, {super.options, super.interceptors});
60-
61-
$grpc.ResponseFuture<$0.Output> unary($0.Input request,
62-
{$grpc.CallOptions? options}) {
63-
return $createUnaryCall(_$unary, request, options: options);
64-
}
65-
66-
$grpc.ResponseFuture<$0.Output> clientStreaming(
67-
$async.Stream<$0.Input> request,
68-
{$grpc.CallOptions? options}) {
69-
return $createStreamingCall(_$clientStreaming, request, options: options)
70-
.single;
71-
}
72-
73-
$grpc.ResponseStream<$0.Output> serverStreaming($0.Input request,
74-
{$grpc.CallOptions? options}) {
75-
return $createStreamingCall(
76-
_$serverStreaming, $async.Stream.fromIterable([request]),
77-
options: options);
78-
}
79-
80-
$grpc.ResponseStream<$0.Output> bidirectional($async.Stream<$0.Input> request,
81-
{$grpc.CallOptions? options}) {
82-
return $createStreamingCall(_$bidirectional, request, options: options);
83-
}
84-
85-
$grpc.ResponseFuture<$0.Output> call($0.Input request,
86-
{$grpc.CallOptions? options}) {
87-
return $createUnaryCall(_$call, request, options: options);
88-
}
89-
90-
$grpc.ResponseFuture<$0.Output> request($0.Input request,
91-
{$grpc.CallOptions? options}) {
92-
return $createUnaryCall(_$request, request, options: options);
93-
}
85+
$0.Output.fromBuffer);
9486
}
9587

9688
@$pb.GrpcServiceName('Test')
@@ -142,33 +134,32 @@ abstract class TestServiceBase extends $grpc.Service {
142134
($0.Output value) => value.writeToBuffer()));
143135
}
144136

145-
$async.Future<$0.Output> unary_Pre(
146-
$grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
137+
$async.Future<$0.Output> unary_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
147138
return unary($call, await $request);
148139
}
149140

150-
$async.Stream<$0.Output> serverStreaming_Pre(
151-
$grpc.ServiceCall $call, $async.Future<$0.Input> $request) async* {
141+
$async.Future<$0.Output> unary($grpc.ServiceCall call, $0.Input request);
142+
143+
$async.Future<$0.Output> clientStreaming($grpc.ServiceCall call, $async.Stream<$0.Input> request);
144+
145+
$async.Stream<$0.Output> serverStreaming_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async* {
152146
yield* serverStreaming($call, await $request);
153147
}
154148

155-
$async.Future<$0.Output> call_Pre(
156-
$grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
149+
$async.Stream<$0.Output> serverStreaming($grpc.ServiceCall call, $0.Input request);
150+
151+
$async.Stream<$0.Output> bidirectional($grpc.ServiceCall call, $async.Stream<$0.Input> request);
152+
153+
$async.Future<$0.Output> call_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
157154
return call($call, await $request);
158155
}
159156

160-
$async.Future<$0.Output> request_Pre(
161-
$grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
157+
$async.Future<$0.Output> call($grpc.ServiceCall call, $0.Input request);
158+
159+
$async.Future<$0.Output> request_Pre($grpc.ServiceCall $call, $async.Future<$0.Input> $request) async {
162160
return request($call, await $request);
163161
}
164162

165-
$async.Future<$0.Output> unary($grpc.ServiceCall call, $0.Input request);
166-
$async.Future<$0.Output> clientStreaming(
167-
$grpc.ServiceCall call, $async.Stream<$0.Input> request);
168-
$async.Stream<$0.Output> serverStreaming(
169-
$grpc.ServiceCall call, $0.Input request);
170-
$async.Stream<$0.Output> bidirectional(
171-
$grpc.ServiceCall call, $async.Stream<$0.Input> request);
172-
$async.Future<$0.Output> call($grpc.ServiceCall call, $0.Input request);
173163
$async.Future<$0.Output> request($grpc.ServiceCall call, $0.Input request);
164+
174165
}

0 commit comments

Comments
 (0)