| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- Metadata-Version: 2.1
- Name: marshmallow-oneofschema
- Version: 3.1.1
- Summary: marshmallow multiplexing schema
- Author-email: Maxim Kulkin <maxim.kulkin@gmail.com>
- Maintainer-email: Steven Loria <sloria1@gmail.com>
- Requires-Python: >=3.8
- Description-Content-Type: text/x-rst
- Classifier: Intended Audience :: Developers
- Classifier: License :: OSI Approved :: MIT License
- Classifier: Programming Language :: Python :: 3
- Classifier: Programming Language :: Python :: 3.8
- Classifier: Programming Language :: Python :: 3.9
- Classifier: Programming Language :: Python :: 3.10
- Classifier: Programming Language :: Python :: 3.11
- Classifier: Programming Language :: Python :: 3.12
- Requires-Dist: marshmallow>=3.0.0,<4.0.0
- Requires-Dist: marshmallow-oneofschema[tests] ; extra == "dev"
- Requires-Dist: tox ; extra == "dev"
- Requires-Dist: pre-commit~=3.5 ; extra == "dev"
- Requires-Dist: pytest ; extra == "tests"
- Project-URL: Funding, https://opencollective.com/marshmallow
- Project-URL: Issues, https://github.com/marshmallow-code/marshmallow-oneofschema/issues
- Project-URL: Source, https://github.com/marshmallow-code/marshmallow-oneofschema
- Provides-Extra: dev
- Provides-Extra: tests
- =======================
- marshmallow-oneofschema
- =======================
- .. image:: https://github.com/marshmallow-code/marshmallow-oneofschema/actions/workflows/build-release.yml/badge.svg
- :target: https://github.com/marshmallow-code/flask-marshmallow/actions/workflows/build-release.yml
- :alt: Build Status
- .. image:: https://badgen.net/badge/marshmallow/3
- :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
- :alt: marshmallow 3 compatible
- An extension to marshmallow to support schema (de)multiplexing.
- marshmallow is a fantastic library for serialization and deserialization of data.
- For more on that project see its `GitHub <https://github.com/marshmallow-code/marshmallow>`_
- page or its `Documentation <http://marshmallow.readthedocs.org/en/latest/>`_.
- This library adds a special kind of schema that actually multiplexes other schemas
- based on object type. When serializing values, it uses get_obj_type() method
- to get object type name. Then it uses ``type_schemas`` name-to-Schema mapping
- to get schema for that particular object type, serializes object using that
- schema and adds an extra field with name of object type. Deserialization is reverse.
- Installing
- ----------
- ::
- $ pip install marshmallow-oneofschema
- Example
- -------
- The code below demonstrates how to set up a polymorphic schema. For the full context check out the tests.
- Once setup the schema should act like any other schema. If it does not then please file an Issue.
- .. code:: python
- import marshmallow
- import marshmallow.fields
- from marshmallow_oneofschema import OneOfSchema
- class Foo:
- def __init__(self, foo):
- self.foo = foo
- class Bar:
- def __init__(self, bar):
- self.bar = bar
- class FooSchema(marshmallow.Schema):
- foo = marshmallow.fields.String(required=True)
- @marshmallow.post_load
- def make_foo(self, data, **kwargs):
- return Foo(**data)
- class BarSchema(marshmallow.Schema):
- bar = marshmallow.fields.Integer(required=True)
- @marshmallow.post_load
- def make_bar(self, data, **kwargs):
- return Bar(**data)
- class MyUberSchema(OneOfSchema):
- type_schemas = {"foo": FooSchema, "bar": BarSchema}
- def get_obj_type(self, obj):
- if isinstance(obj, Foo):
- return "foo"
- elif isinstance(obj, Bar):
- return "bar"
- else:
- raise Exception("Unknown object type: {}".format(obj.__class__.__name__))
- MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
- # => [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]
- MyUberSchema().load(
- [{"type": "foo", "foo": "hello"}, {"type": "bar", "bar": 123}], many=True
- )
- # => [Foo('hello'), Bar(123)]
- By default get_obj_type() returns obj.__class__.__name__, so you can just reuse that
- to save some typing:
- .. code:: python
- class MyUberSchema(OneOfSchema):
- type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
- You can customize type field with `type_field` class property:
- .. code:: python
- class MyUberSchema(OneOfSchema):
- type_field = "object_type"
- type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
- MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
- # => [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]
- You can use resulting schema everywhere marshmallow.Schema can be used, e.g.
- .. code:: python
- import marshmallow as m
- import marshmallow.fields as f
- class MyOtherSchema(m.Schema):
- items = f.List(f.Nested(MyUberSchema))
- License
- -------
- MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/LICENSE>`_ file for more details.
|