Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc-tools): add omit_serialize_instanceof generator option #2874

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 …`.
24 changes: 14 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,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);
Expand All @@ -129,12 +130,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 +235,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");
}
Expand Down Expand Up @@ -273,7 +277,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 messages in serialize methods
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
Loading