@@ -114,7 +114,7 @@ class t_go_generator : public t_generator {
114
114
void generate_xception (t_struct* txception);
115
115
void generate_service (t_service* tservice);
116
116
117
- std::string render_const_value (t_type* type, t_const_value* value, const string& name);
117
+ std::string render_const_value (t_type* type, t_const_value* value, const string& name, bool is_optional = false );
118
118
119
119
/* *
120
120
* Struct generation code
@@ -942,46 +942,74 @@ void t_go_generator::generate_const(t_const* tconst) {
942
942
* is NOT performed in this function as it is always run beforehand using the
943
943
* validate_types method in main.cc
944
944
*/
945
- string t_go_generator::render_const_value (t_type* type, t_const_value* value, const string& name) {
945
+ string t_go_generator::render_const_value (t_type* type, t_const_value* value, const string& name, bool is_optional ) {
946
946
type = get_true_type (type);
947
947
std::ostringstream out;
948
948
949
949
if (type->is_base_type ()) {
950
950
t_base_type::t_base tbase = ((t_base_type*)type)->get_base ();
951
+ std::ostringstream go_value;
952
+ std::string ptr_method;
953
+
954
+ if (is_optional) {
955
+ out << " thrift." ;
956
+ }
957
+
951
958
952
959
switch (tbase) {
953
960
case t_base_type::TYPE_STRING:
954
961
if (((t_base_type*)type)->is_binary ()) {
955
- out << " []byte(\" " << get_escaped_string (value) << " \" )" ;
962
+ ptr_method = " ByteSlicePtr" ;
963
+ go_value << " []byte(\" " << get_escaped_string (value) << " \" )" ;
956
964
} else {
957
- out << ' "' << get_escaped_string (value) << ' "' ;
965
+ ptr_method = " StringPtr" ;
966
+ go_value << ' "' << get_escaped_string (value) << ' "' ;
958
967
}
959
968
960
969
break ;
961
970
962
971
case t_base_type::TYPE_BOOL:
963
- out << (value->get_integer () > 0 ? " true" : " false" );
972
+ ptr_method = " BoolPtr" ;
973
+ go_value << (value->get_integer () > 0 ? " true" : " false" );
964
974
break ;
965
975
966
976
case t_base_type::TYPE_BYTE:
977
+ ptr_method = " Int8Ptr" ;
967
978
case t_base_type::TYPE_I16:
979
+ ptr_method = " Int16Ptr" ;
968
980
case t_base_type::TYPE_I32:
981
+ ptr_method = " Int32Ptr" ;
969
982
case t_base_type::TYPE_I64:
970
- out << value->get_integer ();
983
+ if (ptr_method == " " ) {
984
+ ptr_method = " Int64Ptr" ;
985
+ }
986
+
987
+ go_value << value->get_integer ();
971
988
break ;
972
989
973
990
case t_base_type::TYPE_DOUBLE:
991
+ ptr_method = " Float64Ptr" ;
974
992
if (value->get_type () == t_const_value::CV_INTEGER) {
975
- out << value->get_integer ();
993
+ go_value << value->get_integer ();
976
994
} else {
977
- out << value->get_double ();
995
+ go_value << value->get_double ();
978
996
}
979
997
980
998
break ;
981
999
982
1000
default :
983
1001
throw " compiler error: no const of base type " + t_base_type::t_base_name (tbase);
984
1002
}
1003
+
1004
+ if (is_optional) {
1005
+ out << ptr_method << " (" ;
1006
+ }
1007
+
1008
+ out << go_value.str ();
1009
+
1010
+ if (is_optional) {
1011
+ out << " )" ;
1012
+ }
985
1013
} else if (type->is_enum ()) {
986
1014
indent (out) << value->get_integer ();
987
1015
} else if (type->is_struct () || type->is_xception ()) {
@@ -993,21 +1021,24 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co
993
1021
map<t_const_value*, t_const_value*>::const_iterator v_iter;
994
1022
995
1023
for (v_iter = val.begin (); v_iter != val.end (); ++v_iter) {
996
- t_type* field_type = NULL ;
1024
+ t_field* field = NULL ;
997
1025
998
1026
for (f_iter = fields.begin (); f_iter != fields.end (); ++f_iter) {
999
1027
if ((*f_iter)->get_name () == v_iter->first ->get_string ()) {
1000
- field_type = (*f_iter)->get_type ();
1028
+ field = *f_iter;
1029
+
1001
1030
}
1002
1031
}
1003
1032
1004
- if (field_type == NULL ) {
1033
+ if (field == NULL ) {
1005
1034
throw " type error: " + type->get_name () + " has no field " + v_iter->first ->get_string ();
1006
1035
}
1007
1036
1037
+ t_type* field_type = field->get_type ();
1038
+
1008
1039
if (field_type->is_base_type () || field_type->is_enum ()) {
1009
1040
out << endl << indent () << publicize (v_iter->first ->get_string ()) << " : "
1010
- << render_const_value (field_type, v_iter->second , name) << " ," ;
1041
+ << render_const_value (field_type, v_iter->second , name, field-> get_req () == t_field::e_req::T_OPTIONAL ) << " ," ;
1011
1042
} else {
1012
1043
string k (tmp (" k" ));
1013
1044
string v (tmp (" v" ));
@@ -1017,7 +1048,7 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co
1017
1048
}
1018
1049
}
1019
1050
1020
- out << " }" ;
1051
+ out << endl << " }" ;
1021
1052
1022
1053
indent_down ();
1023
1054
} else if (type->is_map ()) {
0 commit comments