Skip to content

Commit 12d46f3

Browse files
compiler/cpp/src/generate/t_go_generator: Fix the render_const_value implementation
1 parent ad51db7 commit 12d46f3

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

Makefile.am

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ endif
3232
include_types_knowndir = ${TYPES_PREFIX}/types/known
3333
include_types_known_HEADERS = types/known/*.thrift
3434

35+
include_types_knowndir = ${TYPES_PREFIX}/types/annotation
36+
include_types_known_HEADERS = types/annotation/*.thrift
37+
3538
include_typesdir = ${TYPES_PREFIX}/types
3639
include_types_HEADERS = types/*.thrift
3740

compiler/cpp/src/generate/t_go_generator.cc

+44-13
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class t_go_generator : public t_generator {
114114
void generate_xception(t_struct* txception);
115115
void generate_service(t_service* tservice);
116116

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);
118118

119119
/**
120120
* Struct generation code
@@ -942,46 +942,74 @@ void t_go_generator::generate_const(t_const* tconst) {
942942
* is NOT performed in this function as it is always run beforehand using the
943943
* validate_types method in main.cc
944944
*/
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) {
946946
type = get_true_type(type);
947947
std::ostringstream out;
948948

949949
if (type->is_base_type()) {
950950
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+
951958

952959
switch (tbase) {
953960
case t_base_type::TYPE_STRING:
954961
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) << "\")";
956964
} else {
957-
out << '"' << get_escaped_string(value) << '"';
965+
ptr_method = "StringPtr";
966+
go_value << '"' << get_escaped_string(value) << '"';
958967
}
959968

960969
break;
961970

962971
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");
964974
break;
965975

966976
case t_base_type::TYPE_BYTE:
977+
ptr_method = "Int8Ptr";
967978
case t_base_type::TYPE_I16:
979+
ptr_method = "Int16Ptr";
968980
case t_base_type::TYPE_I32:
981+
ptr_method = "Int32Ptr";
969982
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();
971988
break;
972989

973990
case t_base_type::TYPE_DOUBLE:
991+
ptr_method = "Float64Ptr";
974992
if (value->get_type() == t_const_value::CV_INTEGER) {
975-
out << value->get_integer();
993+
go_value << value->get_integer();
976994
} else {
977-
out << value->get_double();
995+
go_value << value->get_double();
978996
}
979997

980998
break;
981999

9821000
default:
9831001
throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
9841002
}
1003+
1004+
if (is_optional) {
1005+
out << ptr_method << "(";
1006+
}
1007+
1008+
out << go_value.str();
1009+
1010+
if (is_optional) {
1011+
out << ")";
1012+
}
9851013
} else if (type->is_enum()) {
9861014
indent(out) << value->get_integer();
9871015
} 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
9931021
map<t_const_value*, t_const_value*>::const_iterator v_iter;
9941022

9951023
for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
996-
t_type* field_type = NULL;
1024+
t_field* field = NULL;
9971025

9981026
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
9991027
if ((*f_iter)->get_name() == v_iter->first->get_string()) {
1000-
field_type = (*f_iter)->get_type();
1028+
field = *f_iter;
1029+
10011030
}
10021031
}
10031032

1004-
if (field_type == NULL) {
1033+
if (field == NULL) {
10051034
throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
10061035
}
10071036

1037+
t_type* field_type = field->get_type();
1038+
10081039
if (field_type->is_base_type() || field_type->is_enum()) {
10091040
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) << ",";
10111042
} else {
10121043
string k(tmp("k"));
10131044
string v(tmp("v"));
@@ -1017,7 +1048,7 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co
10171048
}
10181049
}
10191050

1020-
out << "}";
1051+
out << endl << "}";
10211052

10221053
indent_down();
10231054
} else if (type->is_map()) {

lib/go/thrift/pointerize.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ package thrift
4141
func Float32Ptr(v float32) *float32 { return &v }
4242
func Float64Ptr(v float64) *float64 { return &v }
4343
func IntPtr(v int) *int { return &v }
44+
func Int8Ptr(v int8) *int8 { return &v }
45+
func Int16Ptr(v int16) *int16 { return &v }
4446
func Int32Ptr(v int32) *int32 { return &v }
4547
func Int64Ptr(v int64) *int64 { return &v }
4648
func StringPtr(v string) *string { return &v }

0 commit comments

Comments
 (0)