fishy commented on a change in pull request #2307:
URL: https://github.com/apache/thrift/pull/2307#discussion_r560383440
##########
File path: compiler/cpp/src/thrift/generate/t_go_generator.cc
##########
@@ -1847,6 +1860,56 @@ void t_go_generator::generate_go_struct_writer(ostream&
out,
}
}
+void t_go_generator::generate_go_struct_deepequal(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 << ") " << deepequal_method_name_
<< "(other *"
+ << tstruct_name << ") bool {" << endl;
+ indent_up();
+
+ string field_name;
+ string publicize_field_name;
+ int32_t field_id = -1;
+ out << indent() << "if p == other {" << endl;
+ indent_up();
+ out << indent() << "return true" << endl;
+ indent_down();
+ out << indent() << "} else if p == nil || other == nil {" << endl;
+ indent_up();
+ out << indent() << "return false" << endl;
+ indent_down();
+ out << indent() << "}" << endl;
+
+ 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())) {
+ tgt = "(*" + tgt + ")";
+ src = "(*" + src + ")";
Review comment:
1. wrong indentation
2. This generates code like this for optional fields:
```go
if (*p.FieldN) != (*other.FieldN) { return false }
```
this will cause panic when the field is not set.
----------------------------------------------------------------
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]