Skip to content

Commit 09b9e87

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Add to_type_array helper to base Whisker generator
Summary: Add `to_type_array` helper to return most-derived prototype for a collection of `t_type` elements. Reviewed By: praihan Differential Revision: D95302151 fbshipit-source-id: 074d5fcdc2179d3c370768203c31135050f18b31
1 parent 42cd6d1 commit 09b9e87

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,7 @@ class t_mstch_go_generator : public t_whisker_generator {
149149
return to_array(data_.req_resp_structs, proto.of<t_struct>());
150150
});
151151
def.property("thrift_metadata_types", [this, &proto](const t_program&) {
152-
whisker::array::raw result;
153-
result.reserve(data_.thrift_metadata_types.size());
154-
for (const t_type* type : data_.thrift_metadata_types) {
155-
result.emplace_back(resolve_derived_t_type(proto, *type));
156-
}
157-
return whisker::array::of(std::move(result));
152+
return to_type_array(data_.thrift_metadata_types, proto);
158153
});
159154

160155
return std::move(def).make();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ class t_mstch_py3_generator : public t_mstch_generator {
10161016
visible.reserve(self.structured_definitions().size());
10171017
for (const t_structured* s : self.structured_definitions()) {
10181018
if (!is_hidden(*s)) {
1019-
visible.emplace_back(proto.create<t_structured>(*s));
1019+
visible.emplace_back(resolve_derived_t_type(proto, *s));
10201020
}
10211021
}
10221022
return whisker::make::array(std::move(visible));
@@ -1104,10 +1104,10 @@ class t_mstch_py3_generator : public t_mstch_generator {
11041104
!context_->stream_types(self).empty();
11051105
});
11061106
def.property("custom_templates", [&](const t_program& self) {
1107-
return to_array(context_->custom_templates(self), proto.of<t_type>());
1107+
return to_type_array(context_->custom_templates(self), proto);
11081108
});
11091109
def.property("custom_cpp_types", [&](const t_program& self) {
1110-
return to_array(context_->custom_cpp_types(self), proto.of<t_type>());
1110+
return to_type_array(context_->custom_cpp_types(self), proto);
11111111
});
11121112

11131113
return std::move(def).make();

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,7 @@ prototype<t_program>::ptr t_whisker_generator::make_prototype_for_program(
607607
});
608608

609609
def.property("structured_definitions", [&](const t_program& self) {
610-
array::raw result;
611-
result.reserve(self.structured_definitions().size());
612-
for (const t_structured* structured : self.structured_definitions()) {
613-
result.emplace_back(resolve_derived_t_type(proto, *structured));
614-
}
615-
return array::of(std::move(result));
610+
return to_type_array(self.structured_definitions(), proto);
616611
});
617612
def.property("services", mem_fn(&t_program::services, proto.of<t_service>()));
618613
def.property(
@@ -736,8 +731,7 @@ prototype<t_function>::ptr t_whisker_generator::make_prototype_for_function(
736731
def.property("stream", mem_fn(&t_function::stream, proto.of<t_stream>()));
737732

738733
def.property("return_type", [&](const t_function& function) {
739-
const t_type* type = function.return_type().get_type();
740-
return resolve_derived_t_type(proto, *type);
734+
return resolve_derived_t_type(proto, function.return_type().deref());
741735
});
742736

743737
def.property("oneway?", [](const t_function& self) {

third-party/thrift/src/thrift/compiler/generate/t_whisker_generator.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,16 @@ class t_whisker_generator : public t_generator {
460460
*/
461461
template <typename T>
462462
static auto to_array(node_list_view<const T> nodes, prototype_ptr<T> proto) {
463+
static_assert(
464+
!std::is_same_v<t_type, std::remove_cv_t<T>>,
465+
"Use `to_type_array` for lists of `t_type`, to avoid bugs with incorrect derived prototype resolution");
463466
whisker::array::raw refs;
464467
refs.reserve(nodes.size());
465468
for (const T& ref : nodes) {
466469
refs.emplace_back(
467470
whisker::make::native_handle(whisker::manage_as_static(ref), proto));
468471
}
469-
return whisker::make::array(refs);
472+
return whisker::make::array(std::move(refs));
470473
}
471474

472475
/**
@@ -475,13 +478,31 @@ class t_whisker_generator : public t_generator {
475478
template <typename T>
476479
static auto to_array(
477480
const std::vector<T*>& nodes, prototype_ptr<std::remove_cv_t<T>> proto) {
481+
static_assert(
482+
!std::is_same_v<t_type, std::remove_cv_t<T>>,
483+
"Use `to_type_array` for lists of `t_type`, to avoid bugs with incorrect derived prototype resolution");
478484
whisker::array::raw refs;
479485
refs.reserve(nodes.size());
480486
for (const T* ref : nodes) {
481487
refs.emplace_back(
482488
whisker::make::native_handle(whisker::manage_as_static(*ref), proto));
483489
}
484-
return whisker::make::array(refs);
490+
return whisker::make::array(std::move(refs));
491+
}
492+
493+
/**
494+
* Converts a container of `t_type` instances to a Whisker array, resolving
495+
* the most derived prototype for each element.
496+
*/
497+
template <typename T>
498+
static auto to_type_array(
499+
const std::vector<T*>& nodes, const prototype_database& proto) {
500+
whisker::array::raw refs;
501+
refs.reserve(nodes.size());
502+
for (const T* ref : nodes) {
503+
refs.emplace_back(resolve_derived_t_type(proto, *ref));
504+
}
505+
return whisker::make::array(std::move(refs));
485506
}
486507

487508
/**

0 commit comments

Comments
 (0)