Skip to content

Commit

Permalink
Add primitive types to python patch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Oct 20, 2024
1 parent bece6c1 commit 3ef9a48
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
6 changes: 6 additions & 0 deletions thrift/compiler/generate/t_mstch_python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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},
});
}
Expand All @@ -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(); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +41,7 @@ def MyIntField(self) -> OptionalFieldPatch[
I64Patch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_i64_patch(),
self._patch,
1)
@property
Expand All @@ -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
Expand All @@ -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
Expand All @@ -69,7 +77,7 @@ def oneway(self) -> OptionalFieldPatch[
BoolPatch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_bool_patch(),
self._patch,
5)
@property
Expand All @@ -78,7 +86,7 @@ def readonly(self) -> OptionalFieldPatch[
BoolPatch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_bool_patch(),
self._patch,
6)
@property
Expand All @@ -87,7 +95,7 @@ def idempotent(self) -> OptionalFieldPatch[
BoolPatch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_bool_patch(),
self._patch,
7)
@property
Expand All @@ -96,7 +104,7 @@ def floatSet(self) -> OptionalFieldPatch[
SetPatch[float]]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_set_patch(),
self._patch,
8)
@property
Expand All @@ -116,7 +124,7 @@ def I32List(self) -> OptionalFieldPatch[
ListPatch[int]]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_list_patch(),
self._patch,
1)
@property
Expand All @@ -125,7 +133,7 @@ def StringSet(self) -> OptionalFieldPatch[
SetPatch[str]]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_set_patch(),
self._patch,
2)
@property
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -174,7 +182,7 @@ def floatSet(self) -> OptionalFieldPatch[
SetPatch[float]]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_set_patch(),
self._patch,
4)

Expand All @@ -185,7 +193,7 @@ def MyIntField(self) -> OptionalFieldPatch[
I64Patch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_i64_patch(),
self._patch,
1)
@property
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -223,7 +231,7 @@ def MyIntField(self) -> OptionalFieldPatch[
I64Patch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_i64_patch(),
self._patch,
1)
@property
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -261,7 +269,7 @@ def reserved_field(self) -> OptionalFieldPatch[
I32Patch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_i32_patch(),
self._patch,
1)

Expand All @@ -272,6 +280,6 @@ def reserved_field(self) -> OptionalFieldPatch[
I32Patch]:

return OptionalFieldPatch(
lambda patch: ,
lambda patch: patch.as_i32_patch(),
self._patch,
1)

0 comments on commit 3ef9a48

Please sign in to comment.