Skip to content

Commit

Permalink
Always use whisker for rendering code
Browse files Browse the repository at this point in the history
Summary: Everything is using whisker now. Goodbye `mstch::render`!

Reviewed By: createdbysk

Differential Revision: D67954314

fbshipit-source-id: 00b861895d149169553ce44f7625f1ba80b644f9
  • Loading branch information
praihan authored and facebook-github-bot committed Jan 10, 2025
1 parent 4e9ff2d commit 9959f54
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 59 deletions.
4 changes: 0 additions & 4 deletions thrift/compiler/generate/t_json_experimental_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ class t_json_experimental_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
return whisker_options();
}

std::string template_prefix() const override { return "json"; }

void generate_program() override;
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_cpp2_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class t_mstch_cpp2_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"program:autogen_path",
Expand Down
33 changes: 6 additions & 27 deletions thrift/compiler/generate/t_mstch_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,38 +575,17 @@ t_mstch_generator::gen_whisker_render_state(whisker_options whisker_opts) {

std::string t_mstch_generator::render(
const std::string& template_name, const mstch::node& context) {
if (std::holds_alternative<std::monostate>(whisker_render_state_)) {
auto whisker_requested = use_whisker();
whisker_render_state_ = whisker_requested
? gen_whisker_render_state(*whisker_requested)
: std::optional<whisker_render_state>();
}
auto& render_state =
std::get<std::optional<whisker_render_state>>(whisker_render_state_);

if (!render_state.has_value()) {
// The implementation chose not to use Whisker.

// Remove the template prefix from the template map
std::map<std::string, std::string> partials;
auto prefix = template_prefix() + '/';
for (const auto& [k, v] : get_template_map()) {
if (k.substr(0, prefix.size()) == prefix) {
partials[k.substr(prefix.size())] = v;
}
}

return mstch::render(
get_template(prefix + template_name), context, partials);
if (!whisker_render_state_.has_value()) {
whisker_render_state_ = gen_whisker_render_state(render_options());
}

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

std::optional<whisker::ast::root> ast =
render_state->template_resolver->resolve(
partial_path, {}, render_state->diagnostic_engine);
whisker_render_state_->template_resolver->resolve(
partial_path, {}, whisker_render_state_->diagnostic_engine);
if (!ast.has_value()) {
throw std::runtime_error{
fmt::format("Could not find template \"{}\"", template_name)};
Expand All @@ -617,8 +596,8 @@ std::string t_mstch_generator::render(
out,
*ast,
whisker::from_mstch(context),
render_state->diagnostic_engine,
render_state->render_options)) {
whisker_render_state_->diagnostic_engine,
whisker_render_state_->render_options)) {
throw std::runtime_error{
fmt::format("Failed to render template \"{}\"", template_name)};
}
Expand Down
13 changes: 3 additions & 10 deletions thrift/compiler/generate/t_mstch_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ class t_mstch_generator : public t_generator {
std::unordered_set<std::string> allowed_undefined_variables;
};
/**
* If true, uses the Whisker template engine instead of Mustache.
* Customization point for Whisker's rendering options.
*/
virtual std::optional<whisker_options> use_whisker() const {
return std::nullopt;
}
virtual whisker_options render_options() const { return {}; }

/**
* If true, typedefs will be automatically resolved to their underlying type.
Expand Down Expand Up @@ -237,12 +235,7 @@ class t_mstch_generator : public t_generator {
std::shared_ptr<whisker::template_resolver> template_resolver;
whisker::render_options render_options;
};
// std::monostate implies that we have not yet checked if the implemented
// requested the use of Whisker.
// empty optional implies that we have checked and the implementation is NOT
// using Whisker.
std::variant<std::monostate, std::optional<whisker_render_state>>
whisker_render_state_;
std::optional<whisker_render_state> whisker_render_state_;
whisker_render_state gen_whisker_render_state(whisker_options);

/**
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_go_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class t_mstch_go_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"service:autogen_path",
Expand Down
6 changes: 0 additions & 6 deletions thrift/compiler/generate/t_mstch_html_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ class t_mstch_html_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options opts;
opts.allowed_undefined_variables = {};
return opts;
}

std::string template_prefix() const override { return "html"; }

void generate_program() override {
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_java_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class t_mstch_java_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"type:typedef_type", // in UnionWrite.mustache
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_py3_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ class t_mstch_py3_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"service:autogen_path", "function:stream?"};
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_pyi_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class t_mstch_pyi_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"service:autogen_path",
Expand Down
4 changes: 0 additions & 4 deletions thrift/compiler/generate/t_mstch_python_capi_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,6 @@ class t_mstch_python_capi_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
return whisker_options();
}

std::string template_prefix() const override { return "python_capi"; }

void generate_program() override {
Expand Down
4 changes: 2 additions & 2 deletions thrift/compiler/generate/t_mstch_python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ class t_mstch_python_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"field:has_adapter?",
Expand Down Expand Up @@ -1535,7 +1535,7 @@ class t_python_patch_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"field:has_adapter?",
Expand Down
2 changes: 1 addition & 1 deletion thrift/compiler/generate/t_mstch_rust_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ class t_mstch_rust_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options render_options() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"typedef:newtype?",
Expand Down

0 comments on commit 9959f54

Please sign in to comment.