Skip to content

Commit f708c87

Browse files
praihanfacebook-github-bot
authored andcommitted
Always use whisker for rendering code
Summary: Everything is using whisker now. Goodbye `mstch::render`! Reviewed By: createdbysk Differential Revision: D67954314 fbshipit-source-id: 00b861895d149169553ce44f7625f1ba80b644f9
1 parent 668c6b7 commit f708c87

12 files changed

+17
-59
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ class t_json_experimental_generator : public t_mstch_generator {
5656
public:
5757
using t_mstch_generator::t_mstch_generator;
5858

59-
std::optional<whisker_options> use_whisker() const override {
60-
return whisker_options();
61-
}
62-
6359
std::string template_prefix() const override { return "json"; }
6460

6561
void generate_program() override;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class t_mstch_cpp2_generator : public t_mstch_generator {
269269
public:
270270
using t_mstch_generator::t_mstch_generator;
271271

272-
std::optional<whisker_options> use_whisker() const override {
272+
whisker_options render_options() const override {
273273
whisker_options opts;
274274
opts.allowed_undefined_variables = {
275275
"program:autogen_path",

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -575,38 +575,17 @@ t_mstch_generator::gen_whisker_render_state(whisker_options whisker_opts) {
575575

576576
std::string t_mstch_generator::render(
577577
const std::string& template_name, const mstch::node& context) {
578-
if (std::holds_alternative<std::monostate>(whisker_render_state_)) {
579-
auto whisker_requested = use_whisker();
580-
whisker_render_state_ = whisker_requested
581-
? gen_whisker_render_state(*whisker_requested)
582-
: std::optional<whisker_render_state>();
583-
}
584-
auto& render_state =
585-
std::get<std::optional<whisker_render_state>>(whisker_render_state_);
586-
587-
if (!render_state.has_value()) {
588-
// The implementation chose not to use Whisker.
589-
590-
// Remove the template prefix from the template map
591-
std::map<std::string, std::string> partials;
592-
auto prefix = template_prefix() + '/';
593-
for (const auto& [k, v] : get_template_map()) {
594-
if (k.substr(0, prefix.size()) == prefix) {
595-
partials[k.substr(prefix.size())] = v;
596-
}
597-
}
598-
599-
return mstch::render(
600-
get_template(prefix + template_name), context, partials);
578+
if (!whisker_render_state_.has_value()) {
579+
whisker_render_state_ = gen_whisker_render_state(render_options());
601580
}
602581

603582
std::vector<std::string> partial_path;
604583
boost::algorithm::split(
605584
partial_path, template_name, [](char c) { return c == '/'; });
606585

607586
std::optional<whisker::ast::root> ast =
608-
render_state->template_resolver->resolve(
609-
partial_path, {}, render_state->diagnostic_engine);
587+
whisker_render_state_->template_resolver->resolve(
588+
partial_path, {}, whisker_render_state_->diagnostic_engine);
610589
if (!ast.has_value()) {
611590
throw std::runtime_error{
612591
fmt::format("Could not find template \"{}\"", template_name)};
@@ -617,8 +596,8 @@ std::string t_mstch_generator::render(
617596
out,
618597
*ast,
619598
whisker::from_mstch(context),
620-
render_state->diagnostic_engine,
621-
render_state->render_options)) {
599+
whisker_render_state_->diagnostic_engine,
600+
whisker_render_state_->render_options)) {
622601
throw std::runtime_error{
623602
fmt::format("Failed to render template \"{}\"", template_name)};
624603
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ class t_mstch_generator : public t_generator {
5959
std::unordered_set<std::string> allowed_undefined_variables;
6060
};
6161
/**
62-
* If true, uses the Whisker template engine instead of Mustache.
62+
* Customization point for Whisker's rendering options.
6363
*/
64-
virtual std::optional<whisker_options> use_whisker() const {
65-
return std::nullopt;
66-
}
64+
virtual whisker_options render_options() const { return {}; }
6765

6866
/**
6967
* If true, typedefs will be automatically resolved to their underlying type.
@@ -237,12 +235,7 @@ class t_mstch_generator : public t_generator {
237235
std::shared_ptr<whisker::template_resolver> template_resolver;
238236
whisker::render_options render_options;
239237
};
240-
// std::monostate implies that we have not yet checked if the implemented
241-
// requested the use of Whisker.
242-
// empty optional implies that we have checked and the implementation is NOT
243-
// using Whisker.
244-
std::variant<std::monostate, std::optional<whisker_render_state>>
245-
whisker_render_state_;
238+
std::optional<whisker_render_state> whisker_render_state_;
246239
whisker_render_state gen_whisker_render_state(whisker_options);
247240

248241
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class t_mstch_go_generator : public t_mstch_generator {
4545
public:
4646
using t_mstch_generator::t_mstch_generator;
4747

48-
std::optional<whisker_options> use_whisker() const override {
48+
whisker_options render_options() const override {
4949
whisker_options opts;
5050
opts.allowed_undefined_variables = {
5151
"service:autogen_path",

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ class t_mstch_html_generator : public t_mstch_generator {
2222
public:
2323
using t_mstch_generator::t_mstch_generator;
2424

25-
std::optional<whisker_options> use_whisker() const override {
26-
whisker_options opts;
27-
opts.allowed_undefined_variables = {};
28-
return opts;
29-
}
30-
3125
std::string template_prefix() const override { return "html"; }
3226

3327
void generate_program() override {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class t_mstch_java_generator : public t_mstch_generator {
159159
public:
160160
using t_mstch_generator::t_mstch_generator;
161161

162-
std::optional<whisker_options> use_whisker() const override {
162+
whisker_options render_options() const override {
163163
whisker_options opts;
164164
opts.allowed_undefined_variables = {
165165
"type:typedef_type", // in UnionWrite.mustache

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ class t_mstch_py3_generator : public t_mstch_generator {
13791379
public:
13801380
using t_mstch_generator::t_mstch_generator;
13811381

1382-
std::optional<whisker_options> use_whisker() const override {
1382+
whisker_options render_options() const override {
13831383
whisker_options opts;
13841384
opts.allowed_undefined_variables = {
13851385
"service:autogen_path", "function:stream?"};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class t_mstch_pyi_generator : public t_mstch_generator {
554554
public:
555555
using t_mstch_generator::t_mstch_generator;
556556

557-
std::optional<whisker_options> use_whisker() const override {
557+
whisker_options render_options() const override {
558558
whisker_options opts;
559559
opts.allowed_undefined_variables = {
560560
"service:autogen_path",

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,6 @@ class t_mstch_python_capi_generator : public t_mstch_generator {
659659
public:
660660
using t_mstch_generator::t_mstch_generator;
661661

662-
std::optional<whisker_options> use_whisker() const override {
663-
return whisker_options();
664-
}
665-
666662
std::string template_prefix() const override { return "python_capi"; }
667663

668664
void generate_program() override {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ class t_mstch_python_generator : public t_mstch_generator {
11251125
public:
11261126
using t_mstch_generator::t_mstch_generator;
11271127

1128-
std::optional<whisker_options> use_whisker() const override {
1128+
whisker_options render_options() const override {
11291129
whisker_options opts;
11301130
opts.allowed_undefined_variables = {
11311131
"field:has_adapter?",
@@ -1535,7 +1535,7 @@ class t_python_patch_generator : public t_mstch_generator {
15351535
public:
15361536
using t_mstch_generator::t_mstch_generator;
15371537

1538-
std::optional<whisker_options> use_whisker() const override {
1538+
whisker_options render_options() const override {
15391539
whisker_options opts;
15401540
opts.allowed_undefined_variables = {
15411541
"field:has_adapter?",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ class t_mstch_rust_generator : public t_mstch_generator {
619619
public:
620620
using t_mstch_generator::t_mstch_generator;
621621

622-
std::optional<whisker_options> use_whisker() const override {
622+
whisker_options render_options() const override {
623623
whisker_options opts;
624624
opts.allowed_undefined_variables = {
625625
"typedef:newtype?",

0 commit comments

Comments
 (0)