Skip to content

Commit cc2d009

Browse files
committed
THRIFT-3037: Support Struct Typedefs in Go Codegen
Client: Go fix indentation
1 parent 4115e95 commit cc2d009

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

compiler/cpp/src/thrift/generate/t_go_generator.cc

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,12 @@ void t_go_generator::generate_typedef(t_typedef* ttypedef) {
766766
return;
767767
}
768768

769-
f_types_ << "type " << new_type_name << " " << base_type << endl << endl;
769+
if (ttypedef->get_type()->is_base_type()) {
770+
f_types_ << "type " << new_type_name << " " << base_type << endl << endl;
771+
} else {
772+
f_types_ << "type " << new_type_name << " struct { " << base_type << " }" << endl << endl;
773+
}
774+
770775
// Generate a convenience function that converts an instance of a type
771776
// (which may be a constant) into a pointer to an instance of a type.
772777
f_types_ << "func " << new_type_name << "Ptr(v " << new_type_name << ") *" << new_type_name
@@ -1175,8 +1180,9 @@ void t_go_generator::get_publicized_name_and_def_value(t_field* tfield,
11751180

11761181
void t_go_generator::generate_go_struct_initializer(ostream& out,
11771182
t_struct* tstruct,
1178-
bool is_args_or_result) {
1179-
out << publicize(type_name(tstruct), is_args_or_result) << "{";
1183+
bool is_args_or_result,
1184+
string alias_name) {
1185+
out << publicize((alias_name != "") ? alias_name : type_name(tstruct), is_args_or_result) << "{";
11801186
const vector<t_field*>& members = tstruct->get_members();
11811187
for (auto member : members) {
11821188
bool pointer_field = is_pointer_field(member);
@@ -2084,7 +2090,8 @@ void t_go_generator::generate_service_client(t_service* tservice) {
20842090
f_types_ << indent() << "}" << endl << endl;
20852091
}
20862092

2087-
if ((*f_iter)->get_returntype()->is_struct()) {
2093+
bool is_alias = (*f_iter)->get_returntype()->is_typedef();
2094+
if ((*f_iter)->get_returntype()->is_struct() || is_alias) {
20882095
// Check if the result is nil, which likely means we have a new
20892096
// exception added but unknown to the client yet
20902097
// (e.g. client hasn't updated the thrift file).
@@ -3037,7 +3044,7 @@ void t_go_generator::generate_deserialize_field(ostream& out,
30373044
(void)inclass;
30383045
(void)coerceData;
30393046
t_type* orig_type = tfield->get_type();
3040-
t_type* type = get_true_type(orig_type);
3047+
t_type* type = orig_type->get_true_type();
30413048
string name(prefix + publicize(tfield->get_name()));
30423049

30433050
if (type->is_void()) {
@@ -3049,7 +3056,8 @@ void t_go_generator::generate_deserialize_field(ostream& out,
30493056
(t_struct*)type,
30503057
is_pointer_field(tfield, in_container_value),
30513058
declare,
3052-
name);
3059+
name,
3060+
(orig_type->is_typedef()) ? orig_type->get_name() : "");
30533061
} else if (type->is_container()) {
30543062
generate_deserialize_container(out, orig_type, is_pointer_field(tfield), declare, name);
30553063
} else if (type->is_base_type() || type->is_enum()) {
@@ -3150,11 +3158,12 @@ void t_go_generator::generate_deserialize_struct(ostream& out,
31503158
t_struct* tstruct,
31513159
bool pointer_field,
31523160
bool declare,
3153-
string prefix) {
3161+
string prefix,
3162+
string alias_name) {
31543163
string eq(declare ? " := " : " = ");
31553164

31563165
out << indent() << prefix << eq << (pointer_field ? "&" : "");
3157-
generate_go_struct_initializer(out, tstruct);
3166+
generate_go_struct_initializer(out, tstruct, false, alias_name);
31583167
out << indent() << "if err := " << prefix << "." << read_method_name_ << "(ctx, iprot); err != nil {" << endl;
31593168
out << indent() << " return thrift.PrependError(fmt.Sprintf(\"%T error reading struct: \", "
31603169
<< prefix << "), err)" << endl;
@@ -3921,15 +3930,16 @@ string t_go_generator::type_to_go_type(t_type* type) {
39213930
* Converts the parse type to a go type, taking into account whether the field
39223931
* associated with the type is T_OPTIONAL.
39233932
*/
3924-
string t_go_generator::type_to_go_type_with_opt(t_type* type,
3933+
string t_go_generator::type_to_go_type_with_opt(t_type* ttype,
39253934
bool optional_field) {
39263935
string maybe_pointer(optional_field ? "*" : "");
39273936

3928-
if (type->is_typedef() && ((t_typedef*)type)->is_forward_typedef()) {
3929-
type = ((t_typedef*)type)->get_true_type();
3930-
}
3937+
t_type* type = get_true_type(ttype);
39313938

3932-
if (type->is_base_type()) {
3939+
if (type->is_base_type()){
3940+
if (ttype->is_typedef() && ((t_typedef*)ttype)->is_forward_typedef()) {
3941+
return maybe_pointer + publicize(type_name(ttype));
3942+
}
39333943
t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
39343944

39353945
switch (tbase) {
@@ -3970,7 +3980,7 @@ string t_go_generator::type_to_go_type_with_opt(t_type* type,
39703980
} else if (type->is_enum()) {
39713981
return maybe_pointer + publicize(type_name(type));
39723982
} else if (type->is_struct() || type->is_xception()) {
3973-
return "*" + publicize(type_name(type));
3983+
return "*" + publicize(type_name(ttype));
39743984
} else if (type->is_map()) {
39753985
t_map* t = (t_map*)type;
39763986
string keyType = type_to_go_key_type(t->get_key_type());
@@ -3985,7 +3995,7 @@ string t_go_generator::type_to_go_type_with_opt(t_type* type,
39853995
string elemType = type_to_go_type(t->get_elem_type());
39863996
return maybe_pointer + string("[]") + elemType;
39873997
} else if (type->is_typedef()) {
3988-
return maybe_pointer + publicize(type_name(type));
3998+
return maybe_pointer + publicize(type_name(ttype));
39893999
}
39904000

39914001
throw "INVALID TYPE IN type_to_go_type: " + type->get_name();

compiler/cpp/src/thrift/generate/t_go_generator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ class t_go_generator : public t_generator {
124124
bool is_args = false);
125125
void generate_go_struct_initializer(std::ostream& out,
126126
t_struct* tstruct,
127-
bool is_args_or_result = false);
127+
bool is_args_or_result = false,
128+
string alias_name = "");
128129
void generate_isset_helpers(std::ostream& out,
129130
t_struct* tstruct,
130131
const string& tstruct_name,
@@ -176,7 +177,8 @@ class t_go_generator : public t_generator {
176177
t_struct* tstruct,
177178
bool is_pointer_field,
178179
bool declare,
179-
std::string prefix = "");
180+
std::string prefix = "",
181+
string alias_name = "");
180182

181183
void generate_deserialize_container(std::ostream& out,
182184
t_type* ttype,

0 commit comments

Comments
 (0)