This is an automated email from the ASF dual-hosted git repository.

jking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c93bb6  THRIFT-4738: Fix typescript declaration generation
0c93bb6 is described below

commit 0c93bb69032e7dd234a15efc75c19e9eecd27276
Author: Mustafa Senol Cosar <must...@unscrambl.com>
AuthorDate: Wed Jan 16 15:03:12 2019 +0300

    THRIFT-4738: Fix typescript declaration generation
---
 compiler/cpp/src/thrift/generate/t_js_generator.cc | 62 +++++++++++++++-------
 1 file changed, 42 insertions(+), 20 deletions(-)

diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc 
b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index fc23e87..f8f1bee 100644
--- a/compiler/cpp/src/thrift/generate/t_js_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_js_generator.cc
@@ -206,6 +206,7 @@ public:
   std::string ts_includes();
   std::string ts_service_includes();
   std::string render_includes();
+  std::string render_ts_includes();
   std::string declare_field(t_field* tfield, bool init = false, bool obj = 
false);
   std::string function_signature(t_function* tfunction,
                                  std::string prefix = "",
@@ -287,7 +288,7 @@ public:
    * TypeScript Definition File helper functions
    */
 
-  string ts_function_signature(t_function* tfunction, bool include_callback, 
bool optional_callback);
+  string ts_function_signature(t_function* tfunction, bool include_callback);
   string ts_get_type(t_type* type);
 
   /**
@@ -511,6 +512,27 @@ string t_js_generator::render_includes() {
 }
 
 /**
+ * Renders all the imports necessary for including another Thrift program
+ */
+string t_js_generator::render_ts_includes() {
+  string result;
+
+  if (!gen_node_) {
+    return result;
+  }
+  const vector<t_program*>& includes = program_->get_includes();
+  for (size_t i = 0; i < includes.size(); ++i) {
+    result += "import " + 
make_valid_nodeJs_identifier(includes[i]->get_name()) + "_ttypes = require('./" 
+ includes[i]->get_name()
+              + "_types');\n";
+  }
+  if (includes.size() > 0) {
+    result += "\n";
+  }
+
+  return result;
+}
+
+/**
  * Close up (or down) some filez.
  */
 void t_js_generator::close_generator() {
@@ -1117,7 +1139,7 @@ void t_js_generator::generate_service(t_service* 
tservice) {
       f_service_ts_ << "/// <reference path=\"" << 
tservice->get_extends()->get_name()
                     << ".d.ts\" />" << endl;
     }
-    f_service_ts_ << autogen_comment() << endl << ts_includes() << endl << 
endl;
+    f_service_ts_ << autogen_comment() << endl << ts_includes() << endl << 
render_ts_includes() << endl;
     if (gen_node_) {
       f_service_ts_ << "import ttypes = require('./" + program_->get_name() + 
"_types');" << endl;
       // Generate type aliases
@@ -1219,9 +1241,9 @@ void 
t_js_generator::generate_service_processor(t_service* tservice) {
       }
       f_service_ts_ << "{" << endl;
       indent_up();
-      f_service_ts_ << ts_indent() << "private _handler: Object;" << endl << 
endl;
-      f_service_ts_ << ts_indent() << "constructor(handler: Object);" << endl;
-      f_service_ts_ << ts_indent() << "process(input: Thrift.TJSONProtocol, 
output: Thrift.TJSONProtocol): void;" << endl;
+      f_service_ts_ << ts_indent() << "private _handler: object;" << endl << 
endl;
+      f_service_ts_ << ts_indent() << "constructor(handler: object);" << endl;
+      f_service_ts_ << ts_indent() << "process(input: thrift.TProtocol, 
output: thrift.TProtocol): void;" << endl;
       indent_down();
     }
   } else {
@@ -1325,7 +1347,7 @@ void t_js_generator::generate_process_function(t_service* 
tservice, t_function*
   }
   if (gen_ts_) {
     indent_up();
-    f_service_ts_ << ts_indent() << "process_" << tfunction->get_name() << 
"(seqid: number, input: Thrift.TJSONProtocol, output: Thrift.TJSONProtocol): 
void;" << endl;
+    f_service_ts_ << ts_indent() << "process_" << tfunction->get_name() << 
"(seqid: number, input: thrift.TProtocol, output: thrift.TProtocol): void;" << 
endl;
     indent_down();
   }
 
@@ -1645,10 +1667,11 @@ void t_js_generator::generate_service_client(t_service* 
tservice) {
     indent(f_service_) << "this._seqid = 0;" << endl;
     indent(f_service_) << "this._reqs = {};" << endl;
     if (gen_ts_) {
-      f_service_ts_ << ts_indent() << "private input: Thrift.TJSONProtocol;" 
<< endl << ts_indent()
-                    << "private output: Thrift.TJSONProtocol;" << endl << 
ts_indent() << "private seqid: number;"
-                    << endl << endl << ts_indent()
-                    << "constructor(input: Thrift.TJSONProtocol, output?: 
Thrift.TJSONProtocol);"
+      f_service_ts_ << ts_indent() << "private output: thrift.TTransport;" << 
endl
+                    << ts_indent() << "private pClass: thrift.TProtocol;" << 
endl
+                    << ts_indent() << "private _seqid: number;" << endl
+                    << endl
+                    << ts_indent() << "constructor(output: thrift.TTransport, 
pClass: { new(trans: thrift.TTransport): thrift.TProtocol });"
                     << endl;
     }
   } else {
@@ -1719,13 +1742,13 @@ void t_js_generator::generate_service_client(t_service* 
tservice) {
 
     if (gen_ts_) {
       // function definition without callback
-      f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, false, false) << endl;
+      f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, false) << endl;
       if (!gen_es6_) {
         // overload with callback
-        f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, true, false) << endl;
+        f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, true) << endl;
       } else {
         // overload with callback
-        f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, true, true) << endl;
+        f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << 
ts_function_signature(*f_iter, true) << endl;
       }
     }
 
