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

Introducing MethodDescriptor #567

Merged
merged 3 commits into from
Jun 6, 2019
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
18 changes: 18 additions & 0 deletions javascript/net/grpc/web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ closure_js_library(
deps = [
":clientreadablestream",
":error",
":methoddescriptor",
],
)

Expand Down Expand Up @@ -107,6 +108,23 @@ closure_js_library(
],
)

closure_js_library(
name = "methoddescriptor",
srcs = [
"methoddescriptor.js",
],
deps = [
":methodtype",
],
)

closure_js_library(
name = "methodtype",
srcs = [
"methodtype.js",
],
)

closure_js_library(
name = "status",
srcs = [
Expand Down
11 changes: 7 additions & 4 deletions javascript/net/grpc/web/abstractclientbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ goog.module.declareLegacyNamespace();

const ClientReadableStream = goog.require('grpc.web.ClientReadableStream');
const Error = goog.require('grpc.web.Error');
const MethodDescriptor = goog.require('grpc.web.MethodDescriptor');


/**
Expand Down Expand Up @@ -80,7 +81,8 @@ AbstractClientBase.MethodInfo = function(
* @param {string} method The method to invoke
* @param {REQUEST} request The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!AbstractClientBase.MethodInfo<REQUEST, RESPONSE_LEAN>}
* @param {!AbstractClientBase.MethodInfo<REQUEST,
* RESPONSE_LEAN>|!MethodDescriptor<REQUEST, RESPONSE_LEAN>}
* methodInfo Information of this RPC method
* @param {function(?Error, ?RESPONSE)}
* callback A callback function which takes (error, response)
Expand All @@ -95,7 +97,8 @@ AbstractClientBase.prototype.rpcCall = goog.abstractMethod;
* @param {string} method The method to invoke
* @param {REQUEST} request The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!AbstractClientBase.MethodInfo<REQUEST, RESPONSE>}
* @param {!AbstractClientBase.MethodInfo<REQUEST,
* RESPONSE>|!MethodDescriptor<REQUEST, RESPONSE>}
* methodInfo Information of this RPC method
* @return {!Promise<!RESPONSE>}
* A promise that resolves to the response message
Expand All @@ -108,7 +111,8 @@ AbstractClientBase.prototype.unaryCall = goog.abstractMethod;
* @param {string} method The method to invoke
* @param {REQUEST} request The request proto
* @param {!Object<string, string>} metadata User defined call metadata
* @param {!AbstractClientBase.MethodInfo<REQUEST, RESPONSE>}
* @param {!AbstractClientBase.MethodInfo<REQUEST,
* RESPONSE>|!MethodDescriptor<REQUEST, RESPONSE>}
* methodInfo Information of this RPC method
* @return {!ClientReadableStream<RESPONSE>} The Client Readable Stream
*/
Expand All @@ -117,4 +121,3 @@ AbstractClientBase.prototype.serverStreaming = goog.abstractMethod;


exports = AbstractClientBase;

107 changes: 71 additions & 36 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,16 @@ string GetModeVar(const Mode mode) {
}

string GetDeserializeMethodName(const string& mode_var) {
if (mode_var == GetModeVar(Mode::OPJSPB)) {
if (mode_var == GetModeVar(Mode::OPJSPB) ||
mode_var == GetModeVar(Mode::FRAMEWORKS)) {
return "deserialize";
}
return "deserializeBinary";
}

string GetSerializeMethodName(const string& mode_var) {
if (mode_var == GetModeVar(Mode::OPJSPB)) {
if (mode_var == GetModeVar(Mode::OPJSPB) ||
mode_var == GetModeVar(Mode::FRAMEWORKS)) {
return "serialize";
}
return "serializeBinary";
Expand Down Expand Up @@ -664,6 +666,7 @@ 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()) {
// TODO(jennyjiang): use methodDescriptor?
printer->Print(vars,
"methodInfo$method_name$ = "
"new grpcWeb.AbstractClientBase.MethodInfo(\n");
Expand Down Expand Up @@ -1058,34 +1061,60 @@ void PrintPromiseServiceConstructor(Printer* printer,
}

void PrintMethodInfo(Printer* printer, std::map<string, string> vars) {
printer->Print(
vars,
"/**\n"
" * @const\n"
" * @type {!grpc.web.AbstractClientBase.MethodInfo<\n"
" * !proto.$in$,\n"
" * !proto.$out$>}\n"
" */\n"
"const methodInfo_$service_name$_$method_name$ = "
"new grpc.web.AbstractClientBase.MethodInfo(\n");
// Print MethodDescriptor.
printer->Print(vars,
"/**\n"
" * @const\n"
" * @type {!grpc.web.MethodDescriptor<\n"
" * !proto.$in$,\n"
" * !proto.$out$>}\n"
" */\n"
"const methodDescriptor_$service_name$_$method_name$ = "
"new grpc.web.MethodDescriptor(\n");
printer->Indent();
printer->Print(vars,
"'/$package_dot$$service_name$/$method_name$',\n"
"$method_type$,\n"
"$in_type$,\n");
printer->Print(vars,
"$out_type$,\n"
"/** @param {!proto.$in$} request */\n"
"function(request) {\n");
printer->Print(
vars,
"$out_type$,\n"
"/** @param {!proto.$in$} request */\n"
"function(request) {\n");
printer->Print(
(" return request." + GetSerializeMethodName(vars["mode"]) +
"();\n").c_str());
(" return request." + GetSerializeMethodName(vars["mode"]) + "();\n")
.c_str());
printer->Print("},\n");
printer->Print(
vars,
("$out_type$." + GetDeserializeMethodName(vars["mode"]) +
"\n").c_str());
("$out_type$." + GetDeserializeMethodName(vars["mode"]) + "\n").c_str());
printer->Outdent();
printer->Print(vars, ");\n\n\n");

// Print AbstractClientBase.MethodInfo, which will be deprecated.
printer->Print(vars,
"/**\n"
" * @const\n"
" * @type {!grpc.web.AbstractClientBase.MethodInfo<\n"
" * !proto.$in$,\n"
" * !proto.$out$>}\n"
" */\n"
"const methodInfo_$service_name$_$method_name$ = "
"new grpc.web.AbstractClientBase.MethodInfo(\n");
printer->Indent();

printer->Print(vars,
"$out_type$,\n"
"/** @param {!proto.$in$} request */\n"
"function(request) {\n");
printer->Print(
(" return request." + GetSerializeMethodName(vars["mode"]) + "();\n")
.c_str());
printer->Print("},\n");
printer->Print(
vars,
");\n\n\n");
("$out_type$." + GetDeserializeMethodName(vars["mode"]) + "\n").c_str());
printer->Outdent();
printer->Print(vars, ");\n\n\n");
}

void PrintUnaryCall(Printer* printer, std::map<string, string> vars) {
Expand Down Expand Up @@ -1116,12 +1145,11 @@ void PrintUnaryCall(Printer* printer, std::map<string, string> vars) {
} else {
printer->Print(vars, "'/$package_dot$$service_name$/$method_name$',\n");
}
printer->Print(
vars,
"request,\n"
"metadata || {},\n"
"methodInfo_$service_name$_$method_name$,\n"
"callback);\n");
printer->Print(vars,
"request,\n"
"metadata || {},\n"
"methodDescriptor_$service_name$_$method_name$,\n"
"callback);\n");
printer->Outdent();
printer->Outdent();
printer->Outdent();
Expand Down Expand Up @@ -1156,7 +1184,7 @@ void PrintPromiseUnaryCall(Printer* printer, std::map<string, string> vars) {
printer->Print(vars,
"request,\n"
"metadata || {},\n"
"methodInfo_$service_name$_$method_name$);\n");
"methodDescriptor_$service_name$_$method_name$);\n");
printer->Outdent();
printer->Outdent();
printer->Outdent();
Expand Down Expand Up @@ -1188,11 +1216,10 @@ void PrintServerStreamingCall(Printer* printer, std::map<string, string> vars) {
} else {
printer->Print(vars, "'/$package_dot$$service_name$/$method_name$',\n");
}
printer->Print(
vars,
"request,\n"
"metadata || {},\n"
"methodInfo_$service_name$_$method_name$);\n");
printer->Print(vars,
"request,\n"
"metadata || {},\n"
"methodDescriptor_$service_name$_$method_name$);\n");
printer->Outdent();
printer->Outdent();
printer->Outdent();
Expand Down Expand Up @@ -1322,6 +1349,8 @@ class GrpcCodeGenerator : public CodeGenerator {
printer.Print(vars, "goog.require('grpc.web.AbstractClientBase');\n");
printer.Print(vars, "goog.require('grpc.web.ClientReadableStream');\n");
printer.Print(vars, "goog.require('grpc.web.Error');\n");
printer.Print(vars, "goog.require('grpc.web.MethodDescriptor');\n");
printer.Print(vars, "goog.require('grpc.web.MethodType');\n");

PrintMessagesDeps(&printer, file);
printer.Print("goog.scope(function() {\n\n");
Expand Down Expand Up @@ -1357,19 +1386,25 @@ class GrpcCodeGenerator : public CodeGenerator {
// of the global name.
vars["out_type"] = ModuleAlias(method->output_type()->file()->name())
+ GetNestedMessageName(method->output_type());
vars["in_type"] = ModuleAlias(method->input_type()->file()->name()) +
GetNestedMessageName(method->input_type());
} else {
vars["out_type"] = "proto."+method->output_type()->full_name();
vars["out_type"] = "proto." + method->output_type()->full_name();
vars["in_type"] = "proto." + method->input_type()->full_name();
}

// Client streaming is not supported yet
if (!method->client_streaming()) {
PrintMethodInfo(&printer, vars);
if (method->server_streaming()) {
vars["method_type"] = "grpc.web.MethodType.SERVER_STREAMING";
PrintMethodInfo(&printer, vars);
vars["client_type"] = "Client";
PrintServerStreamingCall(&printer, vars);
vars["client_type"] = "PromiseClient";
PrintServerStreamingCall(&printer, vars);
} else {
vars["method_type"] = "grpc.web.MethodType.UNARY";
PrintMethodInfo(&printer, vars);
PrintUnaryCall(&printer, vars);
PrintPromiseUnaryCall(&printer, vars);
}
Expand Down
40 changes: 40 additions & 0 deletions javascript/net/grpc/web/methoddescriptor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @fileoverview Description of this file.
*
* A templated class that is used to address gRPC Web requests.
*/

goog.module('grpc.web.MethodDescriptor');
goog.module.declareLegacyNamespace();

const MethodType = goog.require('grpc.web.MethodType');

/**
* @constructor
* @struct
* @template REQUEST, RESPONSE
* @param {string} name
* @param {!MethodType} methodType
* @param {function(new: REQUEST, ...)} requestType
* @param {function(new: RESPONSE, ...)} responseType
* @param {function(REQUEST): ?} requestSerializeFn
* @param {function(?): RESPONSE} responseDeserializeFn
*/
const MethodDescriptor = function(
name, methodType, requestType, responseType, requestSerializeFn,
responseDeserializeFn) {
/** @const */
this.name = name;
/** @const */
this.methodType = methodType;
/** @const */
this.requestType = requestType;
/** @const */
this.responseType = responseType;
/** @const */
this.requestSerializeFn = requestSerializeFn;
/** @const */
this.responseDeserializeFn = responseDeserializeFn;
};

exports = MethodDescriptor;
23 changes: 23 additions & 0 deletions javascript/net/grpc/web/methodtype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @fileoverview Description of this file.
*
* grpc web MethodType
*/

goog.module('grpc.web.MethodType');

goog.module.declareLegacyNamespace();

/**
* See grpc.web.AbstractClientBase.
* MethodType.UNARY for rpcCall/unaryCall.
* MethodType.SERVER_STREAMING for serverStreaming.
*
* @enum {string}
*/
const MethodType = {
'UNARY': 'unary',
'SERVER_STREAMING': 'server_streaming'
};

exports = MethodType;
4 changes: 4 additions & 0 deletions packages/grpc-web/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ goog.module('grpc.web.Exports');
const AbstractClientBase = goog.require('grpc.web.AbstractClientBase');
const GrpcWebClientBase = goog.require('grpc.web.GrpcWebClientBase');
const StatusCode = goog.require('grpc.web.StatusCode');
const MethodDescriptor = goog.require('grpc.web.MethodDescriptor');
const MethodType = goog.require('grpc.web.MethodType');

const exports = module['exports'];

exports['AbstractClientBase'] = {'MethodInfo': AbstractClientBase.MethodInfo};
exports['GrpcWebClientBase'] = GrpcWebClientBase;
exports['StatusCode'] = StatusCode;
exports['MethodDescriptor'] = MethodDescriptor;
exports['MethodType'] = MethodType;
4 changes: 4 additions & 0 deletions packages/grpc-web/test/export_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('grpc-web export test', function() {
assert.equal(typeof grpc.web.AbstractClientBase.MethodInfo, 'function');
});

it('should have MethodDescriptor exported', function() {
assert.equal(typeof grpc.web.MethodDescriptor, 'function');
});

it('should have GrpcWebClientBase#rpcCall() exported', function() {
assert.equal(typeof grpc.web.GrpcWebClientBase.prototype.rpcCall, 'function');
});
Expand Down