Skip to content

Commit

Permalink
Add omit_serialize_instanceof generator option
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed Dec 28, 2024
1 parent 263c478 commit c6bb399
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/grpc-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ one of the following:
gRPC library, and instead generates `PackageDefinition` objects that can
be passed to the `loadPackageDefinition` function provided by both the
`grpc` and `@grpc/grpc-js` libraries.
- `omit_serialize_instanceof`: Omit the `instanceof` check for messages in
client code. This is useful when the message was renamed or is from a
different package, and serialization would fail with
`Expected argument of type …`.
26 changes: 16 additions & 10 deletions packages/grpc-tools/src/node_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ 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);
Expand All @@ -129,12 +131,14 @@ void PrintMessageTransformer(const Descriptor* descriptor, Printer* out) {
// Print the serializer
out->Print(template_vars, "function serialize_$identifier_name$(arg) {\n");
out->Indent();
out->Print(template_vars, "if (!(arg instanceof $node_name$)) {\n");
out->Indent();
out->Print(template_vars,
"throw new Error('Expected argument of type $name$');\n");
out->Outdent();
out->Print("}\n");
if (!params.omit_serialize_instanceof) {
out->Print(template_vars, "if (!(arg instanceof $node_name$)) {\n");
out->Indent();
out->Print(template_vars,
"throw new Error('Expected argument of type $name$');\n");
out->Outdent();
out->Print("}\n");
}
out->Print("return Buffer.from(arg.serializeBinary());\n");
out->Outdent();
out->Print("}\n\n");
Expand Down Expand Up @@ -232,12 +236,14 @@ 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");
}
Expand Down Expand Up @@ -273,7 +279,7 @@ grpc::string GenerateFile(const FileDescriptor* file,

PrintImports(file, &out, params);

PrintTransformers(file, &out);
PrintTransformers(file, &out, params);

PrintServices(file, &out, params);

Expand Down
2 changes: 2 additions & 0 deletions packages/grpc-tools/src/node_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct Parameters {
bool generate_package_definition;
// Use pure JavaScript gRPC Client
bool grpc_js;
// Omit instanceof check for request messages
bool omit_serialize_instanceof;
};

grpc::string GenerateFile(const grpc::protobuf::FileDescriptor* file,
Expand Down
3 changes: 3 additions & 0 deletions packages/grpc-tools/src/node_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
grpc_node_generator::Parameters generator_parameters;
generator_parameters.generate_package_definition = false;
generator_parameters.grpc_js = false;
generator_parameters.omit_serialize_instanceof = false;
if (!parameter.empty()) {
std::vector<grpc::string> parameters_list =
grpc_generator::tokenize(parameter, ",");
Expand All @@ -48,6 +49,8 @@ class NodeGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
generator_parameters.generate_package_definition = true;
} else if (*parameter_string == "grpc_js") {
generator_parameters.grpc_js = true;
} else if (*parameter_string == "omit_serialize_instanceof") {
generator_parameters.omit_serialize_instanceof = true;
}
}
}
Expand Down

0 comments on commit c6bb399

Please sign in to comment.