Chromium Code Reviews| Index: lib/service_generator.dart |
| diff --git a/lib/service_generator.dart b/lib/service_generator.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f00bbeda00f09d3d883ab1c3e9eba1cfef50f71c |
| --- /dev/null |
| +++ b/lib/service_generator.dart |
| @@ -0,0 +1,63 @@ |
| +// 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 ServiceGenerator extends ProtobufContainer { |
| + final String classname; |
| + final String fqname; |
| + |
| + final ProtobufContainer _parent; |
| + final GenerationContext _context; |
| + final ServiceDescriptorProto _descriptor; |
| + final List<MethodDescriptorProto> _methodDescriptors; |
| + |
| + ServiceGenerator(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}' |
|
skybrian
2015/06/24 16:16:30
This calculation is a bit messy. Perhaps move it t
|
| + : '${parent.fqname}.${descriptor.name}'), |
| + _methodDescriptors = descriptor.method { |
| + _context.register(this); |
| + } |
| + |
| + String get package => _parent.package; |
| + |
| + String _simpleType(String typename) { |
| + return typename.substring(typename.lastIndexOf('.') + 1); |
| + } |
| + |
| + void generate(IndentingWriter out) { |
| + out.addBlock( |
| + 'abstract class ${classname}ServiceBase extends GeneratedService {', |
| + '}', () { |
| + for (MethodDescriptorProto m in _methodDescriptors) { |
| + out.println('Future<${_simpleType(m.outputType)}> ${m.name}(' |
| + 'ServerContext ctx, ${_simpleType(m.inputType)} request);'); |
| + } |
| + out.println(); |
| + out.addBlock( |
| + '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
|
| + 'List<int> requestBytes) async {', '}', () { |
| + for (MethodDescriptorProto m in _methodDescriptors) { |
| + 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
|
| + out.println('var request = new ${_simpleType(m.inputType)}' |
| + '.fromBuffer(requestBytes);'); |
| + out.println('var result = await ${m.name}(ctx, request);'); |
| + out.println('return result.writeToBuffer();'); |
| + }, endWithNewline: false); |
| + } |
| + out.addBlock('{', '}', () { |
| + out.println('throw \'Unknown method: \$method\';'); |
|
skybrian
2015/06/24 16:16:30
make this an ArgumentError?
|
| + }); |
| + }); |
| + }); |
| + out.println(); |
| + } |
| +} |