From 3ef9a48a9c48715653500fe4c17a9a80f873d522 Mon Sep 17 00:00:00 2001 From: TJ Yin Date: Sat, 19 Oct 2024 21:29:52 -0700 Subject: [PATCH] Add primitive types to python patch Summary: This diff added all primitive types patch to python. Note that currently `ensure` is not supported for all types, which I explained in the comment. This is a roadblock to launch python patch. Reviewed By: thedavekwon Differential Revision: D64508063 fbshipit-source-id: e1418b84193a5147db012ea52cd309f53a1354c0 --- .../generate/t_mstch_python_generator.cc | 6 +++ .../common/thrift_patch_callback.mustache | 20 ++++++- .../templates/python/thrift_patch.py.mustache | 8 +++ .../gen-python-patch/thrift_patch.py | 52 +++++++++++-------- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/thrift/compiler/generate/t_mstch_python_generator.cc b/thrift/compiler/generate/t_mstch_python_generator.cc index ba24d95572b..8fdefb8458b 100644 --- a/thrift/compiler/generate/t_mstch_python_generator.cc +++ b/thrift/compiler/generate/t_mstch_python_generator.cc @@ -624,6 +624,7 @@ class python_mstch_type : public mstch_type { this, { {"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}, }); } @@ -644,6 +645,11 @@ class python_mstch_type : public mstch_type { get_type_program(), get_option("root_module_prefix")) .append(types_import_path); } + mstch::node patch_module_path() { + return get_py3_namespace_with_name_and_prefix( + get_type_program(), get_option("root_module_prefix")) + .append(".thrift_patch"); + } mstch::node program_name() { return get_type_program()->name(); } diff --git a/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache b/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache index 9ea25a5ef20..75ee2e9b506 100644 --- a/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache +++ b/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache @@ -16,6 +16,22 @@ }}{{! Lambda is not type checked by pyre, though here we only used lambda internally, it won't affect UX. -}}lambda patch: {{#type:map?}}{{#type:value_type}}MapPatch({{> common/thrift_patch_callback}}, patch.as_map_patch()){{/type:value_type}}{{/type:map?}}{{! +}}lambda patch: {{! +}}{{#type:bool?}}patch.as_bool_patch(){{/type:bool?}}{{! +}}{{#type:byte?}}patch.as_byte_patch(){{/type:byte?}}{{! +}}{{#type:i16?}}patch.as_i16_patch(){{/type:i16?}}{{! +}}{{#type:i32?}}patch.as_i32_patch(){{/type:i32?}}{{! +}}{{#type:i64?}}patch.as_i64_patch(){{/type:i64?}}{{! +}}{{#type:double?}}patch.as_double_patch(){{/type:double?}}{{! +}}{{#type:float?}}patch.as_float_patch(){{/type:float?}}{{! }}{{#type:string?}}patch.as_string_patch(){{/type:string?}}{{! -}}{{#type:struct}}{{struct:py_name}}Patch(patch){{/type:struct}} +}}{{#type:binary?}}patch.as_binary_patch(){{/type:binary?}}{{! +}}{{#type:enum}}patch.as_enum_patch(){{/type:enum}}{{! +}}{{#type:list?}}patch.as_list_patch(){{/type:list?}}{{! +}}{{#type:set?}}patch.as_set_patch(){{/type:set?}}{{! +}}{{#type:map?}}{{#type:value_type}}{{! +}}MapPatch({{> common/thrift_patch_callback}}, patch.as_map_patch()){{! +}}{{/type:value_type}}{{/type:map?}}{{! +}}{{#type:struct}}{{! + }}{{type:patch_module_path}}.{{struct:py_name}}Patch(patch){{! +}}{{/type:struct}} diff --git a/thrift/compiler/generate/templates/python/thrift_patch.py.mustache b/thrift/compiler/generate/templates/python/thrift_patch.py.mustache index d85691d3899..ddfb8114784 100644 --- a/thrift/compiler/generate/templates/python/thrift_patch.py.mustache +++ b/thrift/compiler/generate/templates/python/thrift_patch.py.mustache @@ -27,7 +27,15 @@ from common.thrift.patch.detail.dynamic_patch import ( ) from common.thrift.patch.detail.py_bindings.DynamicPatch import ( + BoolPatch, + BytePatch, + I16Patch, + I32Patch, + I64Patch, + FloatPatch, + DoublePatch, StringPatch, + BinaryPatch, ) import {{program:module_path}}.thrift_types diff --git a/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/thrift_patch.py b/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/thrift_patch.py index 0641ab29c32..a36fbde19d0 100644 --- a/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/thrift_patch.py +++ b/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/thrift_patch.py @@ -10,7 +10,15 @@ ) from common.thrift.patch.detail.py_bindings.DynamicPatch import ( + BoolPatch, + BytePatch, + I16Patch, + I32Patch, + I64Patch, + FloatPatch, + DoublePatch, StringPatch, + BinaryPatch, ) import test.fixtures.basic.module.thrift_types @@ -33,7 +41,7 @@ def MyIntField(self) -> OptionalFieldPatch[ I64Patch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_i64_patch(), self._patch, 1) @property @@ -51,7 +59,7 @@ def MyDataField(self) -> OptionalFieldPatch[ MyDataItemPatch]: return OptionalFieldPatch( - lambda patch: MyDataItemPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyDataItemPatch(patch), self._patch, 3) @property @@ -60,7 +68,7 @@ def myEnum(self) -> OptionalFieldPatch[ test.fixtures.basic.module.thrift_types.MyEnum]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_enum_patch(), self._patch, 4) @property @@ -69,7 +77,7 @@ def oneway(self) -> OptionalFieldPatch[ BoolPatch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_bool_patch(), self._patch, 5) @property @@ -78,7 +86,7 @@ def readonly(self) -> OptionalFieldPatch[ BoolPatch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_bool_patch(), self._patch, 6) @property @@ -87,7 +95,7 @@ def idempotent(self) -> OptionalFieldPatch[ BoolPatch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_bool_patch(), self._patch, 7) @property @@ -96,7 +104,7 @@ def floatSet(self) -> OptionalFieldPatch[ SetPatch[float]]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_set_patch(), self._patch, 8) @property @@ -116,7 +124,7 @@ def I32List(self) -> OptionalFieldPatch[ ListPatch[int]]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_list_patch(), self._patch, 1) @property @@ -125,7 +133,7 @@ def StringSet(self) -> OptionalFieldPatch[ SetPatch[str]]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_set_patch(), self._patch, 2) @property @@ -134,7 +142,7 @@ def StringToI64Map(self) -> OptionalFieldPatch[ MapPatch[str, I64Patch]]: return OptionalFieldPatch( - lambda patch: MapPatch(lambda patch: , patch.as_map_patch()), + lambda patch: MapPatch(lambda patch: patch.as_i64_patch(), patch.as_map_patch()), self._patch, 3) @@ -147,7 +155,7 @@ def myEnum(self) -> OptionalFieldPatch[ test.fixtures.basic.module.thrift_types.MyEnum]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_enum_patch(), self._patch, 1) @property @@ -156,7 +164,7 @@ def myStruct(self) -> OptionalFieldPatch[ MyStructPatch]: return OptionalFieldPatch( - lambda patch: MyStructPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyStructPatch(patch), self._patch, 2) @property @@ -165,7 +173,7 @@ def myDataItem(self) -> OptionalFieldPatch[ MyDataItemPatch]: return OptionalFieldPatch( - lambda patch: MyDataItemPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyDataItemPatch(patch), self._patch, 3) @property @@ -174,7 +182,7 @@ def floatSet(self) -> OptionalFieldPatch[ SetPatch[float]]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_set_patch(), self._patch, 4) @@ -185,7 +193,7 @@ def MyIntField(self) -> OptionalFieldPatch[ I64Patch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_i64_patch(), self._patch, 1) @property @@ -203,7 +211,7 @@ def myStruct(self) -> OptionalFieldPatch[ MyStructPatch]: return OptionalFieldPatch( - lambda patch: MyStructPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyStructPatch(patch), self._patch, 3) @property @@ -212,7 +220,7 @@ def myUnion(self) -> OptionalFieldPatch[ MyUnionPatch]: return OptionalFieldPatch( - lambda patch: MyUnionPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyUnionPatch(patch), self._patch, 4) @@ -223,7 +231,7 @@ def MyIntField(self) -> OptionalFieldPatch[ I64Patch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_i64_patch(), self._patch, 1) @property @@ -241,7 +249,7 @@ def myStruct(self) -> OptionalFieldPatch[ MyStructPatch]: return OptionalFieldPatch( - lambda patch: MyStructPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyStructPatch(patch), self._patch, 3) @property @@ -250,7 +258,7 @@ def myUnion(self) -> OptionalFieldPatch[ MyUnionPatch]: return OptionalFieldPatch( - lambda patch: MyUnionPatch(patch), + lambda patch: test.fixtures.basic.module.thrift_patch.MyUnionPatch(patch), self._patch, 4) @@ -261,7 +269,7 @@ def reserved_field(self) -> OptionalFieldPatch[ I32Patch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_i32_patch(), self._patch, 1) @@ -272,6 +280,6 @@ def reserved_field(self) -> OptionalFieldPatch[ I32Patch]: return OptionalFieldPatch( - lambda patch: , + lambda patch: patch.as_i32_patch(), self._patch, 1)