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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Makefile ('k') | lib/file_generator.dart » ('j') | test/service_test.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/client_generator.dart
diff --git a/lib/client_generator.dart b/lib/client_generator.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bffbcae170e3867ffaaafcfea9cffb4f46cc21ae
--- /dev/null
+++ b/lib/client_generator.dart
@@ -0,0 +1,56 @@
+// 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('RpcClient _client;');
+ out.println('${classname}Api(this._client);');
+ out.println();
+ for (MethodDescriptorProto m in _descriptor.method) {
+ // lowercase first letter in method name.
+ var methodName =
+ m.name.substring(0,1).toLowerCase() + m.name.substring(1);
+ out.addBlock('Future<${_shortType(m.outputType)}> $methodName('
+ 'ClientContext ctx, ${_shortType(m.inputType)} request) '
+ 'async {', '}', () {
+ out.println('var emptyResponse = new ${_shortType(m.outputType)}();');
+ out.println('var result = await _client.invoke(ctx, '
+ '\'${_descriptor.name}\', \'${m.name}\', '
+ 'request, emptyResponse);');
+ out.println('return result;');
+ });
+ }
+ });
+ out.println();
+ }
+}
« 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