OLD | NEW |
(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 = (parent.classname == '') |
| 21 ? descriptor.name |
| 22 : '${parent.classname}_${descriptor.name}', |
| 23 fqname = (parent == null || parent.fqname == null) |
| 24 ? descriptor.name |
| 25 : (parent.fqname == '.' |
| 26 ? '.${descriptor.name}' |
| 27 : '${parent.fqname}.${descriptor.name}'), |
| 28 _methodDescriptors = descriptor.method { |
| 29 _context.register(this); |
| 30 } |
| 31 |
| 32 String get package => _parent.package; |
| 33 |
| 34 String _simpleType(String typename) { |
| 35 return typename.substring(typename.lastIndexOf('.') + 1); |
| 36 } |
| 37 |
| 38 void generate(IndentingWriter out) { |
| 39 out.addBlock( |
| 40 'abstract class ${classname}ServiceBase extends GeneratedService {', |
| 41 '}', () { |
| 42 for (MethodDescriptorProto m in _methodDescriptors) { |
| 43 out.println('Future<${_simpleType(m.outputType)}> ${m.name}(' |
| 44 '${_simpleType(m.inputType)} request);'); |
| 45 } |
| 46 out.println(); |
| 47 out.addBlock( |
| 48 'Future<List<int>> handleCall(String method, List<int> requestBytes) ' |
| 49 'async {', '}', () { |
| 50 for (MethodDescriptorProto m in _methodDescriptors) { |
| 51 out.addBlock('if (method == \'${m.name}\') {', '} else ', () { |
| 52 out.println('var request = new ${_simpleType(m.inputType)}' |
| 53 '.fromBuffer(requestBytes);'); |
| 54 out.println('var result = await ${m.name}(request);'); |
| 55 out.println('return result.writeToBuffer();'); |
| 56 }, endWithNewline: false); |
| 57 } |
| 58 out.addBlock('{', '}', () { |
| 59 out.println('throw \'Unknown method: \$method\';'); |
| 60 }); |
| 61 }); |
| 62 }); |
| 63 out.println(); |
| 64 } |
| 65 } |
OLD | NEW |