diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/node_generator.cc | 21 | ||||
-rw-r--r-- | src/compiler/node_generator.h | 9 | ||||
-rw-r--r-- | src/compiler/node_plugin.cc | 22 |
3 files changed, 44 insertions, 8 deletions
diff --git a/src/compiler/node_generator.cc b/src/compiler/node_generator.cc index 661587cbd6..a430628dbc 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,8 @@ 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<grpc::string, grpc::string> template_vars; grpc::string full_name = descriptor->full_name(); template_vars["identifier_name"] = MessageIdentifierName(full_name); @@ -134,7 +136,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 new Buffer(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 +226,13 @@ 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<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); + PrintMessageTransformer(it->second, out, params); } out->Print("\n"); } @@ -236,7 +244,8 @@ 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 +266,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 a9ffe75fc8..f3a531597a 100644 --- a/src/compiler/node_generator.h +++ b/src/compiler/node_generator.h @@ -23,7 +23,14 @@ 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 bc38e9018a..0d19d8e982 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<grpc::string> parameters_list = + grpc_generator::tokenize(parameter, ","); + for (auto parameter_string = parameters_list.begin(); + parameter_string != parameters_list.end(); parameter_string++) { + std::vector<grpc::string> 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; } |