Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serializer factory functions #629

Merged
merged 2 commits into from Jan 4, 2023

Conversation

Solpatium
Copy link
Contributor

No description provided.

Copy link
Collaborator

@war-in war-in left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fire 🔥 Just one comment

Comment on lines 71 to 88
def test_getting_type_serializer():
assert serializer_for_type(abi.defined_structures["Uint256"]) == Uint256Serializer()
assert serializer_for_type(abi.defined_structures["PoolId"]) == pool_id_serializer
assert serializer_for_type(abi.defined_structures["User"]) == user_serializer

struct_with_tuples = StructType(
"structure",
OrderedDict(
regular=TupleType([FeltType()]),
named=NamedTupleType(OrderedDict(value=FeltType())),
),
)
assert serializer_for_type(struct_with_tuples) == StructSerializer(
OrderedDict(
regular=TupleSerializer([FeltSerializer()]),
named=NamedTupleSerializer(OrderedDict(value=FeltSerializer())),
)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_getting_type_serializer():
assert serializer_for_type(abi.defined_structures["Uint256"]) == Uint256Serializer()
assert serializer_for_type(abi.defined_structures["PoolId"]) == pool_id_serializer
assert serializer_for_type(abi.defined_structures["User"]) == user_serializer
struct_with_tuples = StructType(
"structure",
OrderedDict(
regular=TupleType([FeltType()]),
named=NamedTupleType(OrderedDict(value=FeltType())),
),
)
assert serializer_for_type(struct_with_tuples) == StructSerializer(
OrderedDict(
regular=TupleSerializer([FeltSerializer()]),
named=NamedTupleSerializer(OrderedDict(value=FeltSerializer())),
)
)
@pytest.mark.parametrize(
"structure, serializer",
(
(abi.defined_structures["Uint256"], Uint256Serializer()),
(abi.defined_structures["PoolId"], pool_id_serializer),
(abi.defined_structures["User"], user_serializer),
(
StructType(
"structure",
OrderedDict(
regular=TupleType([FeltType()]),
named=NamedTupleType(OrderedDict(value=FeltType())),
),
),
StructSerializer(
OrderedDict(
regular=TupleSerializer([FeltSerializer()]),
named=NamedTupleSerializer(OrderedDict(value=FeltSerializer())),
)
),
),
),
)
def test_getting_type_serializer(structure, serializer):
assert serializer_for_type(structure) == serializer

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like current approach, because it is very simple and easy to follow. We create multiple serialisers here and it itself is complex. In this case it is very easy to track what went wrong, because there will be a line assigned. I am ok if others want me to parametrise it, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think parametrized tests produce nicer output in case of failures (the test name indicates what case failed). Maybe let's changed it.

starknet_py/cairo/serialization/factory.py Show resolved Hide resolved
Comment on lines 71 to 88
def test_getting_type_serializer():
assert serializer_for_type(abi.defined_structures["Uint256"]) == Uint256Serializer()
assert serializer_for_type(abi.defined_structures["PoolId"]) == pool_id_serializer
assert serializer_for_type(abi.defined_structures["User"]) == user_serializer

struct_with_tuples = StructType(
"structure",
OrderedDict(
regular=TupleType([FeltType()]),
named=NamedTupleType(OrderedDict(value=FeltType())),
),
)
assert serializer_for_type(struct_with_tuples) == StructSerializer(
OrderedDict(
regular=TupleSerializer([FeltSerializer()]),
named=NamedTupleSerializer(OrderedDict(value=FeltSerializer())),
)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think parametrized tests produce nicer output in case of failures (the test name indicates what case failed). Maybe let's changed it.

@Solpatium Solpatium merged commit 11af815 into refactor/data-transformer Jan 4, 2023
@Solpatium Solpatium deleted the feat/serializer-factory branch January 4, 2023 10:56
cptartur added a commit that referenced this pull request Jan 23, 2023
* Add cairo type parser (#566)

* Add ABI module (#571)

* Add transformation context (#576)

* Add TupleDataclass (#590)

* Add cairo.serializer module (#593)

* Add fix to nested errors (#599)

* Feat/payload serializer (#601)

* Add tuple serializers (#612)

* Add struct & uin256 serilaizer (#613)

* Add FunctionSerializationAdapter (#614)

* Add cycle check in parser (#630)

* Add serializer factory functions (#629)

* Use new serializer in contract (#637)

* Replace data transfomer in some modules (#642)

* Replace data transfomer in some modules

* Simplify structure in account

* Remove more dependencies on data_transfomer

* Remove string typed types in contract

* Cover missing statements (#648)

* Cover missing statements

* Change deprecation text in felt.py

* Update starknet_py/cairo/type_parser.py

Co-authored-by: war-in <61014013+war-in@users.noreply.github.com>

Co-authored-by: war-in <61014013+war-in@users.noreply.github.com>

* Add missing dots (#652)

* Deprecate data_transformer module (#654)

* Add simple examples in documentation of serializers (#653)

* Add test for complex abi (#663)

* Support {low, high} dict in uint256 serializer (#668)

* Support {low, high} dict in uint256 serializer

* Replace ^ with **

* Add basic docs for serializers (#679)

* Add basic docs for serializers

* Add more docs

* Adjust comment

* Move abi and serialization to top level modules

* Add TupleDataclass to api

* Fix docs

* Fix serializing example

* Fix lint

* Use header consistent with the rest of the api

* Remove unnecessary pylint disable, unify disables

* Update comment

* Format

* Update docs/guide/serialization.rst

Co-authored-by: Kamil Jankowski <kamil.jankowski.x@gmail.com>

Co-authored-by: Artur Michalek <artur.michalek@swmansion.com>
Co-authored-by: Artur Michałek <52135326+cptartur@users.noreply.github.com>
Co-authored-by: Kamil Jankowski <kamil.jankowski.x@gmail.com>

* Apply suggestions from code review

Co-authored-by: war-in <61014013+war-in@users.noreply.github.com>

* Make deployer.py variables private

Co-authored-by: war-in <61014013+war-in@users.noreply.github.com>
Co-authored-by: Artur Michalek <artur.michalek@swmansion.com>
Co-authored-by: Artur Michałek <52135326+cptartur@users.noreply.github.com>
Co-authored-by: Kamil Jankowski <kamil.jankowski.x@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants