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

Side by Side Diff: lib/client_generator.dart

Issue 1196293003: Initial support for generating client and server stubs for Dart. (Closed) Base URL: https://github.com/dart-lang/dart-protoc-plugin.git@master
Patch Set: adddressing nits Created 5 years, 6 months 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of protoc;
6
7 class ClientApiGenerator extends ProtobufContainer {
8 final String classname;
9 final String fqname;
10
11 final ProtobufContainer _parent;
12 final GenerationContext _context;
13 final ServiceDescriptorProto _descriptor;
14
15 ClientApiGenerator(ServiceDescriptorProto descriptor,
16 ProtobufContainer parent, this._context)
17 : _descriptor = descriptor,
18 _parent = parent,
19 classname = descriptor.name,
20 fqname = (parent == null || parent.fqname == null)
21 ? descriptor.name
22 : (parent.fqname == '.'
23 ? '.${descriptor.name}'
24 : '${parent.fqname}.${descriptor.name}') {
25 _context.register(this);
26 }
27
28 String get package => _parent.package;
29
30 String _shortType(String typename) {
31 return typename.substring(typename.lastIndexOf('.')+1);
32 }
33
34 void generate(IndentingWriter out) {
35 out.addBlock('class ${classname}Api {', '}', () {
36 out.println('RpcClient _client;');
37 out.println('${classname}Api(this._client);');
38 out.println();
39 for (MethodDescriptorProto m in _descriptor.method) {
40 // lowercase first letter in method name.
41 var methodName =
42 m.name.substring(0,1).toLowerCase() + m.name.substring(1);
43 out.addBlock('Future<${_shortType(m.outputType)}> $methodName('
44 'ClientContext ctx, ${_shortType(m.inputType)} request) '
45 'async {', '}', () {
46 out.println('var emptyResponse = new ${_shortType(m.outputType)}();');
47 out.println('var result = await _client.invoke(ctx, '
48 '\'${_descriptor.name}\', \'${m.name}\', '
49 'request, emptyResponse);');
50 out.println('return result;');
51 });
52 }
53 });
54 out.println();
55 }
56 }
OLDNEW
« no previous file with comments | « Makefile ('k') | lib/file_generator.dart » ('j') | test/service_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698