-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Bootstrapped table schema * Bootstrapped reference generator * Bootstrapped tests * Improved schema model * Added validate assignment
- Loading branch information
Showing
9 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Schema | ||
|
||
::: dpspecs.Schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .models import * | ||
|
||
__all__ = ["Schema"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .schema import Schema | ||
|
||
__all__ = ["Schema"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import Any, Dict, List | ||
|
||
from pydantic import BaseModel, ValidationError | ||
from pydantic_core import ErrorDetails | ||
|
||
|
||
class Model(BaseModel, validate_assignment=True): | ||
def __str__(self): | ||
return str(self.to_descriptor()) | ||
|
||
@classmethod | ||
def validate_descriptor(cls, descriptor: Dict[str, Any]): | ||
errors: List[ErrorDetails] = [] | ||
try: | ||
cls.model_validate(descriptor) | ||
except ValidationError as e: | ||
errors = e.errors() | ||
return errors | ||
|
||
@classmethod | ||
def from_descriptor(cls, descriptor: Dict[str, Any]): | ||
return cls(**descriptor) | ||
|
||
def to_descriptor(self): | ||
return self.model_dump(exclude_unset=True, exclude_none=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Dict, List, Literal, Optional, Union | ||
|
||
import pydantic | ||
from typing_extensions import Annotated | ||
|
||
from .base import Model | ||
|
||
# Schema | ||
|
||
|
||
class Schema(Model): | ||
"""Schema model""" | ||
|
||
fields: List[Field] | ||
"""List of fields""" | ||
|
||
missingValues: Optional[List[str]] = None | ||
primaryKey: Optional[List[str]] = None | ||
foreignKeys: Optional[List[ForeignKey]] = None | ||
|
||
|
||
# Fields | ||
|
||
|
||
class BaseField(Model): | ||
name: str | ||
type: str | ||
title: Optional[str] = None | ||
description: Optional[str] = None | ||
format: Optional[str] = None | ||
missingValues: Optional[List[str]] = None | ||
|
||
|
||
class AnyField(BaseField): | ||
type: Literal["any"] = "any" | ||
|
||
|
||
class ArrayField(BaseField): | ||
type: Literal["array"] = "array" | ||
# support json/csv format | ||
arrayItem: Optional[Dict[str, Any]] = None | ||
|
||
|
||
class BooleanField(BaseField): | ||
type: Literal["boolean"] = "boolean" | ||
trueValues: Optional[List[str]] = None | ||
falseValues: Optional[List[str]] = None | ||
|
||
|
||
class DateField(BaseField): | ||
type: Literal["date"] = "date" | ||
|
||
|
||
class DatetimeField(BaseField): | ||
type: Literal["datetime"] = "datetime" | ||
|
||
|
||
class DurationField(BaseField): | ||
type: Literal["duration"] = "duration" | ||
|
||
|
||
class GeojsonField(BaseField): | ||
type: Literal["geojson"] = "geojson" | ||
|
||
|
||
class GeopointField(BaseField): | ||
type: Literal["geopoint"] = "geopoint" | ||
|
||
|
||
class IntegerField(BaseField): | ||
type: Literal["integer"] = "integer" | ||
bareNumber: Optional[bool] = None | ||
groupChar: Optional[str] = None | ||
|
||
|
||
class NumberField(BaseField): | ||
type: Literal["number"] = "number" | ||
bareNumber: Optional[bool] = None | ||
groupChar: Optional[str] = None | ||
decimalChar: Optional[str] = None | ||
|
||
|
||
class ObjectField(BaseField): | ||
type: Literal["object"] = "object" | ||
|
||
|
||
class StringField(BaseField): | ||
type: Literal["string"] = "string" | ||
|
||
|
||
class TimeField(BaseField): | ||
type: Literal["time"] = "time" | ||
|
||
|
||
class YearField(BaseField): | ||
type: Literal["year"] = "year" | ||
|
||
|
||
class YearmonthField(BaseField): | ||
type: Literal["yearmonth"] = "yearmonth" | ||
|
||
|
||
Field = Annotated[ | ||
Union[ | ||
AnyField, | ||
ArrayField, | ||
BooleanField, | ||
DateField, | ||
DatetimeField, | ||
DurationField, | ||
GeojsonField, | ||
GeopointField, | ||
IntegerField, | ||
NumberField, | ||
ObjectField, | ||
StringField, | ||
TimeField, | ||
YearField, | ||
YearmonthField, | ||
], | ||
pydantic.Field(discriminator="type"), | ||
] | ||
|
||
|
||
# Foreign keys | ||
|
||
|
||
class ForeignKey(Model): | ||
fields: List[str] | ||
reference: Optional[ForeignKeyReference] = None | ||
|
||
|
||
class ForeignKeyReference(Model): | ||
fields: List[str] | ||
resource: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from dpspecs import Schema | ||
|
||
|
||
def test_error(): | ||
schema = Schema(fields=[]) | ||
assert schema.fields == [] |