METADATA 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. Metadata-Version: 2.1
  2. Name: marshmallow-oneofschema
  3. Version: 3.1.1
  4. Summary: marshmallow multiplexing schema
  5. Author-email: Maxim Kulkin <maxim.kulkin@gmail.com>
  6. Maintainer-email: Steven Loria <sloria1@gmail.com>
  7. Requires-Python: >=3.8
  8. Description-Content-Type: text/x-rst
  9. Classifier: Intended Audience :: Developers
  10. Classifier: License :: OSI Approved :: MIT License
  11. Classifier: Programming Language :: Python :: 3
  12. Classifier: Programming Language :: Python :: 3.8
  13. Classifier: Programming Language :: Python :: 3.9
  14. Classifier: Programming Language :: Python :: 3.10
  15. Classifier: Programming Language :: Python :: 3.11
  16. Classifier: Programming Language :: Python :: 3.12
  17. Requires-Dist: marshmallow>=3.0.0,<4.0.0
  18. Requires-Dist: marshmallow-oneofschema[tests] ; extra == "dev"
  19. Requires-Dist: tox ; extra == "dev"
  20. Requires-Dist: pre-commit~=3.5 ; extra == "dev"
  21. Requires-Dist: pytest ; extra == "tests"
  22. Project-URL: Funding, https://opencollective.com/marshmallow
  23. Project-URL: Issues, https://github.com/marshmallow-code/marshmallow-oneofschema/issues
  24. Project-URL: Source, https://github.com/marshmallow-code/marshmallow-oneofschema
  25. Provides-Extra: dev
  26. Provides-Extra: tests
  27. =======================
  28. marshmallow-oneofschema
  29. =======================
  30. .. image:: https://github.com/marshmallow-code/marshmallow-oneofschema/actions/workflows/build-release.yml/badge.svg
  31. :target: https://github.com/marshmallow-code/flask-marshmallow/actions/workflows/build-release.yml
  32. :alt: Build Status
  33. .. image:: https://badgen.net/badge/marshmallow/3
  34. :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
  35. :alt: marshmallow 3 compatible
  36. An extension to marshmallow to support schema (de)multiplexing.
  37. marshmallow is a fantastic library for serialization and deserialization of data.
  38. For more on that project see its `GitHub <https://github.com/marshmallow-code/marshmallow>`_
  39. page or its `Documentation <http://marshmallow.readthedocs.org/en/latest/>`_.
  40. This library adds a special kind of schema that actually multiplexes other schemas
  41. based on object type. When serializing values, it uses get_obj_type() method
  42. to get object type name. Then it uses ``type_schemas`` name-to-Schema mapping
  43. to get schema for that particular object type, serializes object using that
  44. schema and adds an extra field with name of object type. Deserialization is reverse.
  45. Installing
  46. ----------
  47. ::
  48. $ pip install marshmallow-oneofschema
  49. Example
  50. -------
  51. The code below demonstrates how to set up a polymorphic schema. For the full context check out the tests.
  52. Once setup the schema should act like any other schema. If it does not then please file an Issue.
  53. .. code:: python
  54. import marshmallow
  55. import marshmallow.fields
  56. from marshmallow_oneofschema import OneOfSchema
  57. class Foo:
  58. def __init__(self, foo):
  59. self.foo = foo
  60. class Bar:
  61. def __init__(self, bar):
  62. self.bar = bar
  63. class FooSchema(marshmallow.Schema):
  64. foo = marshmallow.fields.String(required=True)
  65. @marshmallow.post_load
  66. def make_foo(self, data, **kwargs):
  67. return Foo(**data)
  68. class BarSchema(marshmallow.Schema):
  69. bar = marshmallow.fields.Integer(required=True)
  70. @marshmallow.post_load
  71. def make_bar(self, data, **kwargs):
  72. return Bar(**data)
  73. class MyUberSchema(OneOfSchema):
  74. type_schemas = {"foo": FooSchema, "bar": BarSchema}
  75. def get_obj_type(self, obj):
  76. if isinstance(obj, Foo):
  77. return "foo"
  78. elif isinstance(obj, Bar):
  79. return "bar"
  80. else:
  81. raise Exception("Unknown object type: {}".format(obj.__class__.__name__))
  82. MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
  83. # => [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]
  84. MyUberSchema().load(
  85. [{"type": "foo", "foo": "hello"}, {"type": "bar", "bar": 123}], many=True
  86. )
  87. # => [Foo('hello'), Bar(123)]
  88. By default get_obj_type() returns obj.__class__.__name__, so you can just reuse that
  89. to save some typing:
  90. .. code:: python
  91. class MyUberSchema(OneOfSchema):
  92. type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
  93. You can customize type field with `type_field` class property:
  94. .. code:: python
  95. class MyUberSchema(OneOfSchema):
  96. type_field = "object_type"
  97. type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
  98. MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
  99. # => [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]
  100. You can use resulting schema everywhere marshmallow.Schema can be used, e.g.
  101. .. code:: python
  102. import marshmallow as m
  103. import marshmallow.fields as f
  104. class MyOtherSchema(m.Schema):
  105. items = f.List(f.Nested(MyUberSchema))
  106. License
  107. -------
  108. MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/LICENSE>`_ file for more details.