BytedanceRPC commented on a change in pull request #2307:
URL: https://github.com/apache/thrift/pull/2307#discussion_r561561729



##########
File path: compiler/cpp/src/thrift/generate/t_go_generator.cc
##########
@@ -1847,6 +1862,48 @@ void t_go_generator::generate_go_struct_writer(ostream& 
out,
   }
 }
 
+void t_go_generator::generate_go_struct_equals(ostream& out,
+                                               t_struct* tstruct,
+                                               const string& tstruct_name,
+                                               bool is_args_or_result) {
+  if (is_args_or_result) {
+    return;
+  }
+  string name(tstruct->get_name());
+  const vector<t_field*>& fields = tstruct->get_sorted_members();
+  vector<t_field*>::const_iterator f_iter;
+  indent(out) << "func (p *" << tstruct_name << ") " << equals_method_name_ << 
"(other *"
+              << tstruct_name << ") bool {" << endl;
+  indent_up();
+
+  string field_name;
+  string publicize_field_name;
+  generate_go_equals_guard(out, "p", "other");
+
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    field_name = (*f_iter)->get_name();
+    t_type* field_type = (*f_iter)->get_type();
+    publicize_field_name = publicize(field_name);
+    string goType = type_to_go_type_with_opt(field_type, 
is_pointer_field(*f_iter));
+
+    string tgt = "p." + publicize_field_name;
+    string src = "other." + publicize_field_name;
+    t_type* ttype = field_type->get_true_type();
+    if (is_pointer_field(*f_iter)
+        && (ttype->is_base_type() || ttype->is_enum() || 
ttype->is_container())) {

Review comment:
       for struct and xception, since they have `Equals` method, there is no 
need to get their value

##########
File path: compiler/cpp/src/thrift/generate/t_go_generator.cc
##########
@@ -3439,6 +3511,123 @@ void 
t_go_generator::generate_serialize_list_element(ostream& out, t_list* tlist
   generate_serialize_field(out, &efield, prefix);
 }
 
+/**
+ * Compares any type
+ */
+void t_go_generator::generate_go_equals(std::ostream& out,
+                                           t_type* ori_type,
+                                           string tgt,
+                                           string src) {
+
+  t_type* ttype = get_true_type(ori_type);
+  // Do nothing for void types
+  if (ttype->is_void()) {
+    throw "compiler error: cannot generate equals for void type: " + tgt;
+  }
+
+  if (ttype->is_struct() || ttype->is_xception()) {
+    generate_go_equals_struct(out, ttype, tgt, src);
+  } else if (ttype->is_container()) {
+    generate_go_equals_container(out, ttype, tgt, src);
+  } else if (ttype->is_base_type() || ttype->is_enum()) {
+    out << indent() << "if ";
+    if (ttype->is_base_type()) {
+      t_base_type::t_base tbase = ((t_base_type*)ttype)->get_base();
+      switch (tbase) {
+      case t_base_type::TYPE_VOID:
+        throw "compiler error: cannot equals void: " + tgt;
+        break;
+
+      case t_base_type::TYPE_STRING:
+        if (ttype->is_binary()) {
+          out << "bytes.Compare(" << tgt << ", " << src << ") != 0";
+        } else {
+          out << tgt << " != " << src;
+        }
+        break;
+
+      case t_base_type::TYPE_BOOL:
+      case t_base_type::TYPE_I8:
+      case t_base_type::TYPE_I16:
+      case t_base_type::TYPE_I32:
+      case t_base_type::TYPE_I64:
+      case t_base_type::TYPE_DOUBLE:
+        out << tgt << " != " << src;
+        break;
+
+      default:
+        throw "compiler error: no Go name for base type " + 
t_base_type::t_base_name(tbase);
+      }
+    } else if (ttype->is_enum()) {
+      out << tgt << " != " << src;
+    }
+
+    out << " { return false }" << endl;
+  } else {
+    throw "compiler error: Invalid type in generate_go_equals '" + 
ttype->get_name() + "' for '"
+        + tgt + "'";
+  }
+}
+
+void t_go_generator::generate_go_equals_guard(std::ostream& out, string tgt, 
string src) {
+  out << indent() << "if " << tgt << " == " << src << " {" << endl;
+  indent_up();
+  out << indent() << "return true" << endl;
+  indent_down();
+  out << indent() << "} else if " << tgt << " == nil || " << src << " == nil 
{" << endl;
+  indent_up();
+  out << indent() << "return false" << endl;
+  indent_down();
+  out << indent() << "}" << endl;
+}
+
+/**
+ * Compares the members of a struct
+ */
+void t_go_generator::generate_go_equals_struct(std::ostream& out,
+                                                  t_type* ttype,
+                                                  string tgt,
+                                                  string src) {
+  (void)ttype;
+  out << indent() << "if !" << tgt << "." << equals_method_name_ << "(" << src
+      << ") { return false }" << endl;
+}
+
+/**
+ * Compares any container type
+ */
+void t_go_generator::generate_go_equals_container(std::ostream& out,

Review comment:
       Good idea, will do




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to