nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <cctype> |
Nicolas "Pixel" Noble | 36f5323 | 2015-01-16 06:39:58 +0100 | [diff] [blame] | 35 | #include <string> |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 36 | #include <map> |
| 37 | #include <vector> |
| 38 | |
| 39 | #include "src/compiler/ruby_generator.h" |
| 40 | #include "src/compiler/ruby_generator_helpers-inl.h" |
| 41 | #include "src/compiler/ruby_generator_map-inl.h" |
| 42 | #include "src/compiler/ruby_generator_string-inl.h" |
| 43 | #include <google/protobuf/io/printer.h> |
| 44 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
| 45 | #include <google/protobuf/descriptor.pb.h> |
| 46 | #include <google/protobuf/descriptor.h> |
| 47 | |
| 48 | using google::protobuf::FileDescriptor; |
| 49 | using google::protobuf::ServiceDescriptor; |
| 50 | using google::protobuf::MethodDescriptor; |
| 51 | using google::protobuf::io::Printer; |
| 52 | using google::protobuf::io::StringOutputStream; |
| 53 | using std::map; |
| 54 | using std::vector; |
| 55 | |
| 56 | namespace grpc_ruby_generator { |
| 57 | namespace { |
| 58 | |
| 59 | // Prints out the method using the ruby gRPC DSL. |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 60 | void PrintMethod(const MethodDescriptor* method, const std::string& package, |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 61 | Printer* out) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 62 | std::string input_type = RubyTypeOf(method->input_type()->name(), package); |
nnoble | c78b340 | 2014-12-11 16:06:57 -0800 | [diff] [blame] | 63 | if (method->client_streaming()) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 64 | input_type = "stream(" + input_type + ")"; |
| 65 | } |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 66 | std::string output_type = RubyTypeOf(method->output_type()->name(), package); |
nnoble | c78b340 | 2014-12-11 16:06:57 -0800 | [diff] [blame] | 67 | if (method->server_streaming()) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 68 | output_type = "stream(" + output_type + ")"; |
| 69 | } |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 70 | std::map<std::string, std::string> method_vars = ListToDict({ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 71 | "mth.name", method->name(), "input.type", input_type, "output.type", |
| 72 | output_type, |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 73 | }); |
| 74 | out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n"); |
| 75 | } |
| 76 | |
| 77 | // Prints out the service using the ruby gRPC DSL. |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 78 | void PrintService(const ServiceDescriptor* service, const std::string& package, |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 79 | Printer* out) { |
| 80 | if (service->method_count() == 0) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Begin the service module |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 85 | std::map<std::string, std::string> module_vars = ListToDict({ |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 86 | "module.name", CapitalizeFirst(service->name()), |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 87 | }); |
| 88 | out->Print(module_vars, "module $module.name$\n"); |
| 89 | out->Indent(); |
| 90 | |
| 91 | // TODO(temiola): add documentation |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 92 | std::string doc = "TODO: add proto service documentation here"; |
| 93 | std::map<std::string, std::string> template_vars = ListToDict({ |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 94 | "Documentation", doc, |
| 95 | }); |
| 96 | out->Print("\n"); |
| 97 | out->Print(template_vars, "# $Documentation$\n"); |
| 98 | out->Print("class Service\n"); |
| 99 | |
| 100 | // Write the indented class body. |
| 101 | out->Indent(); |
| 102 | out->Print("\n"); |
temiola | 3d726cf | 2015-01-06 18:25:06 -0800 | [diff] [blame] | 103 | out->Print("include GRPC::GenericService\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 104 | out->Print("\n"); |
| 105 | out->Print("self.marshal_class_method = :encode\n"); |
| 106 | out->Print("self.unmarshal_class_method = :decode\n"); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 107 | std::map<std::string, std::string> pkg_vars = ListToDict({ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 108 | "service.name", service->name(), "pkg.name", package, |
temiola | d6fd84a | 2015-01-08 09:59:08 -0800 | [diff] [blame] | 109 | }); |
| 110 | out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 111 | out->Print("\n"); |
| 112 | for (int i = 0; i < service->method_count(); ++i) { |
| 113 | PrintMethod(service->method(i), package, out); |
| 114 | } |
| 115 | out->Outdent(); |
| 116 | |
| 117 | out->Print("end\n"); |
| 118 | out->Print("\n"); |
| 119 | out->Print("Stub = Service.rpc_stub_class\n"); |
| 120 | |
| 121 | // End the service module |
| 122 | out->Outdent(); |
| 123 | out->Print("end\n"); |
| 124 | } |
| 125 | |
| 126 | } // namespace |
| 127 | |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 128 | std::string GetServices(const FileDescriptor* file) { |
| 129 | std::string output; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 130 | StringOutputStream output_stream(&output); |
| 131 | Printer out(&output_stream, '$'); |
| 132 | |
temiola | e5206aa | 2015-01-07 11:43:05 -0800 | [diff] [blame] | 133 | // Don't write out any output if there no services, to avoid empty service |
| 134 | // files being generated for proto files that don't declare any. |
| 135 | if (file->service_count() == 0) { |
| 136 | return output; |
| 137 | } |
| 138 | |
| 139 | // Write out a file header. |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 140 | std::map<std::string, std::string> header_comment_vars = ListToDict({ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 141 | "file.name", file->name(), "file.package", file->package(), |
| 142 | }); |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 143 | out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 144 | out.Print(header_comment_vars, |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 145 | "# Source: $file.name$ for package '$file.package$'\n"); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 146 | |
| 147 | out.Print("\n"); |
| 148 | out.Print("require 'grpc'\n"); |
| 149 | // Write out require statemment to import the separately generated file |
| 150 | // that defines the messages used by the service. This is generated by the |
| 151 | // main ruby plugin. |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 152 | std::map<std::string, std::string> dep_vars = ListToDict({ |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 153 | "dep.name", MessagesRequireName(file), |
| 154 | }); |
| 155 | out.Print(dep_vars, "require '$dep.name$'\n"); |
| 156 | |
| 157 | // Write out services within the modules |
| 158 | out.Print("\n"); |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 159 | std::vector<std::string> modules = Split(file->package(), '.'); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 160 | for (size_t i = 0; i < modules.size(); ++i) { |
Nicolas Noble | f5c5d80 | 2015-01-15 16:36:13 -0800 | [diff] [blame] | 161 | std::map<std::string, std::string> module_vars = ListToDict({ |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 162 | "module.name", CapitalizeFirst(modules[i]), |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 163 | }); |
| 164 | out.Print(module_vars, "module $module.name$\n"); |
| 165 | out.Indent(); |
| 166 | } |
| 167 | for (int i = 0; i < file->service_count(); ++i) { |
| 168 | auto service = file->service(i); |
| 169 | PrintService(service, file->package(), &out); |
| 170 | } |
| 171 | for (size_t i = 0; i < modules.size(); ++i) { |
| 172 | out.Outdent(); |
| 173 | out.Print("end\n"); |
| 174 | } |
| 175 | |
| 176 | return output; |
| 177 | } |
| 178 | |
| 179 | } // namespace grpc_ruby_generator |