Chromium Code Reviews| Index: lib/client_generator.dart |
| diff --git a/lib/client_generator.dart b/lib/client_generator.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6bb761d299a762fa5c6f2abe04e47316c01e299a |
| --- /dev/null |
| +++ b/lib/client_generator.dart |
| @@ -0,0 +1,55 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of protoc; |
| + |
| +class ClientApiGenerator extends ProtobufContainer { |
| + final String classname; |
| + final String fqname; |
| + |
| + final ProtobufContainer _parent; |
| + final GenerationContext _context; |
| + final ServiceDescriptorProto _descriptor; |
| + |
| + ClientApiGenerator(ServiceDescriptorProto descriptor, |
| + ProtobufContainer parent, this._context) |
| + : _descriptor = descriptor, |
| + _parent = parent, |
| + classname = (parent.classname == '') |
| + ? descriptor.name |
| + : '${parent.classname}_${descriptor.name}', |
|
Søren Gjesse
2015/06/23 12:08:31
Can services be nested? If so please add a test fo
wibling
2015/06/23 13:26:34
No, I have simplified this to just descriptor.name
|
| + fqname = (parent == null || parent.fqname == null) |
| + ? descriptor.name |
| + : (parent.fqname == '.' |
| + ? '.${descriptor.name}' |
| + : '${parent.fqname}.${descriptor.name}') { |
| + _context.register(this); |
| + } |
| + |
| + String get package => _parent.package; |
| + |
| + String _shortType(String typename) { |
| + return typename.substring(typename.lastIndexOf('.')+1); |
| + } |
| + |
| + void generate(IndentingWriter out) { |
| + out.addBlock('class ${classname}Api {', '}', () { |
| + out.println('ProtobufClient _client;'); |
| + out.println('${classname}Api(this._client);'); |
| + out.println(); |
| + for (MethodDescriptorProto m in _descriptor.method) { |
| + out.addBlock('Future<${_shortType(m.outputType)}> ${m.name}(' |
| + '${_shortType(m.inputType)} request) ' |
| + 'async {', '}', () { |
| + out.println( |
| + 'var resultBytes = await _client.Invoke(\'/${_descriptor.name}.' |
| + '${m.name}\', request.writeToBuffer());'); |
| + out.println('return new ${_shortType(m.outputType)}' |
| + '.fromBuffer(resultBytes);'); |
| + }); |
| + } |
| + }); |
| + out.println(); |
| + } |
| +} |