Skip to content

Commit

Permalink
Add py3 namespace support
Browse files Browse the repository at this point in the history
Summary:
If users have custom py3 namespace, we need to output file in the namespace path.

e.g., if we have `namespace py3 foo.bar`

* Before this diff, we output to `gen-python-patch/thrift_patch.py`, but this won't work with base_module.
* After this diff, we output to `gen-python-patch/foo/bar/thrift_patch.py` and set base_module to "foo. Bar".

Note:

1. If users don't have custom py3 namespace, we will create a default one that matches the file path of the thrift file to simplify the logic, so that later down the line we don't need special logic to handle py3_namespace == None.
2. Python foundation plans to get rid of base_module, but we won't wait for that.

Reviewed By: ahilger

Differential Revision: D64764839

fbshipit-source-id: 945578f3672d096be793e26e8882277ad19cd9c1
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Oct 25, 2024
1 parent 74a8c31 commit 288dfaa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ class python_mstch_type : public mstch_type {
{"type:module_path", &python_mstch_type::module_path},
{"type:patch_module_path", &python_mstch_type::patch_module_path},
{"type:need_module_path?", &python_mstch_type::need_module_path},
{"type:need_patch_module_path?",
&python_mstch_type::need_patch_module_path},
});
}

Expand Down Expand Up @@ -686,12 +688,14 @@ class python_mstch_type : public mstch_type {
if (!has_option("is_types_file")) {
return true;
}
if (const t_program* prog = type_->program()) {
if (prog != prog_) {
return true;
}
return is_type_defined_in_the_current_program();
}

mstch::node need_patch_module_path() {
if (!has_option("is_patch_file")) {
return true;
}
return false;
return is_type_defined_in_the_current_program();
}

mstch::node is_external_program() {
Expand All @@ -716,6 +720,15 @@ class python_mstch_type : public mstch_type {
return prog_;
}

bool is_type_defined_in_the_current_program() {
if (const t_program* prog = type_->program()) {
if (prog != prog_) {
return true;
}
}
return false;
}

const t_program* prog_;
const t_const* adapter_annotation_;
const t_const* transitive_adapter_annotation_;
Expand Down Expand Up @@ -965,6 +978,11 @@ bool validate_structured(sema_context& ctx, const t_struct& s) {

} // namespace enum_member_union_field_names_validator

std::filesystem::path program_to_path(const t_program& prog) {
auto package = get_py3_namespace(&prog);
return fmt::format("{}", fmt::join(package, "/"));
}

class t_mstch_python_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;
Expand All @@ -981,7 +999,7 @@ class t_mstch_python_generator : public t_mstch_generator {
std::string template_prefix() const override { return "python"; }

void generate_program() override {
generate_root_path_ = package_to_path();
generate_root_path_ = program_to_path(*get_program());
out_dir_base_ = "gen-python";
auto include_prefix = get_option("include_prefix").value_or("");
if (!include_prefix.empty()) {
Expand Down Expand Up @@ -1018,7 +1036,6 @@ class t_mstch_python_generator : public t_mstch_generator {
void generate_metadata();
void generate_clients();
void generate_services();
std::filesystem::path package_to_path();

std::filesystem::path generate_root_path_;
};
Expand Down Expand Up @@ -1241,11 +1258,6 @@ void t_mstch_python_generator::set_mstch_factories() {
mstch_context_.add<python_mstch_deprecated_annotation>();
}

std::filesystem::path t_mstch_python_generator::package_to_path() {
auto package = get_py3_namespace(get_program());
return fmt::format("{}", fmt::join(package, "/"));
}

void t_mstch_python_generator::generate_file(
const std::string& template_name,
IsTypesFile is_types_file,
Expand Down Expand Up @@ -1381,12 +1393,15 @@ class t_python_patch_generator : public t_mstch_generator {

set_mstch_factories();
mstch_context_.set_or_erase_option(true, "generate_immutable_types", "yes");
mstch_context_.set_or_erase_option(true, "is_patch_file", "");
const auto* program = get_program();
auto mstch_program = mstch_context_.program_factory->make_mstch_object(
program, mstch_context_);

render_to_file(
std::move(mstch_program), "thrift_patch.py", "thrift_patch.py");
std::move(mstch_program),
"thrift_patch.py",
program_to_path(*get_program()) / program->name() / "thrift_patch.py");
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ import folly.iobuf as _fbthrift_iobuf

import {{program:module_path}}.thrift_types

{{#program:include_namespaces}}
{{#has_types?}}

import {{included_module_path}}.{{> types/types_import_path}}
import {{included_module_path}}.thrift_patch
{{/has_types?}}
{{/program:include_namespaces}}

{{#program:structs}}

class {{struct:py_name}}Patch(BaseStructPatch[{{program:module_path}}.thrift_types.{{struct:py_name}}]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import test.fixtures.basic.module.thrift_types



class MyStructPatch(BaseStructPatch[test.fixtures.basic.module.thrift_types.MyStruct]):
pass

Expand Down

0 comments on commit 288dfaa

Please sign in to comment.