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

Nested schema resolution directly from dictionaries #90

Closed
PMLP-novo opened this issue Feb 8, 2021 · 1 comment
Closed

Nested schema resolution directly from dictionaries #90

PMLP-novo opened this issue Feb 8, 2021 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@PMLP-novo
Copy link

PMLP-novo commented Feb 8, 2021

I want to be able to create nested objects in the definition of the primary object, such that I can pass in dictionaries that does not know about avro.

So from the tutorial:

from dataclasses import dataclass

import typing

from dataclasses_avroschema import AvroModel


@dataclass
class Address(AvroModel):
    "An Address"
    street: str
    street_number: int

@dataclass
class User(AvroModel):
    "User with multiple Address"
    name: str
    age: int
    addresses: typing.List[Address]

data_user = {
    "name": "john",
    "age": 20,
    "addresses": [ {
    "street": "test",
    "street_number": 10,
}],
}
user = User(**data_user)
assert type(user.addresses[0]) is Address

Inpiration for an implimentaion can be found on https://stackoverflow.com/questions/51564841/creating-nested-dataclass-objects-in-python/65326010

In order not to introduce backwards inconvertabilities an optional init argument or an alternative init could be introduced:


user = User(**data_user, nested=True)
assert type(user.addresses[0]) is Address


user = User.nested_init(**data_user)
assert type(user.addresses[0]) is Address
@marcosschroh marcosschroh added the help wanted Extra attention is needed label Apr 13, 2021
@marcosschroh
Copy link
Owner

Now you can use parse_obj to Cested schema resolution directly from dictionaries.
Documentation and for pydantic batteries

closed by #170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants