Better Type Parsing Interface

This commit is contained in:
2025-03-28 02:03:42 -03:00
parent 529a35d8bd
commit 9f9b900e27
13 changed files with 258 additions and 148 deletions

View File

@@ -0,0 +1,10 @@
# Exports generic type parser
from ._type_parser import GenericTypeParser
# Exports Implementations
from .int_type_parser import IntTypeParser # isort:skip
from .object_type_parser import ObjectTypeParser # isort:skip
from .string_type_parser import StringTypeParser # isort:skip
from .array_type_parser import ArrayTypeParser # isort:skip
from .boolean_type_parser import BooleanTypeParser # isort:skip
from .float_type_parser import FloatTypeParser # isort:skip

View File

@@ -0,0 +1,28 @@
from abc import ABC, abstractmethod
from typing import Generic, Self, TypeVar
T = TypeVar("T")
class GenericTypeParser(ABC, Generic[T]):
@property
@abstractmethod
def mapped_type(self) -> type[T]: ...
@property
@abstractmethod
def json_schema_type(self) -> str: ...
@staticmethod
@abstractmethod
def from_properties(
name: str, properties: dict[str, any]
) -> tuple[type[T], dict[str, any]]: ...
@classmethod
def get_impl(cls, type_name: str) -> Self:
for subcls in cls.__subclasses__():
if subcls.json_schema_type == type_name:
return subcls
raise ValueError(f"Unknown type: {type_name}")

View File

@@ -0,0 +1,21 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
from typing import TypeVar
V = TypeVar("V")
class ArrayTypeParser(GenericTypeParser):
mapped_type = list
json_schema_type = "array"
@classmethod
def from_properties(cls, name, properties):
_item_type = properties["items"]["type"]
if _item_type == "object":
_item_type = type
else:
_item_type = GenericTypeParser.get_impl(_item_type).mapped_type
return list[_item_type], {}

View File

@@ -0,0 +1,11 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
class BooleanTypeParser(GenericTypeParser):
mapped_type = bool
json_schema_type = "boolean"
@staticmethod
def from_properties(name, properties):
return bool, {}

View File

@@ -0,0 +1,11 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
class FloatTypeParser(GenericTypeParser):
mapped_type = float
json_schema_type = "number"
@staticmethod
def from_properties(name, properties):
return float, {}

View File

@@ -0,0 +1,11 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
class IntTypeParser(GenericTypeParser):
mapped_type = int
json_schema_type = "integer"
@staticmethod
def from_properties(name, properties):
return int, {}

View File

@@ -0,0 +1,14 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
class ObjectTypeParser(GenericTypeParser):
mapped_type = object
json_schema_type = "object"
@staticmethod
def from_properties(name, properties):
from jsonschema_pydantic.schema_converter import SchemaConverter
_type = SchemaConverter.build_object(name, properties)
return _type, {}

View File

@@ -0,0 +1,11 @@
from jsonschema_pydantic.types._type_parser import GenericTypeParser
class StringTypeParser(GenericTypeParser):
mapped_type = str
json_schema_type = "string"
@staticmethod
def from_properties(name, properties):
return str, {}