Skip to content

Commit

Permalink
feat: model generator added
Browse files Browse the repository at this point in the history
feat: self relationship schema generation

feat: logicalTypes model generation

feat: logical types model generator

fix: field order, pascal case for class names

fix: add class Meta support
  • Loading branch information
marcosschroh committed Jan 9, 2023
1 parent aaee21c commit dba09d6
Show file tree
Hide file tree
Showing 20 changed files with 1,961 additions and 190 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

The MIT License (MIT)
Copyright (c) 2021, Marcos Schroh
Copyright (c) 2022, Marcos Schroh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ User.fake()
* [X] Data deserialization. Return python dict or class instance
* [X] Generate json from python class instance
* [X] Case Schemas
* [X] Generate models from `avsc` files
* [X] Examples of integration with `kafka` drivers: [aiokafka](https://github.com/aio-libs/aiokafka), [kafka-python](https://github.com/dpkp/kafka-python)
* [X] Example of integration with `redis` drivers: [walrus](https://github.com/coleifer/walrus) and [redisgears-py](https://github.com/RedisGears/redisgears-py)
* [X] Factory instances
Expand Down
2 changes: 2 additions & 0 deletions dataclasses_avroschema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .field_utils import * # noqa: 401
from .model_generator.generator import BaseClassEnum, ModelGenerator # noqa: 401
from .schema_generator import AvroModel # noqa: 401
from .types import * # noqa: 401
2 changes: 1 addition & 1 deletion dataclasses_avroschema/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import stringcase

from .fields import ENUM
from .field_utils import ENUM

# Summary from https://github.com/okunishinishi/python-stringcase
# stringcase.camelcase('foo_bar_baz') # => "fooBarBaz"
Expand Down
122 changes: 122 additions & 0 deletions dataclasses_avroschema/field_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,126 @@
import datetime
import decimal
import enum
import typing
import uuid

from . import types

__all__ = [
"BOOLEAN",
"NULL",
"INT",
"FLOAT",
"LONG",
"DOUBLE",
"BYTES",
"STRING",
"ARRAY",
"ENUM",
"MAP",
"FIXED",
"DATE",
"UUID",
"DECIMAL",
"RECORD",
"TIME_MILLIS",
"TIME_MICROS",
"TIMESTAMP_MILLIS",
"TIMESTAMP_MICROS",
"LOGICAL_DATE",
"LOGICAL_TIME_MILIS",
"LOGICAL_TIME_MICROS",
"LOGICAL_DATETIME_MILIS",
"LOGICAL_DATETIME_MICROS",
"LOGICAL_UUID",
"PYTHON_TYPE_TO_AVRO",
"PYTHON_INMUTABLE_TYPES",
"PYTHON_PRIMITIVE_CONTAINERS",
"PYTHON_LOGICAL_TYPES",
"PYTHON_PRIMITIVE_TYPES",
"PRIMITIVE_AND_LOGICAL_TYPES",
"PythonImmutableTypes",
]

TIME_MILLIS = "time-millis"
TIME_MICROS = "time-micros"
TIMESTAMP_MILLIS = "timestamp-millis"
TIMESTAMP_MICROS = "timestamp-micros"

BOOLEAN = "boolean"
NULL = "null"
INT = "int"
FLOAT = "float"
LONG = "long"
DOUBLE = "double"
BYTES = "bytes"
STRING = "string"
ARRAY = "array"
ENUM = "enum"
MAP = "map"
FIXED = "fixed"
DATE = "date"
UUID = "uuid"
DECIMAL = "decimal"
RECORD = "record"
LOGICAL_DATE = {"type": INT, "logicalType": DATE}
LOGICAL_TIME_MILIS = {"type": INT, "logicalType": TIME_MILLIS}
LOGICAL_TIME_MICROS = {"type": LONG, "logicalType": TIME_MICROS}
LOGICAL_DATETIME_MILIS = {"type": LONG, "logicalType": TIMESTAMP_MILLIS}
LOGICAL_DATETIME_MICROS = {"type": LONG, "logicalType": TIMESTAMP_MICROS}
LOGICAL_UUID = {"type": STRING, "logicalType": UUID}

PYTHON_TYPE_TO_AVRO = {
bool: BOOLEAN,
type(None): NULL,
int: LONG,
float: DOUBLE,
bytes: BYTES,
str: STRING,
list: {"type": ARRAY},
tuple: {"type": ARRAY},
dict: {"type": MAP},
enum.Enum: {"type": ENUM},
types.Fixed: {"type": FIXED},
types.Int32: INT,
types.Float32: FLOAT,
datetime.date: {"type": INT, "logicalType": DATE},
datetime.time: {"type": INT, "logicalType": TIME_MILLIS},
datetime.datetime: {"type": LONG, "logicalType": TIMESTAMP_MILLIS},
uuid.uuid4: {"type": STRING, "logicalType": UUID},
}

# excluding tuple because is a container
PYTHON_INMUTABLE_TYPES = (str, int, types.Int32, types.Float32, bool, float, bytes, type(None))
PYTHON_PRIMITIVE_CONTAINERS = (list, tuple, dict)

PYTHON_LOGICAL_TYPES = (
datetime.date,
datetime.time,
types.TimeMicro,
datetime.datetime,
types.DateTimeMicro,
uuid.uuid4,
uuid.UUID,
decimal.Decimal,
)

PYTHON_PRIMITIVE_TYPES = PYTHON_INMUTABLE_TYPES + PYTHON_PRIMITIVE_CONTAINERS
PRIMITIVE_AND_LOGICAL_TYPES = PYTHON_INMUTABLE_TYPES + PYTHON_LOGICAL_TYPES

PythonImmutableTypes = typing.Union[
str,
int,
types.Int32,
bool,
float,
types.Float32,
list,
tuple,
dict,
datetime.date,
datetime.time,
datetime.datetime,
uuid.UUID,
decimal.Decimal,
]
Loading

0 comments on commit dba09d6

Please sign in to comment.