@@ -159,6 +159,13 @@ string GetSerializeMethodName(const string& mode_var) {
159159 return " serializeBinary" ;
160160}
161161
162+ std::string GetSerializeMethodReturnType (const string& mode_var) {
163+ if (mode_var == GetModeVar (Mode::OPJSPB)) {
164+ return " string" ;
165+ }
166+ return " !Uint8Array" ;
167+ }
168+
162169string LowercaseFirstLetter (string s) {
163170 if (s.empty ()) {
164171 return s;
@@ -504,15 +511,36 @@ string GetRootPath(const string& from_filename, const string& to_filename) {
504511 return result;
505512}
506513
507- // Returns the basename of a file.
508- string GetBasename (string filename)
509- {
510- size_t last_slash = filename.find_last_of (' /' );
511- if (last_slash != string::npos)
512- {
513- return filename.substr (last_slash + 1 );
514+ // Splits path immediately following the final slash, separating it into a
515+ // directory and file name component. Directory will contain the last
516+ // slash, if it's not empty.
517+ // If there is no slash in path, Split returns an empty directory and
518+ // basename set to path.
519+ // Output values have the property that path = directory + basename.
520+ void PathSplit (const string& path, string* directory, string* basename) {
521+ string::size_type last_slash = path.rfind (' /' );
522+ if (last_slash == string::npos) {
523+ if (directory) {
524+ *directory = " " ;
525+ }
526+ if (basename) {
527+ *basename = path;
528+ }
529+ } else {
530+ if (directory) {
531+ *directory = path.substr (0 , last_slash + 1 );
532+ }
533+ if (basename) {
534+ *basename = path.substr (last_slash + 1 );
535+ }
514536 }
515- return filename;
537+ }
538+
539+ // Returns the basename of a file.
540+ string GetBasename (string filename) {
541+ string basename;
542+ PathSplit (filename, nullptr , &basename);
543+ return basename;
516544}
517545
518546/* Finds all message types used in all services in the file, and returns them
@@ -1191,11 +1219,16 @@ void PrintMethodDescriptorFile(Printer* printer,
11911219 " $in_type$,\n " );
11921220 printer->Print (vars,
11931221 " $out_type$,\n "
1194- " /** @param {!proto.$in$} request */\n "
1222+ " /**\n "
1223+ " * @param {!proto.$in$} request\n " );
1224+ printer->Print (
1225+ (" * @return {" + GetSerializeMethodReturnType (vars[" mode" ]) + " }\n " )
1226+ .c_str ());
1227+ printer->Print (" */\n "
11951228 " function(request) {\n " );
11961229 printer->Print (
11971230 (" return request." + GetSerializeMethodName (vars[" mode" ]) + " ();\n " )
1198- .c_str ());
1231+ .c_str ());
11991232 printer->Print (" },\n " );
12001233 printer->Print (
12011234 vars, (" $out_type$." + GetDeserializeMethodName (vars[" mode" ])).c_str ());
@@ -1237,15 +1270,6 @@ void PrintServiceConstructor(Printer* printer,
12371270 " * @private @const {string} The hostname\n "
12381271 " */\n "
12391272 " this.hostname_ = hostname;\n\n "
1240- " /**\n "
1241- " * @private @const {?Object} The credentials to be used to connect\n "
1242- " * to the server\n "
1243- " */\n "
1244- " this.credentials_ = credentials;\n\n "
1245- " /**\n "
1246- " * @private @const {?Object} Options for the client\n "
1247- " */\n "
1248- " this.options_ = options;\n "
12491273 " };\n\n\n " );
12501274}
12511275
@@ -1276,15 +1300,6 @@ void PrintPromiseServiceConstructor(Printer* printer,
12761300 " * @private @const {string} The hostname\n "
12771301 " */\n "
12781302 " this.hostname_ = hostname;\n\n "
1279- " /**\n "
1280- " * @private @const {?Object} The credentials to be used to connect\n "
1281- " * to the server\n "
1282- " */\n "
1283- " this.credentials_ = credentials;\n\n "
1284- " /**\n "
1285- " * @private @const {?Object} Options for the client\n "
1286- " */\n "
1287- " this.options_ = options;\n "
12881303 " };\n\n\n " );
12891304}
12901305
@@ -1307,7 +1322,12 @@ void PrintMethodInfo(Printer* printer, std::map<string, string> vars) {
13071322 " $in_type$,\n " );
13081323 printer->Print (vars,
13091324 " $out_type$,\n "
1310- " /** @param {!proto.$in$} request */\n "
1325+ " /**\n "
1326+ " * @param {!proto.$in$} request\n " );
1327+ printer->Print (
1328+ (" * @return {" + GetSerializeMethodReturnType (vars[" mode" ]) + " }\n " )
1329+ .c_str ());
1330+ printer->Print (" */\n "
13111331 " function(request) {\n " );
13121332 printer->Print (
13131333 (" return request." + GetSerializeMethodName (vars[" mode" ]) + " ();\n " )
@@ -1334,7 +1354,12 @@ void PrintMethodInfo(Printer* printer, std::map<string, string> vars) {
13341354
13351355 printer->Print (vars,
13361356 " $out_type$,\n "
1337- " /** @param {!proto.$in$} request */\n "
1357+ " /**\n "
1358+ " * @param {!proto.$in$} request\n " );
1359+ printer->Print (
1360+ (" * @return {" + GetSerializeMethodReturnType (vars[" mode" ]) + " }\n " )
1361+ .c_str ());
1362+ printer->Print (" */\n "
13381363 " function(request) {\n " );
13391364 printer->Print (
13401365 (" return request." + GetSerializeMethodName (vars[" mode" ]) + " ();\n " )
@@ -1532,8 +1557,13 @@ class GrpcCodeGenerator : public CodeGenerator {
15321557 generate_dts = true ;
15331558 } else if (import_style_str == " typescript" ) {
15341559 import_style = ImportStyle::TYPESCRIPT;
1535- file_name =
1536- UppercaseFirstLetter (StripProto (file->name ())) + " ServiceClientPb.ts" ;
1560+
1561+ string directory;
1562+ string basename;
1563+
1564+ PathSplit (file->name (), &directory, &basename);
1565+ file_name = directory + UppercaseFirstLetter (StripProto (basename)) +
1566+ " ServiceClientPb.ts" ;
15371567 } else {
15381568 *error = " options: invalid import_style - " + import_style_str;
15391569 return false ;
0 commit comments