Index: lib/client_generator.dart |
diff --git a/lib/client_generator.dart b/lib/client_generator.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3df636ebdc4ff444afac8e01e107a174204e2e1a |
--- /dev/null |
+++ b/lib/client_generator.dart |
@@ -0,0 +1,53 @@ |
+// 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 = 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;'); |
Søren Gjesse
2015/06/24 09:08:02
We could just make this 'var' then the class name
|
+ out.println('${classname}Api(this._client);'); |
+ out.println(); |
+ for (MethodDescriptorProto m in _descriptor.method) { |
+ out.addBlock('Future<${_shortType(m.outputType)}> ${m.name}(' |
+ 'ClientContext ctx, ${_shortType(m.inputType)} request) ' |
+ 'async {', '}', () { |
+ out.println( |
+ 'var resultBytes = await _client.Invoke(ctx, ' |
+ '\'/${_descriptor.name}.${m.name}\', request.writeToBuffer());'); |
skybrian
2015/06/24 16:16:29
It seems like ProtobufClient should have the respo
|
+ out.println('return new ${_shortType(m.outputType)}' |
+ '.fromBuffer(resultBytes);'); |
+ }); |
+ } |
+ }); |
+ out.println(); |
+ } |
+} |