| Index: lib/service_generator.dart
|
| diff --git a/lib/service_generator.dart b/lib/service_generator.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..91272826ae4b701165b15a54fb80d4c8a009152e
|
| --- /dev/null
|
| +++ b/lib/service_generator.dart
|
| @@ -0,0 +1,65 @@
|
| +// 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 = (parent.classname == '')
|
| + ? descriptor.name
|
| + : '${parent.classname}_${descriptor.name}',
|
| + fqname = (parent == null || parent.fqname == null)
|
| + ? descriptor.name
|
| + : (parent.fqname == '.'
|
| + ? '.${descriptor.name}'
|
| + : '${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}('
|
| + '${_simpleType(m.inputType)} request);');
|
| + }
|
| + out.println();
|
| + out.addBlock(
|
| + 'Future<List<int>> handleCall(String method, List<int> requestBytes) '
|
| + 'async {', '}', () {
|
| + for (MethodDescriptorProto m in _methodDescriptors) {
|
| + out.addBlock('if (method == \'${m.name}\') {', '} else ', () {
|
| + out.println('var request = new ${_simpleType(m.inputType)}'
|
| + '.fromBuffer(requestBytes);');
|
| + out.println('var result = await ${m.name}(request);');
|
| + out.println('return result.writeToBuffer();');
|
| + }, endWithNewline: false);
|
| + }
|
| + out.addBlock('{', '}', () {
|
| + out.println('throw \'Unknown method: \$method\';');
|
| + });
|
| + });
|
| + });
|
| + out.println();
|
| + }
|
| +}
|
|
|