diff options
Diffstat (limited to 'src/compiler/ruby_generator.cc')
-rw-r--r-- | src/compiler/ruby_generator.cc | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index a0bb92848b..299137519f 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -119,49 +119,52 @@ void PrintService(const ServiceDescriptor *service, const grpc::string &package, grpc::string GetServices(const FileDescriptor *file) { grpc::string output; - StringOutputStream output_stream(&output); - Printer out(&output_stream, '$'); - - // Don't write out any output if there no services, to avoid empty service - // files being generated for proto files that don't declare any. - if (file->service_count() == 0) { - return output; - } - - // Write out a file header. - 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, - "# Source: $file.name$ for package '$file.package$'\n"); - - out.Print("\n"); - out.Print("require 'grpc'\n"); - // 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<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<grpc::string> modules = Split(file->package(), '.'); - for (size_t i = 0; i < modules.size(); ++i) { - std::map<grpc::string, grpc::string> module_vars = - ListToDict({"module.name", CapitalizeFirst(modules[i]), }); - out.Print(module_vars, "module $module.name$\n"); - out.Indent(); + { + // Scope the output stream so it closes and finalizes output to the string. + + StringOutputStream output_stream(&output); + Printer out(&output_stream, '$'); + + // Don't write out any output if there no services, to avoid empty service + // files being generated for proto files that don't declare any. + if (file->service_count() == 0) { + return output; + } + + // Write out a file header. + 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, + "# Source: $file.name$ for package '$file.package$'\n"); + + out.Print("\n"); + out.Print("require 'grpc'\n"); + // 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<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<grpc::string> modules = Split(file->package(), '.'); + for (size_t i = 0; i < modules.size(); ++i) { + std::map<grpc::string, grpc::string> module_vars = + ListToDict({"module.name", CapitalizeFirst(modules[i]), }); + out.Print(module_vars, "module $module.name$\n"); + out.Indent(); + } + for (int i = 0; i < file->service_count(); ++i) { + auto service = file->service(i); + PrintService(service, file->package(), &out); + } + for (size_t i = 0; i < modules.size(); ++i) { + out.Outdent(); + out.Print("end\n"); + } } - for (int i = 0; i < file->service_count(); ++i) { - auto service = file->service(i); - PrintService(service, file->package(), &out); - } - for (size_t i = 0; i < modules.size(); ++i) { - out.Outdent(); - out.Print("end\n"); - } - return output; } |