Skip to content

Commit 6b36381

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate property struct:go_qualified_new_func to Whisker prototype
Reviewed By: echistyakov Differential Revision: D88299172 fbshipit-source-id: 59f26efa026af7ab76679c0849f6afd4bd04be53
1 parent 808fe70 commit 6b36381

3 files changed

Lines changed: 21 additions & 31 deletions

File tree

third-party/thrift/src/thrift/compiler/generate/go/util.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ void codegen_data::compute_req_resp_structs() {
164164
}
165165
}
166166

167+
bool codegen_data::is_req_resp_struct(const t_structured& structured) const {
168+
return structured.generated() &&
169+
std::find(
170+
req_resp_structs.begin(), req_resp_structs.end(), &structured) !=
171+
req_resp_structs.end();
172+
}
173+
167174
void codegen_data::add_to_thrift_metadata_types(
168175
const t_type* type, std::set<std::string>& visited_type_names) {
169176
// Check if we have already visited this type.

third-party/thrift/src/thrift/compiler/generate/go/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class codegen_data {
8787
std::string get_go_package_alias(const t_program* program) const;
8888
std::string go_package_alias_prefix(const t_program* program) const;
8989

90+
bool is_req_resp_struct(const t_structured& tstruct) const;
91+
9092
private:
9193
std::string make_go_package_name_unique(const std::string& name);
9294
void add_to_thrift_metadata_types(

third-party/thrift/src/thrift/compiler/generate/t_mstch_go_generator.cc

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ class t_mstch_go_generator : public t_mstch_generator {
299299
self.find_structured_annotation_or_null(kGoUseReflectCodecUri);
300300
return data_.use_reflect_codec || use_reflect_codec_annotation != nullptr;
301301
});
302+
def.property("go_qualified_new_func", [this](const t_structured& self) {
303+
return fmt::format(
304+
"{}{}{}",
305+
data_.go_package_alias_prefix(self.program()),
306+
data_.is_req_resp_struct(self) ? "new" : "New",
307+
go::munge_ident(self.name(), /*exported=*/true));
308+
});
302309

303310
return std::move(def).make();
304311
}
@@ -530,8 +537,6 @@ class mstch_go_struct : public mstch_struct {
530537
{
531538
{"struct:go_name", &mstch_go_struct::go_name},
532539
{"struct:go_qualified_name", &mstch_go_struct::go_qualified_name},
533-
{"struct:go_qualified_new_func",
534-
&mstch_go_struct::go_qualified_new_func},
535540
{"struct:req_resp?", &mstch_go_struct::is_req_resp_struct},
536541
{"struct:resp?", &mstch_go_struct::is_resp_struct},
537542
{"struct:req?", &mstch_go_struct::is_req_struct},
@@ -545,27 +550,23 @@ class mstch_go_struct : public mstch_struct {
545550
auto prefix = data_.go_package_alias_prefix(struct_->program());
546551
return prefix + go_name_();
547552
}
548-
mstch::node go_qualified_new_func() {
549-
auto prefix = data_.go_package_alias_prefix(struct_->program());
550-
return prefix + go_new_func_();
551-
}
552553
mstch::node is_req_resp_struct() {
553554
// Whether this is a helper request or response struct.
554-
return is_req_resp_struct_();
555+
return data_.is_req_resp_struct(*struct_);
555556
}
556557
mstch::node is_resp_struct() {
557558
// Whether this is a helper response struct.
558-
return is_req_resp_struct_() &&
559+
return data_.is_req_resp_struct(*struct_) &&
559560
boost::algorithm::starts_with(struct_->name(), "resp");
560561
}
561562
mstch::node is_req_struct() {
562563
// Whether this is a helper request struct.
563-
return is_req_resp_struct_() &&
564+
return data_.is_req_resp_struct(*struct_) &&
564565
boost::algorithm::starts_with(struct_->name(), "req");
565566
}
566567
mstch::node is_stream_struct() {
567568
// Whether this is a helper stream struct.
568-
return is_req_resp_struct_() &&
569+
return data_.is_req_resp_struct(*struct_) &&
569570
boost::algorithm::starts_with(struct_->name(), "stream");
570571
}
571572
mstch::node fields_sorted() {
@@ -585,7 +586,7 @@ class mstch_go_struct : public mstch_struct {
585586

586587
std::string go_name_() {
587588
auto name = struct_->name();
588-
if (is_req_resp_struct_()) {
589+
if (data_.is_req_resp_struct(*struct_)) {
589590
// Unexported/lowercase
590591
return go::munge_ident(name, false);
591592
} else {
@@ -597,26 +598,6 @@ class mstch_go_struct : public mstch_struct {
597598
return go::munge_ident(name, true);
598599
}
599600
}
600-
601-
std::string go_new_func_() {
602-
auto name = struct_->name();
603-
auto go_name = go::munge_ident(struct_->name(), true);
604-
if (is_req_resp_struct_()) {
605-
// Unexported/lowercase
606-
return "new" + go_name;
607-
} else {
608-
// Exported/uppercase
609-
return "New" + go_name;
610-
}
611-
}
612-
613-
bool is_req_resp_struct_() {
614-
return struct_->generated() &&
615-
std::find(
616-
data_.req_resp_structs.begin(),
617-
data_.req_resp_structs.end(),
618-
struct_) != data_.req_resp_structs.end();
619-
}
620601
};
621602

622603
void t_mstch_go_generator::generate_program() {

0 commit comments

Comments
 (0)