@@ -2669,7 +2692,7 @@ string t_js_generator::ts_get_type(t_type* type) {
  * @param bool in-/exclude the callback argument
  * @return String of rendered function definition
  */
-std::string t_js_generator::ts_function_signature(t_function* tfunction, bool 
include_callback, bool optional_callback) {
+std::string t_js_generator::ts_function_signature(t_function* tfunction, bool 
include_callback) {
   string str;
   const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
   vector<t_field*>::const_iterator f_iter;
@@ -2685,7 +2708,6 @@ std::string 
t_js_generator::ts_function_signature(t_function* tfunction, bool in
   }
 
   if (include_callback) {
-    string callback_optional_string = optional_callback ? "?" : "";
     if (gen_node_) {
       t_struct* exceptions = tfunction->get_xceptions();
       string exception_types;
@@ -2694,19 +2716,19 @@ std::string 
t_js_generator::ts_function_signature(t_function* tfunction, bool in
         for (vector<t_field*>::const_iterator it = members.begin(); it != 
members.end(); ++it) {
           t_type* t = get_true_type((*it)->get_type());
           if (it == members.begin()) {
-            exception_types = t->get_name();
+            exception_types = js_type_namespace(t->get_program()) + 
t->get_name();
           } else {
-            exception_types += " | " + t->get_name();
+            exception_types += " | " + js_type_namespace(t->get_program()) + 
t->get_name();
           }
         }
       }
       if (exception_types == "") {
-        str += "callback" + callback_optional_string + ": (error: void, 
response: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
+        str += "callback?: (error: void, response: " + 
ts_get_type(tfunction->get_returntype()) + ")=>void): ";
       } else {
-        str += "callback" + callback_optional_string + ": (error: " + 
exception_types + ", response: " + ts_get_type(tfunction->get_returntype()) + 
")=>void): ";
+        str += "callback?: (error: " + exception_types + ", response: " + 
ts_get_type(tfunction->get_returntype()) + ")=>void): ";
       }
     } else {
-      str += "callback" + callback_optional_string + ": (data: " + 
ts_get_type(tfunction->get_returntype()) + ")=>void): ";
+      str += "callback?: (data: " + ts_get_type(tfunction->get_returntype()) + 
")=>void): ";
     }
 
     if (gen_jquery_) {

Reply via email to