THRIFT-5442 Separate client service calls into send/recv methods and make them public

Client: netstd
Patch: Jens Geyer
This commit is contained in:
Jens Geyer 2021-07-23 22:55:31 +02:00 committed by Jens Geyer
parent fb1d50dfc5
commit 47bf0e46e7
2 changed files with 91 additions and 63 deletions

View File

@ -2014,9 +2014,10 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
<< indent() << "{" << endl
<< indent() << "}" << endl
<< endl
<< indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)"
<< indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)" << endl
<< indent() << "{" << endl
<< indent() << "}" << endl;
<< indent() << "}" << endl
<< endl;
vector<t_function*> functions = tservice->get_functions();
vector<t_function*>::const_iterator functions_iterator;
@ -2030,6 +2031,23 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
out << indent() << "public async " << function_signature_async(*functions_iterator, "") << endl
<< indent() << "{" << endl;
indent_up();
out << indent() << "await send_" << function_name << "(";
string call_args = argument_list((*functions_iterator)->get_arglist(),false);
if(! call_args.empty()) {
out << call_args << ", ";
}
out << "cancellationToken);" << endl;
if(! (*functions_iterator)->is_oneway()) {
out << indent() << ((*functions_iterator)->get_returntype()->is_void() ? "" : "return ")
<< "await recv_" << function_name << "(cancellationToken);" << endl;
}
indent_down();
out << indent() << "}" << endl << endl;
// async send
out << indent() << "public async " << function_signature_async(*functions_iterator, "send_", MODE_NO_RETURN) << endl
<< indent() << "{" << endl;
indent_up();
string tmpvar = tmp("tmp");
string argsname = (*functions_iterator)->get_name() + "Args";
@ -2061,8 +2079,16 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
<< indent() << "await OutputProtocol.WriteMessageEndAsync(cancellationToken);" << endl
<< indent() << "await OutputProtocol.Transport.FlushAsync(cancellationToken);" << endl;
indent_down();
out << indent() << "}" << endl << endl;
if (!(*functions_iterator)->is_oneway())
{
// async recv
out << indent() << "public async " << function_signature_async(*functions_iterator, "recv_", MODE_NO_ARGS) << endl
<< indent() << "{" << endl;
indent_up();
string resultname = (*functions_iterator)->get_name() + "Result";
t_struct noargs(program_);
t_struct* xs = (*functions_iterator)->get_xceptions();
@ -2111,11 +2137,7 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
out << indent() << "}" << endl;
}
if ((*functions_iterator)->get_returntype()->is_void())
{
out << indent() << "return;" << endl;
}
else
if (!(*functions_iterator)->get_returntype()->is_void())
{
out << indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \""
<< function_name << " failed: unknown result\");" << endl;
@ -2125,11 +2147,6 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi
indent_down();
out << indent() << "}" << endl << endl;
}
else
{
indent_down();
out << indent() << "}" << endl;
}
cleanup_member_name_mapping(arg_struct);
}
@ -3384,28 +3401,30 @@ string t_netstd_generator::function_signature(t_function* tfunction, string pref
return type_name(ttype) + " " + func_name(normalize_name(prefix + tfunction->get_name())) + "(" + argument_list(tfunction->get_arglist()) + ")";
}
string t_netstd_generator::function_signature_async(t_function* tfunction, string prefix)
string t_netstd_generator::function_signature_async(t_function* tfunction, string prefix, int mode)
{
t_type* ttype = tfunction->get_returntype();
string task = "global::System.Threading.Tasks.Task";
if (!ttype->is_void())
if ((!ttype->is_void()) && ((mode & MODE_NO_RETURN) == 0))
{
task += "<" + type_name(ttype) + ">";
}
string result = task + " " + func_name(normalize_name(prefix + tfunction->get_name()) + (add_async_postfix ? "Async" : "")) + "(";
string args = argument_list(tfunction->get_arglist());
result += args;
if (!args.empty())
{
result += ", ";
if((mode & MODE_NO_ARGS) == 0) {
result += args;
if (!args.empty())
{
result += ", ";
}
}
result += "CancellationToken cancellationToken = default)";
return result;
}
string t_netstd_generator::argument_list(t_struct* tstruct)
string t_netstd_generator::argument_list(t_struct* tstruct, bool with_types)
{
string result = "";
const vector<t_field*>& fields = tstruct->get_members();
@ -3421,7 +3440,12 @@ string t_netstd_generator::argument_list(t_struct* tstruct)
{
result += ", ";
}
result += type_name((*f_iter)->get_type()) + " " + normalize_name((*f_iter)->get_name());
if( with_types) {
result += type_name((*f_iter)->get_type()) + " ";
}
result += normalize_name((*f_iter)->get_name());
}
return result;
}

View File

@ -131,12 +131,16 @@ public:
string netstd_type_usings() const;
string netstd_thrift_usings() const;
static const int MODE_FULL_DECL = 0x00;
static const int MODE_NO_RETURN = 0x01;
static const int MODE_NO_ARGS = 0x02;
string type_name(t_type* ttype);
string base_type_name(t_base_type* tbase);
string declare_field(t_field* tfield, bool init = false, string prefix = "");
string function_signature_async(t_function* tfunction, string prefix = "");
string function_signature_async(t_function* tfunction, string prefix = "", int mode = MODE_FULL_DECL);
string function_signature(t_function* tfunction, string prefix = "");
string argument_list(t_struct* tstruct);
string argument_list(t_struct* tstruct, bool with_types = true);
string type_to_enum(t_type* ttype);
string prop_name(t_field* tfield, bool suppress_mapping = false);
string func_name(t_function* tfunc, bool suppress_mapping = false);