You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2020/09/25 20:07:31 UTC

[thrift] branch master updated: THRIFT-4942 Set PHP struct generated field values as private with getters and setters Client: php Patch: Das Dedipyaman

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

jensg 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 c553155  THRIFT-4942 Set PHP struct generated field values as private with getters and setters Client: php Patch: Das Dedipyaman
c553155 is described below

commit c553155af8330a54212eefefaf877b1725a449bc
Author: Dedipyaman Das <30...@users.noreply.github.com>
AuthorDate: Sat Sep 19 23:20:46 2020 +0200

    THRIFT-4942 Set PHP struct generated field values as private with getters and setters
    Client: php
    Patch: Das Dedipyaman
    
    This closes #1859
    This closes #2238
---
 .../cpp/src/thrift/generate/t_php_generator.cc     | 95 +++++++++++++++++++++-
 1 file changed, 94 insertions(+), 1 deletion(-)

diff --git a/compiler/cpp/src/thrift/generate/t_php_generator.cc b/compiler/cpp/src/thrift/generate/t_php_generator.cc
index c327854..10dc570 100644
--- a/compiler/cpp/src/thrift/generate/t_php_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_php_generator.cc
@@ -61,6 +61,8 @@ public:
     oop_ = false;
     validate_ = false;
     json_serializable_ = false;
+    getters_setters_ = false;
+	    
     nsglobal_ = ""; // by default global namespace is empty
     classmap_ = false;
     for (iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
@@ -86,6 +88,8 @@ public:
         } else {
           pwarning(0, "psr4 is default option! needn't add psr4 option!\n");
         }
+      } else if (iter->first.compare("getters_setters") == 0) {
+        getters_setters_ = true;
       } else {
         throw "unknown option php:" + iter->first;
       }
@@ -150,6 +154,13 @@ public:
 
   void generate_php_type_spec(std::ostream& out, t_type* t);
   void generate_php_struct_spec(std::ostream& out, t_struct* tstruct);
+  void generate_generic_field_getters_setters(std::ostream& out, t_struct* tstruct);
+
+  void generate_reflection_setters(ostringstream& out, string field_name, string cap_name);
+  void generate_reflection_getters(ostringstream& out, string field_name, string cap_name);
+
+  std::string get_cap_name(std::string name);
+
 
   /**
    * Service-level generation functions
@@ -400,6 +411,11 @@ private:
    * Global namespace for PHP 5.3
    */
   std::string nsglobal_;
+
+  /**
+   * Whether to generate getters and setters
+   */
+  bool getters_setters_;
 };
 
 bool t_php_generator::is_valid_namespace(const std::string& sub_namespace) {
@@ -808,7 +824,80 @@ void t_php_generator::generate_php_struct_spec(ostream& out, t_struct* tstruct)
   indent_down();
   indent(out) << ");" << endl << endl;
 }
+/**
+ * Generates necessary accessors and mutators for the fields
+ */
+void t_php_generator::generate_generic_field_getters_setters(std::ostream& out,
+                                                              t_struct* tstruct) {
+  std::ostringstream getter_stream;
+  std::ostringstream setter_stream;
+
+  // build up the bodies of both the getter and setter at once
+  const vector<t_field*>& fields = tstruct->get_members();
+  vector<t_field*>::const_iterator f_iter;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    t_field* field = *f_iter;
+    std::string field_name = field->get_name();
+    std::string cap_name = get_cap_name(field_name);
+
+    indent_up();
+    generate_reflection_setters(setter_stream, field_name, cap_name);
+    generate_reflection_getters(getter_stream, field_name, cap_name);
+    indent_down();
+  }
+
+  indent(out) << endl;
+  out << getter_stream.str();
+  out << setter_stream.str();
+  indent(out) << endl;
+}
+/**
+ * Generates a getter for the generated private fields
+ */
+void t_php_generator::generate_reflection_getters(ostringstream& out,
+                                                   string field_name,
+                                                   string cap_name) {
+
+
+  out << indent() << "public function " << "get" << cap_name << "()" << endl
+      << indent() << "{" << endl;
+
+  indent_up();
+
+  out << indent() << "return $this->" << field_name << ";" << endl;
+
+  indent_down();
+  out << indent() << "}" << endl;
+  out << endl;
+}
+/**
+ * Generates a setter for the generated private fields
+ */
+void t_php_generator::generate_reflection_setters(ostringstream& out,
+						  string field_name,
+						  string cap_name) {
+
+  out << indent() << "public function set" << cap_name << "(" << "$" << field_name << ")" << endl
+      << indent() << "{" << endl;
+
+  indent_up();
+
+  out << indent() << "$this->" << field_name << " = $" << field_name << ";" << endl;
+
 
+  indent_down();
+  out << indent() << "}" << endl;
+  out << endl;
+}
+/**
+ * Gets the first-letter capitalized name for the field
+ *
+ * @param std::string name of the field
+ */
+std::string t_php_generator::get_cap_name(std::string name) {
+  name[0] = toupper(name[0]);
+  return name;
+}
 /**
  * Generates a struct definition for a thrift data type. This is nothing in PHP
  * where the objects are all just associative arrays (unless of course we
@@ -901,6 +990,9 @@ void t_php_generator::generate_php_struct_definition(ostream& out,
   out << indent() << "}" << endl << endl;
 
   out << endl;
+  if (getters_setters_) {
+    generate_generic_field_getters_setters(out, tstruct);
+  }
   generate_php_struct_reader(out, tstruct, is_result);
   out << endl;
   generate_php_struct_writer(out, tstruct, is_result);
@@ -2782,4 +2874,5 @@ THRIFT_REGISTER_GENERATOR(
     "    rest:            Generate PHP REST processors\n"
     "    nsglobal=NAME:   Set global namespace\n"
     "    validate:        Generate PHP validator methods\n"
-    "    json:            Generate JsonSerializable classes (requires PHP >= 5.4)\n")
+    "    json:            Generate JsonSerializable classes (requires PHP >= 5.4)\n"
+    "    getters_setters: Generate Getters and Setters for struct variables\n")