Skip to content

Commit

Permalink
Make callback in python patch more readable.
Browse files Browse the repository at this point in the history
Summary:
Currently callback uses lambda. We can create an actual function to make it more readable.

{F1973681999}

Reviewed By: thedavekwon

Differential Revision: D66996590

fbshipit-source-id: f0d22c4107e62c536d57b957abaf31ae66ac9698
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Dec 10, 2024
1 parent 432d9df commit 9a2e59e
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ from common.thrift.patch.detail.py_bindings.DynamicPatch import (
BinaryPatch,
StructPatch as DynamicStructPatch,
UnionPatch as DynamicUnionPatch,
DynamicPatch
)

import thrift.python.types as _fbthrift_python_types
Expand Down Expand Up @@ -73,8 +74,14 @@ class {{struct:py_name}}Patch(
{{#field:type}}{{> ../python/types/unadapted_pep484_type}}{{/field:type}},
{{#field:type}}{{> ../python/types/unadapted_pep484_patch_type}}{{/field:type}}]:

{{#field:type}}
{{! e.g., if field is an i32, this function convert DynamicPatch --> I32Patch }}
def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> {{#field:type}}{{> ../python/types/unadapted_pep484_patch_type}}{{/field:type}}:
{{> ../python/common/thrift_patch_callback}}
{{/field:type}}

return {{> types/field_patch_type}}(
{{#field:type}}{{> ../python/common/thrift_patch_callback}}{{/field:type}},
cast_dynamic_patch_to_typed_field_patch,
self._patch,
{{field:key}},
{{#field:type}}{{> ../python/types/typeinfo }}{{/field:type}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,51 @@
See the License for the specific language governing permissions and
limitations under the License.
}}{{!
Lambda is not type checked by pyre, though here we only used lambda internally, it won't affect UX.
}}lambda patch, type_info: {{!
}}{{#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:binary?}}patch.as_binary_patch(){{/type:binary?}}{{!
}}{{#type:enum}}patch.as_enum_patch(){{/type:enum}}{{!
}}{{#type:list?}}ListPatch(patch.as_list_patch(), type_info){{/type:list?}}{{!
}}{{#type:set?}}SetPatch(patch.as_set_patch(), type_info){{/type:set?}}{{!
}}{{#type:map?}}{{#type:value_type}}{{!
}}MapPatch({{> common/thrift_patch_callback}}, patch.as_map_patch(), type_info){{!
}}{{/type:value_type}}{{/type:map?}}{{!
}}{{#type:struct}}{{!
}}{{#type:need_patch_module_path?}}{{type:patch_module_path}}.{{/type:need_patch_module_path?}}{{struct:py_name}}Patch(patch){{!
}}{{/type:struct}}
}}
{{#type:bool?}}
return patch.as_bool_patch()
{{/type:bool?}}
{{#type:byte?}}
return patch.as_byte_patch()
{{/type:byte?}}
{{#type:i16?}}
return patch.as_i16_patch()
{{/type:i16?}}
{{#type:i32?}}
return patch.as_i32_patch()
{{/type:i32?}}
{{#type:i64?}}
return patch.as_i64_patch()
{{/type:i64?}}
{{#type:float?}}
return patch.as_float_patch()
{{/type:float?}}
{{#type:double?}}
return patch.as_double_patch()
{{/type:double?}}
{{#type:string?}}
return patch.as_string_patch()
{{/type:string?}}
{{#type:binary?}}
return patch.as_binary_patch()
{{/type:binary?}}
{{#type:enum?}}
return patch.as_enum_patch()
{{/type:enum?}}
{{#type:list?}}
return ListPatch(patch.as_list_patch(), type_info)
{{/type:list?}}
{{#type:set?}}
return SetPatch(patch.as_set_patch(), type_info)
{{/type:set?}}
{{#type:map?}}
{{! e.g., if map value is an i32, this function convert DynamicPatch --> I32Patch }}
{{#type:value_type}}
def cast_dynamic_patch_to_typed_map_value_patch(patch: DynamicPatch, type_info) -> {{> types/unadapted_pep484_patch_type}}:
{{> common/thrift_patch_callback}}
{{/type:value_type}}
return MapPatch(cast_dynamic_patch_to_typed_map_value_patch, patch.as_map_patch(), type_info)
{{/type:map?}}
{{#type:struct}}
return {{#type:need_patch_module_path?}}{{type:patch_module_path}}.{{/type:need_patch_module_path?}}{{struct:py_name}}Patch(patch)
{{/type:struct}}
Loading

0 comments on commit 9a2e59e

Please sign in to comment.