Skip to content

Commit

Permalink
THRIFT-3037: Support Struct Typedefs in Go Codegen
Browse files Browse the repository at this point in the history
Client: Go
  • Loading branch information
ethan-gallant committed Nov 27, 2023
1 parent 8a6bcc7 commit b6ebd28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
31 changes: 18 additions & 13 deletions compiler/cpp/src/thrift/generate/t_go_generator.cc
Expand Up @@ -766,7 +766,13 @@ void t_go_generator::generate_typedef(t_typedef* ttypedef) {
return;
}

f_types_ << "type " << new_type_name << " " << base_type << endl << endl;
std::string base_type_name = publicize(ttypedef->get_type()->get_name());
if (ttypedef->get_type()->is_base_type()) {
base_type_name = base_type;
}

f_types_ << "type " << new_type_name << " = " << base_type_name << endl << endl;

// Generate a convenience function that converts an instance of a type
// (which may be a constant) into a pointer to an instance of a type.
f_types_ << "func " << new_type_name << "Ptr(v " << new_type_name << ") *" << new_type_name
Expand Down Expand Up @@ -1175,8 +1181,9 @@ void t_go_generator::get_publicized_name_and_def_value(t_field* tfield,

void t_go_generator::generate_go_struct_initializer(ostream& out,
t_struct* tstruct,
bool is_args_or_result) {
out << publicize(type_name(tstruct), is_args_or_result) << "{";
bool is_args_or_result,
string alias_name) {
out << publicize((alias_name != "") ? alias_name : type_name(tstruct), is_args_or_result) << "{";
const vector<t_field*>& members = tstruct->get_members();
for (auto member : members) {
bool pointer_field = is_pointer_field(member);
Expand Down Expand Up @@ -3049,7 +3056,8 @@ void t_go_generator::generate_deserialize_field(ostream& out,
(t_struct*)type,
is_pointer_field(tfield, in_container_value),
declare,
name);
name,
(orig_type->is_typedef()) ? orig_type->get_name() : "");
} else if (type->is_container()) {
generate_deserialize_container(out, orig_type, is_pointer_field(tfield), declare, name);
} else if (type->is_base_type() || type->is_enum()) {
Expand Down Expand Up @@ -3150,11 +3158,12 @@ void t_go_generator::generate_deserialize_struct(ostream& out,
t_struct* tstruct,
bool pointer_field,
bool declare,
string prefix) {
string prefix,
string alias_name) {
string eq(declare ? " := " : " = ");

out << indent() << prefix << eq << (pointer_field ? "&" : "");
generate_go_struct_initializer(out, tstruct);
generate_go_struct_initializer(out, tstruct, false, alias_name);
out << indent() << "if err := " << prefix << "." << read_method_name_ << "(ctx, iprot); err != nil {" << endl;
out << indent() << " return thrift.PrependError(fmt.Sprintf(\"%T error reading struct: \", "
<< prefix << "), err)" << endl;
Expand Down Expand Up @@ -3921,13 +3930,11 @@ string t_go_generator::type_to_go_type(t_type* type) {
* Converts the parse type to a go type, taking into account whether the field
* associated with the type is T_OPTIONAL.
*/
string t_go_generator::type_to_go_type_with_opt(t_type* type,
string t_go_generator::type_to_go_type_with_opt(t_type* ttype,
bool optional_field) {
string maybe_pointer(optional_field ? "*" : "");

if (type->is_typedef() && ((t_typedef*)type)->is_forward_typedef()) {
type = ((t_typedef*)type)->get_true_type();
}
t_type* type = get_true_type(ttype);

if (type->is_base_type()) {
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
Expand Down Expand Up @@ -3970,7 +3977,7 @@ string t_go_generator::type_to_go_type_with_opt(t_type* type,
} else if (type->is_enum()) {
return maybe_pointer + publicize(type_name(type));
} else if (type->is_struct() || type->is_xception()) {
return "*" + publicize(type_name(type));
return "*" + publicize(type_name(ttype));
} else if (type->is_map()) {
t_map* t = (t_map*)type;
string keyType = type_to_go_key_type(t->get_key_type());
Expand All @@ -3984,8 +3991,6 @@ string t_go_generator::type_to_go_type_with_opt(t_type* type,
t_list* t = (t_list*)type;
string elemType = type_to_go_type(t->get_elem_type());
return maybe_pointer + string("[]") + elemType;
} else if (type->is_typedef()) {
return maybe_pointer + publicize(type_name(type));
}

throw "INVALID TYPE IN type_to_go_type: " + type->get_name();
Expand Down
6 changes: 4 additions & 2 deletions compiler/cpp/src/thrift/generate/t_go_generator.h
Expand Up @@ -124,7 +124,8 @@ class t_go_generator : public t_generator {
bool is_args = false);
void generate_go_struct_initializer(std::ostream& out,
t_struct* tstruct,
bool is_args_or_result = false);
bool is_args_or_result = false,
string alias_name = "");
void generate_isset_helpers(std::ostream& out,
t_struct* tstruct,
const string& tstruct_name,
Expand Down Expand Up @@ -176,7 +177,8 @@ class t_go_generator : public t_generator {
t_struct* tstruct,
bool is_pointer_field,
bool declare,
std::string prefix = "");
std::string prefix = "",
string alias_name = "");

void generate_deserialize_container(std::ostream& out,
t_type* ttype,
Expand Down

0 comments on commit b6ebd28

Please sign in to comment.