From: Mark Slee Date: Tue, 10 Jul 2007 16:48:37 +0000 (+0000) Subject: Add typecasting to REST parameters in generated Thrift PHP code X-Git-Tag: 0.2.0~1311 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=b1ad0141d4df4b4488882880a6d9d4aa84f3b178;p=common%2Fthrift.git Add typecasting to REST parameters in generated Thrift PHP code Trac Bug: #4360 Reviewed By: ari Test Plan: Honky tonk, view the new generated api_10/x packages git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665161 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index d8db9fa3..372d1c02 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -913,9 +913,10 @@ void t_php_generator::generate_service_rest(t_service* tservice) { while (atype->is_typedef()) { atype = ((t_typedef*)atype)->get_type(); } + string cast = type_to_cast(atype); string req = "$request['" + (*a_iter)->get_name() + "']"; f_service_ << - indent() << "$" << (*a_iter)->get_name() << " = isset(" << req << ") ? " << req << " : null;" << endl; + indent() << "$" << (*a_iter)->get_name() << " = isset(" << req << ") ? " << cast << req << " : null;" << endl; if (atype->is_string() && ((t_base_type*)atype)->is_string_list()) { f_service_ << @@ -1796,6 +1797,33 @@ string t_php_generator::argument_list(t_struct* tstruct) { return result; } +/** + * Gets a typecast string for a particular type. + */ +string t_php_generator::type_to_cast(t_type* type) { + if (type->is_base_type()) { + t_base_type* btype = (t_base_type*)type; + switch (btype->get_base()) { + case t_base_type::TYPE_BOOL: + return "(bool)"; + case t_base_type::TYPE_BYTE: + case t_base_type::TYPE_I16: + case t_base_type::TYPE_I32: + case t_base_type::TYPE_I64: + return "(int)"; + case t_base_type::TYPE_DOUBLE: + return "(double)"; + case t_base_type::TYPE_STRING: + return "(string)"; + default: + return ""; + } + } else if (type->is_enum()) { + return "(int)"; + } + return ""; +} + /** * Converts the parse type to a C++ enum string for the given type. */ diff --git a/compiler/cpp/src/generate/t_php_generator.h b/compiler/cpp/src/generate/t_php_generator.h index 1df23ec3..16aac870 100644 --- a/compiler/cpp/src/generate/t_php_generator.h +++ b/compiler/cpp/src/generate/t_php_generator.h @@ -135,6 +135,7 @@ class t_php_generator : public t_oop_generator { std::string declare_field(t_field* tfield, bool init=false, bool obj=false); std::string function_signature(t_function* tfunction, std::string prefix=""); std::string argument_list(t_struct* tstruct); + std::string type_to_cast(t_type* ttype); std::string type_to_enum(t_type* ttype); std::string php_namespace(t_program* p) {