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 = descriptor.name, |
| 21 fqname = _qualifiedName(descriptor, parent), |
| 22 _methodDescriptors = descriptor.method { |
| 23 _context.register(this); |
| 24 } |
| 25 |
| 26 static String _qualifiedName(ServiceDescriptorProto descriptor, |
| 27 ProtobufContainer parent) { |
| 28 if (parent == null || parent.fqname == null) { |
| 29 return descriptor.name; |
| 30 } else if (parent.fqname == '.') { |
| 31 return '.${descriptor.name}'; |
| 32 } else { |
| 33 return '${parent.fqname}.${descriptor.name}'; |
| 34 } |
| 35 } |
| 36 |
| 37 static String _serviceClassName(descriptor) { |
| 38 if (descriptor.name.endsWith("Service")) { |
| 39 return descriptor.name + "Base"; // avoid: ServiceServiceBase |
| 40 } else { |
| 41 return descriptor.name + "ServiceBase"; |
| 42 } |
| 43 } |
| 44 |
| 45 String get package => _parent.package; |
| 46 |
| 47 String _shortType(String typename) { |
| 48 return typename.substring(typename.lastIndexOf('.') + 1); |
| 49 } |
| 50 |
| 51 String _methodName(String name) => |
| 52 name.substring(0,1).toLowerCase() + name.substring(1); |
| 53 |
| 54 void generate(IndentingWriter out) { |
| 55 out.addBlock( |
| 56 'abstract class ${_serviceClassName(_descriptor)} extends ' |
| 57 'GeneratedService {', |
| 58 '}', () { |
| 59 for (MethodDescriptorProto m in _methodDescriptors) { |
| 60 var methodName = _methodName(m.name); |
| 61 out.println('Future<${_shortType(m.outputType)}> $methodName(' |
| 62 'ServerContext ctx, ${_shortType(m.inputType)} request);'); |
| 63 } |
| 64 out.println(); |
| 65 |
| 66 out.addBlock( |
| 67 'GeneratedMessage createRequest(String method) {', '}', () { |
| 68 out.addBlock("switch (method) {", "}", () { |
| 69 for (MethodDescriptorProto m in _methodDescriptors) { |
| 70 out.println( |
| 71 "case '${m.name}': return new ${_shortType(m.inputType)}();"); |
| 72 } |
| 73 out.println("default: " |
| 74 "throw new ArgumentError('Unknown method: \$method');"); |
| 75 }); |
| 76 }); |
| 77 out.println(); |
| 78 |
| 79 out.addBlock( |
| 80 'Future<GeneratedMessage> handleCall(ServerContext ctx, ' |
| 81 'String method, GeneratedMessage request) async {', '}', () { |
| 82 out.addBlock("switch (method) {", "}", () { |
| 83 for (MethodDescriptorProto m in _methodDescriptors) { |
| 84 var methodName = _methodName(m.name); |
| 85 out.println( |
| 86 "case '${m.name}': return await $methodName(ctx, request);"); |
| 87 } |
| 88 out.println("default: " |
| 89 "throw new ArgumentError('Unknown method: \$method');"); |
| 90 }); |
| 91 }); |
| 92 }); |
| 93 out.println(); |
| 94 } |
| 95 } |
OLD | NEW |