Skip to content

Commit

Permalink
Ran the code thru more linter
Browse files Browse the repository at this point in the history
  • Loading branch information
stanley-cheung committed Aug 24, 2018
1 parent 24e98a1 commit 2edebf9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 133 deletions.
145 changes: 61 additions & 84 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ string UppercaseFirstLetter(string s) {
return s;
}


// The following 5 functions were copied from
// google/protobuf/src/google/protobuf/stubs/strutil.h

Expand Down Expand Up @@ -154,7 +153,6 @@ string StripProto(const string& filename) {
}
}


// The following 6 functions were copied from
// google/protobuf/src/google/protobuf/compiler/js/js_generator.cc

Expand Down Expand Up @@ -353,8 +351,7 @@ void PrintES6Imports(Printer* printer, const FileDescriptor* file) {
void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
std::map<string, string> vars) {
PrintES6Imports(printer, file);
for (int service_index = 0;
service_index < file->service_count();
for (int service_index = 0; service_index < file->service_count();
++service_index) {
printer->Print("export class ");
const ServiceDescriptor* service = file->service(service_index);
Expand All @@ -372,21 +369,17 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
printer->Indent();
printer->Print("if (!options) options = {};\n");
if (vars["mode"] == GetModeVar(Mode::GRPCWEB)) {
printer->Print(
vars,
"options['format'] = '$format$';\n\n");
printer->Print(vars, "options['format'] = '$format$';\n\n");
}
printer->Print(
vars,
"this.client_ = new grpcWeb.$mode$ClientBase(options);\n"
"this.hostname_ = hostname;\n"
"this.credentials_ = credentials;\n"
"this.options_ = options;\n");
printer->Print(vars,
"this.client_ = new grpcWeb.$mode$ClientBase(options);\n"
"this.hostname_ = hostname;\n"
"this.credentials_ = credentials;\n"
"this.options_ = options;\n");
printer->Outdent();
printer->Print("}\n\n");

for (int method_index = 0;
method_index < service->method_count();
for (int method_index = 0; method_index < service->method_count();
++method_index) {
const MethodDescriptor* method = service->method(method_index);
vars["js_method_name"] = LowercaseFirstLetter(method->name());
Expand All @@ -396,62 +389,52 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
vars["serialize_func_name"] = GetSerializeMethodName(vars["mode"]);
vars["deserialize_func_name"] = GetDeserializeMethodName(vars["mode"]);
if (!method->client_streaming()) {
printer->Print(
vars,
"methodInfo_$method_name$ = "
"new grpcWeb.AbstractClientBase.MethodInfo(\n");
printer->Print(vars,
"methodInfo_$method_name$ = "
"new grpcWeb.AbstractClientBase.MethodInfo(\n");
printer->Indent();
printer->Print(
vars,
"$output_type$,\n"
"function(request: $input_type$) {\n"
" return request.$serialize_func_name$();\n"
"},\n"
"$output_type$.$deserialize_func_name$\n");
printer->Print(vars,
"$output_type$,\n"
"function(request: $input_type$) {\n"
" return request.$serialize_func_name$();\n"
"},\n"
"$output_type$.$deserialize_func_name$\n");
printer->Outdent();
printer->Print(");\n\n");
if (method->server_streaming()) {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(
vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata) {\n");
printer->Print(
vars,
"return this.client_.serverStreaming(\n");
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata) {\n");
printer->Print(vars, "return this.client_.serverStreaming(\n");
printer->Indent();
printer->Print(
vars,
"this.hostname_ +\n"
" '/$package_dot$$service_name$/$method_name$',\n"
"request,\n"
"metadata,\n"
"this.methodInfo_$method_name$);\n");
printer->Print(vars,
"this.hostname_ +\n"
" '/$package_dot$$service_name$/$method_name$',\n"
"request,\n"
"metadata,\n"
"this.methodInfo_$method_name$);\n");
printer->Outdent();
printer->Outdent();
printer->Print("}\n\n");
} else {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(
vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata,\n"
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void) {\n");
printer->Print(
vars,
"return this.client_.rpcCall(\n");
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata,\n"
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void) {\n");
printer->Print(vars, "return this.client_.rpcCall(\n");
printer->Indent();
printer->Print(
vars,
"this.hostname_ +\n"
" '/$package_dot$$service_name$/$method_name$',\n"
"request,\n"
"metadata,\n"
"this.methodInfo_$method_name$,\n"
"callback);\n");
printer->Print(vars,
"this.hostname_ +\n"
" '/$package_dot$$service_name$/$method_name$',\n"
"request,\n"
"metadata,\n"
"this.methodInfo_$method_name$,\n"
"callback);\n");
printer->Outdent();
printer->Outdent();
printer->Print("}\n\n");
Expand All @@ -466,8 +449,7 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file,
void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
PrintES6Imports(printer, file);
std::map<string, string> vars;
for (int service_index = 0;
service_index < file->service_count();
for (int service_index = 0; service_index < file->service_count();
++service_index) {
printer->Print("export class ");
const ServiceDescriptor* service = file->service(service_index);
Expand All @@ -478,8 +460,7 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
"constructor (hostname: string,\n"
" credentials: any,\n"
" options: any);\n\n");
for (int method_index = 0;
method_index < service->method_count();
for (int method_index = 0; method_index < service->method_count();
++method_index) {
const MethodDescriptor* method = service->method(method_index);
vars["js_method_name"] = LowercaseFirstLetter(method->name());
Expand All @@ -489,21 +470,19 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) {
if (method->server_streaming()) {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(
vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata\n");
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata\n");
printer->Outdent();
printer->Print("): grpcWeb.ClientReadableStream;\n\n");
} else {
printer->Print(vars, "$js_method_name$(\n");
printer->Indent();
printer->Print(
vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata,\n"
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void\n");
printer->Print(vars,
"request: $input_type$,\n"
"metadata: grpcWeb.Metadata,\n"
"callback: (err: grpcWeb.Error,\n"
" response: $output_type$) => void\n");
printer->Outdent();
printer->Print("): grpcWeb.ClientReadableStream;\n\n");
}
Expand Down Expand Up @@ -558,8 +537,8 @@ void PrintProtoDtsFile(Printer* printer, const FileDescriptor* file) {
printer->Print(vars, "set$js_field_name$(a: $js_field_type$): void;\n");
}
printer->Print(vars,
"serializeBinary(): Uint8Array;\n"
"static deserializeBinary: (bytes: any) => $class_name$;\n");
"serializeBinary(): Uint8Array;\n"
"static deserializeBinary: (bytes: any) => $class_name$;\n");
printer->Outdent();
printer->Print("}\n\n");
}
Expand Down Expand Up @@ -798,8 +777,8 @@ class GrpcCodeGenerator : public CodeGenerator {
generate_dts = true;
} else if (import_style_str == "typescript") {
import_style = ImportStyle::TYPESCRIPT;
file_name = UppercaseFirstLetter(StripProto(file->name())) +
"ServiceClientPb.ts";
file_name =
UppercaseFirstLetter(StripProto(file->name())) + "ServiceClientPb.ts";
} else {
*error = "options: invalid import_style - " + import_style_str;
return false;
Expand All @@ -813,10 +792,9 @@ class GrpcCodeGenerator : public CodeGenerator {
if (import_style == ImportStyle::TYPESCRIPT) {
PrintTypescriptFile(&printer, file, vars);

string proto_dts_file_name = StripProto(file->name()) +
"_pb.d.ts";
string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts";
std::unique_ptr<ZeroCopyOutputStream> proto_dts_output(
context->Open(proto_dts_file_name));
context->Open(proto_dts_file_name));
Printer proto_dts_printer(proto_dts_output.get(), '$');
PrintProtoDtsFile(&proto_dts_printer, file);

Expand Down Expand Up @@ -907,19 +885,18 @@ class GrpcCodeGenerator : public CodeGenerator {
}

if (generate_dts) {
string grpcweb_dts_file_name = StripProto(file->name()) +
"_grpc_web_pb.d.ts";
string proto_dts_file_name = StripProto(file->name()) +
"_pb.d.ts";
string grpcweb_dts_file_name =
StripProto(file->name()) + "_grpc_web_pb.d.ts";
string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts";

std::unique_ptr<ZeroCopyOutputStream> grpcweb_dts_output(
context->Open(grpcweb_dts_file_name));
context->Open(grpcweb_dts_file_name));
Printer grpcweb_dts_printer(grpcweb_dts_output.get(), '$');

PrintGrpcWebDtsFile(&grpcweb_dts_printer, file);

std::unique_ptr<ZeroCopyOutputStream> proto_dts_output(
context->Open(proto_dts_file_name));
context->Open(proto_dts_file_name));
Printer proto_dts_printer(proto_dts_output.get(), '$');

PrintProtoDtsFile(&proto_dts_printer, file);
Expand Down
Loading

0 comments on commit 2edebf9

Please sign in to comment.