aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler/node_generator.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/node_generator.cc')
-rw-r--r--src/compiler/node_generator.cc117
1 files changed, 61 insertions, 56 deletions
diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc
index 822622cccf..1fe090d17a 100644
--- a/src/compiler/node_generator.cc
+++ b/src/compiler/node_generator.cc
@@ -74,8 +74,16 @@ grpc::string GetJSMessageFilename(const grpc::string& filename) {
// Given a filename like foo/bar/baz.proto, returns the root directory
// path ../../
-grpc::string GetRootPath(const grpc::string& filename) {
- size_t slashes = std::count(filename.begin(), filename.end(), '/');
+grpc::string GetRootPath(const grpc::string& from_filename,
+ const grpc::string& to_filename) {
+ if (to_filename.find("google/protobuf") == 0) {
+ // Well-known types (.proto files in the google/protobuf directory) are
+ // assumed to come from the 'google-protobuf' npm package. We may want to
+ // generalize this exception later by letting others put generated code in
+ // their own npm packages.
+ return "google-protobuf/";
+ }
+ size_t slashes = std::count(from_filename.begin(), from_filename.end(), '/');
if (slashes == 0) {
return "./";
}
@@ -90,7 +98,7 @@ grpc::string GetRootPath(const grpc::string& filename) {
// from_file, assuming that both paths are relative to the same directory
grpc::string GetRelativePath(const grpc::string& from_file,
const grpc::string& to_file) {
- return GetRootPath(from_file) + to_file;
+ return GetRootPath(from_file, to_file) + to_file;
}
/* Finds all message types used in all services in the file, and returns them
@@ -181,62 +189,67 @@ void PrintMethod(const MethodDescriptor *method, Printer *out) {
// Prints out the service descriptor object
void PrintService(const ServiceDescriptor *service, Printer *out) {
map<grpc::string, grpc::string> template_vars;
+ out->Print(GetNodeComments(service, true).c_str());
template_vars["name"] = service->name();
out->Print(template_vars, "var $name$Service = exports.$name$Service = {\n");
out->Indent();
for (int i = 0; i < service->method_count(); i++) {
grpc::string method_name = grpc_generator::LowercaseFirstLetter(
service->method(i)->name());
+ out->Print(GetNodeComments(service->method(i), true).c_str());
out->Print("$method_name$: ",
"method_name", method_name);
PrintMethod(service->method(i), out);
out->Print(",\n");
+ out->Print(GetNodeComments(service->method(i), false).c_str());
}
out->Outdent();
out->Print("};\n\n");
out->Print(template_vars, "exports.$name$Client = "
"grpc.makeGenericClientConstructor($name$Service);\n");
+ out->Print(GetNodeComments(service, false).c_str());
}
-}
-
-grpc::string GetImports(const FileDescriptor *file) {
- grpc::string output;
- {
- StringOutputStream output_stream(&output);
- Printer out(&output_stream, '$');
-
- if (file->service_count() == 0) {
- return output;
- }
-
- out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
+void PrintImports(const FileDescriptor *file, Printer *out) {
+ out->Print("var grpc = require('grpc');\n");
+ if (file->message_type_count() > 0) {
+ grpc::string file_path = GetRelativePath(file->name(),
+ GetJSMessageFilename(
+ file->name()));
+ out->Print("var $module_alias$ = require('$file_path$');\n",
+ "module_alias", ModuleAlias(file->name()),
+ "file_path", file_path);
+ }
- out.Print("'use strict';\n");
+ for (int i = 0; i < file->dependency_count(); i++) {
+ grpc::string file_path = GetRelativePath(
+ file->name(), GetJSMessageFilename(file->dependency(i)->name()));
+ out->Print("var $module_alias$ = require('$file_path$');\n",
+ "module_alias", ModuleAlias(file->dependency(i)->name()),
+ "file_path", file_path);
+ }
+ out->Print("\n");
+}
- out.Print("var grpc = require('grpc');\n");
- if (file->message_type_count() > 0) {
- grpc::string file_path = GetRelativePath(file->name(),
- GetJSMessageFilename(
- file->name()));
- out.Print("var $module_alias$ = require('$file_path$');\n",
- "module_alias", ModuleAlias(file->name()),
- "file_path", file_path);
- }
+void PrintTransformers(const FileDescriptor *file, Printer *out) {
+ map<grpc::string, const Descriptor*> messages = GetAllMessages(file);
+ for (std::map<grpc::string, const Descriptor*>::iterator it =
+ messages.begin();
+ it != messages.end(); it++) {
+ PrintMessageTransformer(it->second, out);
+ }
+ out->Print("\n");
+}
- for (int i = 0; i < file->dependency_count(); i++) {
- grpc::string file_path = GetRelativePath(
- file->name(), GetJSMessageFilename(file->dependency(i)->name()));
- out.Print("var $module_alias$ = require('$file_path$');\n",
- "module_alias", ModuleAlias(file->dependency(i)->name()),
- "file_path", file_path);
- }
- out.Print("\n");
+void PrintServices(const FileDescriptor *file, Printer *out) {
+ for (int i = 0; i < file->service_count(); i++) {
+ PrintService(file->service(i), out);
}
- return output;
}
-grpc::string GetTransformers(const FileDescriptor *file) {
+}
+
+grpc::string GenerateFile(const FileDescriptor *file) {
grpc::string output;
{
StringOutputStream output_stream(&output);
@@ -245,31 +258,23 @@ grpc::string GetTransformers(const FileDescriptor *file) {
if (file->service_count() == 0) {
return output;
}
+ out.Print("// GENERATED CODE -- DO NOT EDIT!\n\n");
- map<grpc::string, const Descriptor*> messages = GetAllMessages(file);
- for (std::map<grpc::string, const Descriptor*>::iterator it =
- messages.begin();
- it != messages.end(); it++) {
- PrintMessageTransformer(it->second, &out);
+ grpc::string leading_comments = GetNodeComments(file, true);
+ if (!leading_comments.empty()) {
+ out.Print("// Original file comments:\n");
+ out.Print(leading_comments.c_str());
}
- out.Print("\n");
- }
- return output;
-}
-grpc::string GetServices(const FileDescriptor *file) {
- grpc::string output;
- {
- StringOutputStream output_stream(&output);
- Printer out(&output_stream, '$');
+ out.Print("'use strict';\n");
- if (file->service_count() == 0) {
- return output;
- }
+ PrintImports(file, &out);
- for (int i = 0; i < file->service_count(); i++) {
- PrintService(file->service(i), &out);
- }
+ PrintTransformers(file, &out);
+
+ PrintServices(file, &out);
+
+ out.Print(GetNodeComments(file, false).c_str());
}
return output;
}