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 d8db9fa..372d1c0 100644
--- a/compiler/cpp/src/generate/t_php_generator.cc
+++ b/compiler/cpp/src/generate/t_php_generator.cc
@@ -913,9 +913,10 @@
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_ <<
@@ -1797,6 +1798,33 @@
}
/**
+ * 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.
*/
string t_php_generator ::type_to_enum(t_type* type) {
diff --git a/compiler/cpp/src/generate/t_php_generator.h b/compiler/cpp/src/generate/t_php_generator.h
index 1df23ec..16aac87 100644
--- a/compiler/cpp/src/generate/t_php_generator.h
+++ b/compiler/cpp/src/generate/t_php_generator.h
@@ -135,6 +135,7 @@
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) {