@@ -766,7 +766,12 @@ void t_go_generator::generate_typedef(t_typedef* ttypedef) {
766
766
return ;
767
767
}
768
768
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
+
770
775
// Generate a convenience function that converts an instance of a type
771
776
// (which may be a constant) into a pointer to an instance of a type.
772
777
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,
1175
1180
1176
1181
void t_go_generator::generate_go_struct_initializer (ostream& out,
1177
1182
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) << " {" ;
1180
1186
const vector<t_field*>& members = tstruct->get_members ();
1181
1187
for (auto member : members) {
1182
1188
bool pointer_field = is_pointer_field (member);
@@ -2084,7 +2090,8 @@ void t_go_generator::generate_service_client(t_service* tservice) {
2084
2090
f_types_ << indent () << " }" << endl << endl;
2085
2091
}
2086
2092
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) {
2088
2095
// Check if the result is nil, which likely means we have a new
2089
2096
// exception added but unknown to the client yet
2090
2097
// (e.g. client hasn't updated the thrift file).
@@ -3037,7 +3044,7 @@ void t_go_generator::generate_deserialize_field(ostream& out,
3037
3044
(void )inclass;
3038
3045
(void )coerceData;
3039
3046
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 ();
3041
3048
string name (prefix + publicize (tfield->get_name ()));
3042
3049
3043
3050
if (type->is_void ()) {
@@ -3049,7 +3056,8 @@ void t_go_generator::generate_deserialize_field(ostream& out,
3049
3056
(t_struct*)type,
3050
3057
is_pointer_field (tfield, in_container_value),
3051
3058
declare,
3052
- name);
3059
+ name,
3060
+ (orig_type->is_typedef ()) ? orig_type->get_name () : " " );
3053
3061
} else if (type->is_container ()) {
3054
3062
generate_deserialize_container (out, orig_type, is_pointer_field (tfield), declare, name);
3055
3063
} else if (type->is_base_type () || type->is_enum ()) {
@@ -3150,11 +3158,12 @@ void t_go_generator::generate_deserialize_struct(ostream& out,
3150
3158
t_struct* tstruct,
3151
3159
bool pointer_field,
3152
3160
bool declare,
3153
- string prefix) {
3161
+ string prefix,
3162
+ string alias_name) {
3154
3163
string eq (declare ? " := " : " = " );
3155
3164
3156
3165
out << indent () << prefix << eq << (pointer_field ? " &" : " " );
3157
- generate_go_struct_initializer (out, tstruct);
3166
+ generate_go_struct_initializer (out, tstruct, false , alias_name );
3158
3167
out << indent () << " if err := " << prefix << " ." << read_method_name_ << " (ctx, iprot); err != nil {" << endl;
3159
3168
out << indent () << " return thrift.PrependError(fmt.Sprintf(\" %T error reading struct: \" , "
3160
3169
<< prefix << " ), err)" << endl;
@@ -3921,15 +3930,16 @@ string t_go_generator::type_to_go_type(t_type* type) {
3921
3930
* Converts the parse type to a go type, taking into account whether the field
3922
3931
* associated with the type is T_OPTIONAL.
3923
3932
*/
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 ,
3925
3934
bool optional_field) {
3926
3935
string maybe_pointer (optional_field ? " *" : " " );
3927
3936
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);
3931
3938
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
+ }
3933
3943
t_base_type::t_base tbase = ((t_base_type*)type)->get_base ();
3934
3944
3935
3945
switch (tbase) {
@@ -3970,7 +3980,7 @@ string t_go_generator::type_to_go_type_with_opt(t_type* type,
3970
3980
} else if (type->is_enum ()) {
3971
3981
return maybe_pointer + publicize (type_name (type));
3972
3982
} else if (type->is_struct () || type->is_xception ()) {
3973
- return " *" + publicize (type_name (type ));
3983
+ return " *" + publicize (type_name (ttype ));
3974
3984
} else if (type->is_map ()) {
3975
3985
t_map* t = (t_map*)type;
3976
3986
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,
3985
3995
string elemType = type_to_go_type (t->get_elem_type ());
3986
3996
return maybe_pointer + string (" []" ) + elemType;
3987
3997
} else if (type->is_typedef ()) {
3988
- return maybe_pointer + publicize (type_name (type ));
3998
+ return maybe_pointer + publicize (type_name (ttype ));
3989
3999
}
3990
4000
3991
4001
throw " INVALID TYPE IN type_to_go_type: " + type->get_name ();
0 commit comments