From 423ae6d013f0df1295db035faa200f8696344d8a Mon Sep 17 00:00:00 2001 From: Ben Sykes Date: Sat, 28 Apr 2018 17:37:23 -0700 Subject: [PATCH 1/3] Switch to Buffer.from to avoid using deprecated constructor --- src/compiler/node_generator.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc index 661587cbd63..dbc7eefb391 100644 --- a/src/compiler/node_generator.cc +++ b/src/compiler/node_generator.cc @@ -134,7 +134,7 @@ void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) { "throw new Error('Expected argument of type $name$');\n"); out->Outdent(); out->Print("}\n"); - out->Print("return new Buffer(arg.serializeBinary());\n"); + out->Print("return Buffer.from(arg.serializeBinary());\n"); out->Outdent(); out->Print("}\n\n"); From 1de3f4ae8d7de2bee7cf5ad3f014cd8f7f76bb0b Mon Sep 17 00:00:00 2001 From: Ben Sykes Date: Mon, 30 Apr 2018 22:43:31 -0700 Subject: [PATCH 2/3] Add grpc_out option to set minimum_node_version Example protoc call: protoc --plugin=protoc-gen-grpc=grpc_node_plugin --js_out=import_style=commonjs,binary:./autogen/ --grpc_out=minimum_node_version=8:./autogen/ hello.proto --- src/compiler/node_generator.cc | 18 ++++++++++++------ src/compiler/node_generator.h | 8 +++++++- src/compiler/node_plugin.cc | 22 +++++++++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc index dbc7eefb391..36ee60a198f 100644 --- a/src/compiler/node_generator.cc +++ b/src/compiler/node_generator.cc @@ -20,6 +20,7 @@ #include "src/compiler/config.h" #include "src/compiler/generator_helpers.h" +#include "src/compiler/node_generator.h" #include "src/compiler/node_generator_helpers.h" using grpc::protobuf::Descriptor; @@ -119,7 +120,7 @@ grpc::string NodeObjectPath(const Descriptor* descriptor) { } // Prints out the message serializer and deserializer functions -void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) { +void PrintMessageTransformer(const Descriptor* descriptor, Printer* out, const Parameters& params) { map template_vars; grpc::string full_name = descriptor->full_name(); template_vars["identifier_name"] = MessageIdentifierName(full_name); @@ -134,7 +135,12 @@ void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) { "throw new Error('Expected argument of type $name$');\n"); out->Outdent(); out->Print("}\n"); - out->Print("return Buffer.from(arg.serializeBinary());\n"); + if (params.minimum_node_version > 5) { + // Node version is > 5, we should use Buffer.from + out->Print("return Buffer.from(arg.serializeBinary());\n"); + } else { + out->Print("return new Buffer(arg.serializeBinary());\n"); + } out->Outdent(); out->Print("}\n\n"); @@ -219,12 +225,12 @@ void PrintImports(const FileDescriptor* file, Printer* out) { out->Print("\n"); } -void PrintTransformers(const FileDescriptor* file, Printer* out) { +void PrintTransformers(const FileDescriptor* file, Printer* out, const Parameters& params) { map messages = GetAllMessages(file); for (std::map::iterator it = messages.begin(); it != messages.end(); it++) { - PrintMessageTransformer(it->second, out); + PrintMessageTransformer(it->second, out, params); } out->Print("\n"); } @@ -236,7 +242,7 @@ void PrintServices(const FileDescriptor* file, Printer* out) { } } // namespace -grpc::string GenerateFile(const FileDescriptor* file) { +grpc::string GenerateFile(const FileDescriptor* file, const Parameters& params) { grpc::string output; { StringOutputStream output_stream(&output); @@ -257,7 +263,7 @@ grpc::string GenerateFile(const FileDescriptor* file) { PrintImports(file, &out); - PrintTransformers(file, &out); + PrintTransformers(file, &out, params); PrintServices(file, &out); diff --git a/src/compiler/node_generator.h b/src/compiler/node_generator.h index a9ffe75fc86..6906f2f450d 100644 --- a/src/compiler/node_generator.h +++ b/src/compiler/node_generator.h @@ -23,7 +23,13 @@ namespace grpc_node_generator { -grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file); +// Contains all the parameters that are parsed from the command line. +struct Parameters { + // Sets the earliest version of nodejs that needs to be supported. + int minimum_node_version; +}; + +grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file, const Parameters& params); } // namespace grpc_node_generator diff --git a/src/compiler/node_plugin.cc b/src/compiler/node_plugin.cc index bc38e9018a2..a224ac89d08 100644 --- a/src/compiler/node_plugin.cc +++ b/src/compiler/node_plugin.cc @@ -36,7 +36,27 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { const grpc::string& parameter, grpc::protobuf::compiler::GeneratorContext* context, grpc::string* error) const { - grpc::string code = GenerateFile(file); + + grpc_node_generator::Parameters generator_parameters; + generator_parameters.minimum_node_version = 4; + + if (!parameter.empty()) { + std::vector parameters_list = + grpc_generator::tokenize(parameter, ","); + for (auto parameter_string = parameters_list.begin(); + parameter_string != parameters_list.end(); parameter_string++) { + std::vector param = + grpc_generator::tokenize(*parameter_string, "="); + if (param[0] == "minimum_node_version") { + sscanf(param[1].c_str(), "%d", &generator_parameters.minimum_node_version); + } else { + *error = grpc::string("Unknown parameter: ") + *parameter_string; + return false; + } + } + } + + grpc::string code = GenerateFile(file, generator_parameters); if (code.size() == 0) { return true; } From 86d65ce29032840349b165d63fbee213fb2612d8 Mon Sep 17 00:00:00 2001 From: Ben Sykes Date: Wed, 2 May 2018 13:23:33 -0700 Subject: [PATCH 3/3] cleanup formatting with clang_format_code.sh --- src/compiler/node_generator.cc | 9 ++++++--- src/compiler/node_generator.h | 3 ++- src/compiler/node_plugin.cc | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc index 36ee60a198f..a430628dbc6 100644 --- a/src/compiler/node_generator.cc +++ b/src/compiler/node_generator.cc @@ -120,7 +120,8 @@ grpc::string NodeObjectPath(const Descriptor* descriptor) { } // Prints out the message serializer and deserializer functions -void PrintMessageTransformer(const Descriptor* descriptor, Printer* out, const Parameters& params) { +void PrintMessageTransformer(const Descriptor* descriptor, Printer* out, + const Parameters& params) { map template_vars; grpc::string full_name = descriptor->full_name(); template_vars["identifier_name"] = MessageIdentifierName(full_name); @@ -225,7 +226,8 @@ void PrintImports(const FileDescriptor* file, Printer* out) { out->Print("\n"); } -void PrintTransformers(const FileDescriptor* file, Printer* out, const Parameters& params) { +void PrintTransformers(const FileDescriptor* file, Printer* out, + const Parameters& params) { map messages = GetAllMessages(file); for (std::map::iterator it = messages.begin(); @@ -242,7 +244,8 @@ void PrintServices(const FileDescriptor* file, Printer* out) { } } // namespace -grpc::string GenerateFile(const FileDescriptor* file, const Parameters& params) { +grpc::string GenerateFile(const FileDescriptor* file, + const Parameters& params) { grpc::string output; { StringOutputStream output_stream(&output); diff --git a/src/compiler/node_generator.h b/src/compiler/node_generator.h index 6906f2f450d..f3a531597a1 100644 --- a/src/compiler/node_generator.h +++ b/src/compiler/node_generator.h @@ -29,7 +29,8 @@ struct Parameters { int minimum_node_version; }; -grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file, const Parameters& params); +grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file, + const Parameters& params); } // namespace grpc_node_generator diff --git a/src/compiler/node_plugin.cc b/src/compiler/node_plugin.cc index a224ac89d08..0d19d8e9820 100644 --- a/src/compiler/node_plugin.cc +++ b/src/compiler/node_plugin.cc @@ -36,7 +36,6 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { const grpc::string& parameter, grpc::protobuf::compiler::GeneratorContext* context, grpc::string* error) const { - grpc_node_generator::Parameters generator_parameters; generator_parameters.minimum_node_version = 4; @@ -48,7 +47,8 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { std::vector param = grpc_generator::tokenize(*parameter_string, "="); if (param[0] == "minimum_node_version") { - sscanf(param[1].c_str(), "%d", &generator_parameters.minimum_node_version); + sscanf(param[1].c_str(), "%d", + &generator_parameters.minimum_node_version); } else { *error = grpc::string("Unknown parameter: ") + *parameter_string; return false;