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

Side by Side Diff: lib/service_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: don't import dart:async if no services Created 5 years, 5 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 ServiceGenerator extends ProtobufContainer {
8 final String classname;
9 final String fqname;
10
11 final ProtobufContainer _parent;
12 final GenerationContext _context;
13 final ServiceDescriptorProto _descriptor;
14 final List<MethodDescriptorProto> _methodDescriptors;
15
16 ServiceGenerator(ServiceDescriptorProto descriptor, ProtobufContainer parent,
17 this._context)
18 : _descriptor = descriptor,
19 _parent = parent,
20 classname = descriptor.name,
21 fqname = (parent == null || parent.fqname == null)
22 ? descriptor.name
23 : (parent.fqname == '.'
24 ? '.${descriptor.name}'
skybrian 2015/06/24 16:16:30 This calculation is a bit messy. Perhaps move it t
25 : '${parent.fqname}.${descriptor.name}'),
26 _methodDescriptors = descriptor.method {
27 _context.register(this);
28 }
29
30 String get package => _parent.package;
31
32 String _simpleType(String typename) {
33 return typename.substring(typename.lastIndexOf('.') + 1);
34 }
35
36 void generate(IndentingWriter out) {
37 out.addBlock(
38 'abstract class ${classname}ServiceBase extends GeneratedService {',
39 '}', () {
40 for (MethodDescriptorProto m in _methodDescriptors) {
41 out.println('Future<${_simpleType(m.outputType)}> ${m.name}('
42 'ServerContext ctx, ${_simpleType(m.inputType)} request);');
43 }
44 out.println();
45 out.addBlock(
46 'Future<List<int>> handleCall(ServerContext ctx, String method, '
skybrian 2015/06/24 16:16:30 We should make this handle JSON and binary on the
47 'List<int> requestBytes) async {', '}', () {
48 for (MethodDescriptorProto m in _methodDescriptors) {
49 out.addBlock('if (method == \'${m.name}\') {', '} else ', () {
skybrian 2015/06/24 16:16:30 Not a big deal, but it seems like a switch stateme
50 out.println('var request = new ${_simpleType(m.inputType)}'
51 '.fromBuffer(requestBytes);');
52 out.println('var result = await ${m.name}(ctx, request);');
53 out.println('return result.writeToBuffer();');
54 }, endWithNewline: false);
55 }
56 out.addBlock('{', '}', () {
57 out.println('throw \'Unknown method: \$method\';');
skybrian 2015/06/24 16:16:30 make this an ArgumentError?
58 });
59 });
60 });
61 out.println();
62 }
63 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698