THRIFT-5692 Support for deprecated methods (via annotation)
Client: haxe
Patch: Jens Geyer
diff --git a/compiler/cpp/src/thrift/generate/t_haxe_generator.cc b/compiler/cpp/src/thrift/generate/t_haxe_generator.cc
index aa628b7..9397ff1 100644
--- a/compiler/cpp/src/thrift/generate/t_haxe_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_haxe_generator.cc
@@ -209,6 +209,8 @@
   string generate_service_method_onsuccess(t_function* tfunction, bool as_type, bool omit_name);
   void generate_service_method_signature_combined(t_function* tfunction, bool is_interface);
   void generate_service_method_signature_normal(t_function* tfunction, bool is_interface);
+  void generate_deprecation_attribute(ostream& out, t_function* func, bool as_comment);
+  string make_haxe_string_literal( string const& value);
 
   bool type_can_be_null(t_type* ttype) {
     ttype = get_true_type(ttype);
@@ -1664,6 +1666,7 @@
 void t_haxe_generator::generate_service_method_signature_normal(t_function* tfunction,
                                                                 bool is_interface) {
   if (is_interface) {
+    generate_deprecation_attribute(f_service_, tfunction, true);
     indent(f_service_) << function_signature_normal(tfunction) << ";" << endl << endl;
   } else {
     indent(f_service_) << "public " << function_signature_normal(tfunction) << " {" << endl;
@@ -1684,12 +1687,60 @@
   }
 
   if (is_interface) {
+    generate_deprecation_attribute(f_service_, tfunction, false);
     indent(f_service_) << function_signature_combined(tfunction) << ";" << endl << endl;
   } else {
     indent(f_service_) << "public " << function_signature_combined(tfunction) << " {" << endl;
   }
 }
 
+string t_haxe_generator::make_haxe_string_literal( string const& value)
+{
+  if (value.length() == 0) {
+    return "";
+  }
+
+  std::stringstream result;
+  result << "\"";
+  for (signed char const c: value) {
+    if( (c >= 0) && (c < 32)) {  // convert ctrl chars, but leave UTF-8 alone
+      int width = std::min( (int)sizeof(c), 6);
+      result << "\\u{" << std::hex << std::setw(width) << std::setfill('0') << (int)c << '}';
+    } else if ((c == '\\') || (c == '"')) {
+      result << "\\" << c;
+    } else {
+      result << c;   // anything else "as is"
+    }
+  }
+  result << "\"";
+
+  return result.str();
+}
+
+void t_haxe_generator::generate_deprecation_attribute(ostream& out, t_function* func, bool as_comment)
+{
+  auto iter = func->annotations_.find("deprecated");
+  if( func->annotations_.end() != iter) {
+    if( as_comment) {
+      out << indent() << "// DEPRECATED";
+    } else {
+      out << indent() << "@:deprecated";
+    }
+
+    // empty annotation values end up with "1" somewhere, ignore these as well
+    if ((iter->second.back().length() > 0) && (iter->second.back() != "1")) {
+      string text = make_haxe_string_literal(iter->second.back());
+      if( as_comment) {
+        out << ": " << text;
+      } else {
+        out << "(" << text << ")";
+      }
+    }
+
+    out << endl;
+  }
+}
+
 /**
  * Generates a service interface definition.
  *