OLD | NEW |
(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 } |
OLD | NEW |