Merge pull request #15225 from bsyk/master

Switch to Buffer.from to avoid using deprecated constructor
pull/15258/head
Nicolas Noble 7 years ago committed by GitHub
commit 1ff2e3878f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      src/compiler/node_generator.cc
  2. 9
      src/compiler/node_generator.h
  3. 22
      src/compiler/node_plugin.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);

@ -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

@ -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;
}

Loading…
Cancel
Save