diff options
author | Yuxuan 'fishy' Wang <yuxuan.wang@reddit.com> | 2021-07-29 15:59:10 -0700 |
---|---|---|
committer | Yuxuan 'fishy' Wang <fishywang@gmail.com> | 2021-07-30 08:47:45 -0700 |
commit | f6955351222f51e5662ce41de43c75b7c3e640e1 (patch) | |
tree | 75bd4608863e18904e3faea9a6fe0c08a8f26acf /compiler/cpp | |
parent | 68c0272a0af55f8a50296f5fa3ba672c08937d98 (diff) | |
download | thrift-f6955351222f51e5662ce41de43c75b7c3e640e1.tar.gz |
THRIFT-5389: Fix const generation for optional fields
Client: go
The current compiler will generate uncompilable code when we use
optional enum and/or typedef'd types in a thrift constant.
This fixes the issue, also adds a test for that.
Diffstat (limited to 'compiler/cpp')
-rw-r--r-- | compiler/cpp/src/thrift/generate/t_go_generator.cc | 68 |
1 files changed, 58 insertions, 10 deletions
diff --git a/compiler/cpp/src/thrift/generate/t_go_generator.cc b/compiler/cpp/src/thrift/generate/t_go_generator.cc index 8d25d48b5..4ee0e8fbe 100644 --- a/compiler/cpp/src/thrift/generate/t_go_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_go_generator.cc @@ -1102,6 +1102,10 @@ void t_go_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_go_generator::render_const_value(t_type* type, t_const_value* value, const string& name, bool opt) { + string typedef_opt_ptr; + if (type->is_typedef()) { + typedef_opt_ptr = type_name(type) + "Ptr"; + } type = get_true_type(type); std::ostringstream out; @@ -1109,32 +1113,61 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); if (opt) { - out << "&(&struct{x "; switch (tbase) { case t_base_type::TYPE_BOOL: - out << "bool}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.BoolPtr"; + } + out << "("; out << (value->get_integer() > 0 ? "true" : "false"); break; case t_base_type::TYPE_I8: - out << "int8}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.Int8Ptr"; + } + out << "("; out << value->get_integer(); break; case t_base_type::TYPE_I16: - out << "int16}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.Int16Ptr"; + } + out << "("; out << value->get_integer(); break; case t_base_type::TYPE_I32: - out << "int32}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.Int32Ptr"; + } + out << "("; out << value->get_integer(); break; case t_base_type::TYPE_I64: - out << "int64}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.Int64Ptr"; + } + out << "("; out << value->get_integer(); break; case t_base_type::TYPE_DOUBLE: - out << "float64}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.Float64Ptr"; + } + out << "("; if (value->get_type() == t_const_value::CV_INTEGER) { out << value->get_integer(); } else { @@ -1143,14 +1176,19 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co break; case t_base_type::TYPE_STRING: - out << "string}{"; + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr; + } else { + out << "thrift.StringPtr"; + } + out << "("; out << '"' + get_escaped_string(value) + '"'; break; default: throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase); } - out << "}).x"; + out << ")"; } else { switch (tbase) { case t_base_type::TYPE_STRING: @@ -1193,7 +1231,17 @@ string t_go_generator::render_const_value(t_type* type, t_const_value* value, co } } } else if (type->is_enum()) { - indent(out) << value->get_integer(); + if (opt) { + if (typedef_opt_ptr != "") { + out << typedef_opt_ptr << "("; + } else { + out << type_name(type) << "Ptr("; + } + } + out << value->get_integer(); + if (opt) { + out << ")"; + } } else if (type->is_struct() || type->is_xception()) { out << "&" << publicize(type_name(type)) << "{"; indent_up(); |