Skip to content

Commit

Permalink
fix: schema-generator import improved (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosschroh authored May 25, 2020
1 parent 014521e commit 92d084a
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 42 deletions.
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Generate [Avro](https://avro.apache.org/docs/1.8.2/spec.html) Schemas from a Pyt

## Requirements

python 3.7+
`python 3.7+`

## Installation

Expand All @@ -23,8 +23,10 @@ https://marcosschroh.github.io/dataclasses-avroschema/

## Usage

### Generating the avro schema

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down Expand Up @@ -55,12 +57,12 @@ SchemaGenerator(User).avro_schema()
}'
```

and serialization
### Serialization to avro or avro-json

```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


@dataclass
Expand Down Expand Up @@ -101,6 +103,38 @@ schema.serialize(serialization_type="avro-json")
# >>> b'{"name": "john", "age": 20, "addresses": [{"street": "test", "street_number": 10}]}'
```

### Deserialization

Deserialization could take place with an instance dataclass or the dataclass itself

```python
import typing

from dataclasses_avroschema import SchemaGenerator


class Address:
"An Address"
street: str
street_number: int

class User:
"User with multiple Address"
name: str
age: int
addresses: typing.List[Address]

avro_binary = b"\x08john(\x02\x08test\x14\x00"
avro_json_binary = b'{"name": "john", "age": 20, "addresses": [{"street": "test", "street_number": 10}]}'
schema = SchemaGenerator(user)

schema.deserialize(avro_binary)
# >>> {"name": "john", "age": 20, "addresses": [{"street": "test", "street_number": 10}]}

schema.deserialize(avro_json_binary, serialization_type="avro-json")
# >>> {"name": "john", "age": 20, "addresses": [{"street": "test", "street_number": 10}]}
```

## Features

* [X] Primitive types: int, long, float, boolean, string and null support
Expand Down
1 change: 1 addition & 0 deletions dataclasses_avroschema/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .schema_generator import SchemaGenerator # noqa: 401
2 changes: 1 addition & 1 deletion docs/avro_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For example, a User may be defined with:
Image that you have to define the previous `User` schema but you do not know avro, you know python:

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

class User:
name: str
Expand Down
6 changes: 3 additions & 3 deletions docs/complex_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ import typing


from dataclasses_avroschema import types
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class UserAdvance:
Expand All @@ -188,7 +188,7 @@ import dataclasses
import datetime
import uuid

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

class UnionSchema:
"Some Unions"
Expand Down Expand Up @@ -335,7 +335,7 @@ There are some special avro attributes like `aliases`, `namespace` and `doc` (bo
The `doc` attribute can be set via the docstring class. The `aliases` and `namespaces` must be set using the `extra_avro_attributes` static method.

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down
2 changes: 1 addition & 1 deletion docs/faust_records.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import faust
import dataclasses
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class UserAdvance(faust.Record):
Expand Down
15 changes: 4 additions & 11 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Generate [Avro](https://avro.apache.org/docs/1.8.2/spec.html) Schemas from a Pyt

## Requirements

python 3.7+
`python 3.7+`

## Installation

Expand All @@ -19,8 +19,10 @@ pip install dataclasses-avroschema

## Usage

### Generating the avro schema

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down Expand Up @@ -51,12 +53,3 @@ SchemaGenerator(User).avro_schema()
]
}'
```

## Features

* [X] Primitive types: int, long, float, boolean, string and null support
* [X] Complex types: enum, array, map, fixed, unions and records support
* [x] Logical Types: date, time, datetime, uuid support
* [X] Schema relations (oneToOne, oneToMany)
* [X] Recursive Schemas
* [X] Generate Avro Schemas from `faust.Record`
8 changes: 4 additions & 4 deletions docs/logical_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The following list represent the avro logical types mapped to python types:
```python
import datetime

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

a_datetime = datetime.datetime(2019, 10, 12, 17, 57, 42)

Expand Down Expand Up @@ -64,7 +64,7 @@ SchemaGenerator(DateLogicalType).avro_schema()
```python
import datetime

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

a_datetime = datetime.datetime(2019, 10, 12, 17, 57, 42)

Expand Down Expand Up @@ -114,7 +114,7 @@ SchemaGenerator(TimeLogicalTypes).avro_schema()
```python
import datetime

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

a_datetime = datetime.datetime(2019, 10, 12, 17, 57, 42)

Expand Down Expand Up @@ -163,7 +163,7 @@ SchemaGenerator(DatetimeLogicalType).avro_schema()
```python
import uuid

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class UUIDLogicalTypes:
Expand Down
4 changes: 2 additions & 2 deletions docs/primitive_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The following list represent the avro primitive types mapped to python types:
Example:

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down Expand Up @@ -56,7 +56,7 @@ SchemaGenerator(User).avro_schema()
Example with defaults:

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down
10 changes: 5 additions & 5 deletions docs/schema_relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
An User has one Address example:

```python
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class Address:
Expand Down Expand Up @@ -50,7 +50,7 @@ An User with only one friend :-( :
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class User:
Expand Down Expand Up @@ -89,7 +89,7 @@ An User has multiple Address example:
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class Address:
Expand Down Expand Up @@ -137,7 +137,7 @@ or OneToMany using a Map:
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class Address:
Expand Down Expand Up @@ -188,7 +188,7 @@ An User with multiple friends :-) :
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

# Using a List (Avro Array)
class User:
Expand Down
4 changes: 2 additions & 2 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ For serialization is neccesary to use python dataclasses instances and not just
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


@dataclass
Expand Down Expand Up @@ -56,7 +56,7 @@ Deserialization could take place with an instance dataclass or the dataclass its
```python
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


class Address:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description=long_description,
long_description_content_type="text/markdown",
author="Marcos Schroh",
install_requires=["inflect==2.1.0", "fastavro"],
install_requires=["inflect", "fastavro"],
author_email="[email protected]",
url="https://github.com/marcosschroh/dataclasses-avroschema",
download_url="",
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_fastavro_paser_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from fastavro import parse_schema

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def test_minimal_schema(user_dataclass):
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_faust_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import faust

from dataclasses_avroschema import schema_definition, types
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

encoded = "test".encode()

Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_nested_schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def test_one_to_one_relationship(user_one_address_schema):
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_recursive_schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import typing

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def test_self_one_to_one_relationship(user_self_reference_one_to_one_schema):
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from dataclasses_avroschema.schema_definition import BaseSchemaDefinition
from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator

encoded = "test".encode()

Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_schema_with_complex_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
import uuid

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def test_schema_with_complex_types(user_advance_dataclass, user_advance_avro_json):
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_schema_with_logical_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import uuid

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def test_logical_types_schema(logical_types_schema):
Expand Down
2 changes: 1 addition & 1 deletion tests/schemas/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from dataclasses_avroschema.schema_generator import SchemaGenerator
from dataclasses_avroschema import SchemaGenerator


def create_user():
Expand Down

0 comments on commit 92d084a

Please sign in to comment.