|
|
|
@ -32,24 +32,20 @@ |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#include <cctype> |
|
|
|
|
#include <string> |
|
|
|
|
#include <map> |
|
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
#include "src/compiler/config.h" |
|
|
|
|
#include "src/compiler/ruby_generator.h" |
|
|
|
|
#include "src/compiler/ruby_generator_helpers-inl.h" |
|
|
|
|
#include "src/compiler/ruby_generator_map-inl.h" |
|
|
|
|
#include "src/compiler/ruby_generator_string-inl.h" |
|
|
|
|
#include <google/protobuf/io/printer.h> |
|
|
|
|
#include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
|
|
|
|
#include <google/protobuf/descriptor.pb.h> |
|
|
|
|
#include <google/protobuf/descriptor.h> |
|
|
|
|
|
|
|
|
|
using google::protobuf::FileDescriptor; |
|
|
|
|
using google::protobuf::ServiceDescriptor; |
|
|
|
|
using google::protobuf::MethodDescriptor; |
|
|
|
|
using google::protobuf::io::Printer; |
|
|
|
|
using google::protobuf::io::StringOutputStream; |
|
|
|
|
|
|
|
|
|
using grpc::protobuf::FileDescriptor; |
|
|
|
|
using grpc::protobuf::ServiceDescriptor; |
|
|
|
|
using grpc::protobuf::MethodDescriptor; |
|
|
|
|
using grpc::protobuf::io::Printer; |
|
|
|
|
using grpc::protobuf::io::StringOutputStream; |
|
|
|
|
using std::map; |
|
|
|
|
using std::vector; |
|
|
|
|
|
|
|
|
@ -57,38 +53,38 @@ namespace grpc_ruby_generator { |
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
// Prints out the method using the ruby gRPC DSL.
|
|
|
|
|
void PrintMethod(const MethodDescriptor *method, const std::string &package, |
|
|
|
|
void PrintMethod(const MethodDescriptor *method, const grpc::string &package, |
|
|
|
|
Printer *out) { |
|
|
|
|
std::string input_type = RubyTypeOf(method->input_type()->name(), package); |
|
|
|
|
grpc::string input_type = RubyTypeOf(method->input_type()->name(), package); |
|
|
|
|
if (method->client_streaming()) { |
|
|
|
|
input_type = "stream(" + input_type + ")"; |
|
|
|
|
} |
|
|
|
|
std::string output_type = RubyTypeOf(method->output_type()->name(), package); |
|
|
|
|
grpc::string output_type = RubyTypeOf(method->output_type()->name(), package); |
|
|
|
|
if (method->server_streaming()) { |
|
|
|
|
output_type = "stream(" + output_type + ")"; |
|
|
|
|
} |
|
|
|
|
std::map<std::string, std::string> method_vars = |
|
|
|
|
std::map<grpc::string, grpc::string> method_vars = |
|
|
|
|
ListToDict({"mth.name", method->name(), "input.type", input_type, |
|
|
|
|
"output.type", output_type, }); |
|
|
|
|
out->Print(method_vars, "rpc :$mth.name$, $input.type$, $output.type$\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Prints out the service using the ruby gRPC DSL.
|
|
|
|
|
void PrintService(const ServiceDescriptor *service, const std::string &package, |
|
|
|
|
void PrintService(const ServiceDescriptor *service, const grpc::string &package, |
|
|
|
|
Printer *out) { |
|
|
|
|
if (service->method_count() == 0) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Begin the service module
|
|
|
|
|
std::map<std::string, std::string> module_vars = |
|
|
|
|
std::map<grpc::string, grpc::string> module_vars = |
|
|
|
|
ListToDict({"module.name", CapitalizeFirst(service->name()), }); |
|
|
|
|
out->Print(module_vars, "module $module.name$\n"); |
|
|
|
|
out->Indent(); |
|
|
|
|
|
|
|
|
|
// TODO(temiola): add documentation
|
|
|
|
|
std::string doc = "TODO: add proto service documentation here"; |
|
|
|
|
std::map<std::string, std::string> template_vars = |
|
|
|
|
grpc::string doc = "TODO: add proto service documentation here"; |
|
|
|
|
std::map<grpc::string, grpc::string> template_vars = |
|
|
|
|
ListToDict({"Documentation", doc, }); |
|
|
|
|
out->Print("\n"); |
|
|
|
|
out->Print(template_vars, "# $Documentation$\n"); |
|
|
|
@ -101,7 +97,7 @@ void PrintService(const ServiceDescriptor *service, const std::string &package, |
|
|
|
|
out->Print("\n"); |
|
|
|
|
out->Print("self.marshal_class_method = :encode\n"); |
|
|
|
|
out->Print("self.unmarshal_class_method = :decode\n"); |
|
|
|
|
std::map<std::string, std::string> pkg_vars = |
|
|
|
|
std::map<grpc::string, grpc::string> pkg_vars = |
|
|
|
|
ListToDict({"service.name", service->name(), "pkg.name", package, }); |
|
|
|
|
out->Print(pkg_vars, "self.service_name = '$pkg.name$.$service.name$'\n"); |
|
|
|
|
out->Print("\n"); |
|
|
|
@ -121,8 +117,8 @@ void PrintService(const ServiceDescriptor *service, const std::string &package, |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
std::string GetServices(const FileDescriptor *file) { |
|
|
|
|
std::string output; |
|
|
|
|
grpc::string GetServices(const FileDescriptor *file) { |
|
|
|
|
grpc::string output; |
|
|
|
|
StringOutputStream output_stream(&output); |
|
|
|
|
Printer out(&output_stream, '$'); |
|
|
|
|
|
|
|
|
@ -133,7 +129,7 @@ std::string GetServices(const FileDescriptor *file) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Write out a file header.
|
|
|
|
|
std::map<std::string, std::string> header_comment_vars = ListToDict( |
|
|
|
|
std::map<grpc::string, grpc::string> header_comment_vars = ListToDict( |
|
|
|
|
{"file.name", file->name(), "file.package", file->package(), }); |
|
|
|
|
out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n"); |
|
|
|
|
out.Print(header_comment_vars, |
|
|
|
@ -144,15 +140,15 @@ std::string GetServices(const FileDescriptor *file) { |
|
|
|
|
// Write out require statemment to import the separately generated file
|
|
|
|
|
// that defines the messages used by the service. This is generated by the
|
|
|
|
|
// main ruby plugin.
|
|
|
|
|
std::map<std::string, std::string> dep_vars = |
|
|
|
|
std::map<grpc::string, grpc::string> dep_vars = |
|
|
|
|
ListToDict({"dep.name", MessagesRequireName(file), }); |
|
|
|
|
out.Print(dep_vars, "require '$dep.name$'\n"); |
|
|
|
|
|
|
|
|
|
// Write out services within the modules
|
|
|
|
|
out.Print("\n"); |
|
|
|
|
std::vector<std::string> modules = Split(file->package(), '.'); |
|
|
|
|
std::vector<grpc::string> modules = Split(file->package(), '.'); |
|
|
|
|
for (size_t i = 0; i < modules.size(); ++i) { |
|
|
|
|
std::map<std::string, std::string> module_vars = |
|
|
|
|
std::map<grpc::string, grpc::string> module_vars = |
|
|
|
|
ListToDict({"module.name", CapitalizeFirst(modules[i]), }); |
|
|
|
|
out.Print(module_vars, "module $module.name$\n"); |
|
|
|
|
out.Indent(); |
|
|
|
|