mirror of https://github.com/grpc/grpc-node.git
Merge pull request #2874 from DefangLabs/lio/omit_request_instanceof
feat(grpc-tools): add omit_serialize_instanceof generator option
This commit is contained in:
commit
22bbe8a315
|
@ -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 …`.
|
||||
|
|
|
@ -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);
|
||||
|
@ -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");
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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, ",");
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue