-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6d33b3
commit 2a6c712
Showing
5 changed files
with
81 additions
and
187 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,70 +1,62 @@ | ||
from datetime import datetime | ||
from typing import Any, Type, Union | ||
""" | ||
The `pydantic_extra_types.ULID` module provides the [`ULID`] data type. | ||
This class depends on the [python-ulid] package, which is a validate by the [ULID-spec](https://github.com/ulid/spec#implementations-in-other-languages). | ||
""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Any, Union | ||
|
||
from pydantic import GetCoreSchemaHandler | ||
from pydantic._internal import _repr | ||
from pydantic_core import PydanticCustomError, core_schema | ||
|
||
try: | ||
from ulid import ULID as _ULID | ||
except ModuleNotFoundError: # pragma: no cover | ||
raise RuntimeError( | ||
'The `ulid` module requires "python-ulid" to be installed. You can install it with "pip install python-ulid".' | ||
) | ||
|
||
|
||
UlidType = Union[str, bytes, int] | ||
|
||
|
||
class ULID(str): | ||
@dataclass | ||
class ULID(_repr.Representation): | ||
""" | ||
Represents an ULID - https://github.com/ulid/spec | ||
A wrapper around [python-ulid](https://pypi.org/project/python-ulid/) package, which | ||
is a validate by the [ULID-spec](https://github.com/ulid/spec#implementations-in-other-languages). | ||
""" | ||
|
||
_ulid: _ULID | ||
|
||
def __init__(self, value: UlidType): | ||
if isinstance(value, (bytes, str, int)): | ||
self._ulid = self.validate_ulid(value) | ||
else: | ||
raise PydanticCustomError( | ||
'ulid_error', | ||
'value is not a valid ULID: value must be a string, int or bytes', | ||
) | ||
ulid: _ULID | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, source: Type[Any], handler: GetCoreSchemaHandler | ||
) -> core_schema.AfterValidatorFunctionSchema: | ||
return core_schema.general_after_validator_function( | ||
cls.validate, | ||
core_schema.str_schema(), | ||
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: | ||
return core_schema.no_info_wrap_validator_function( | ||
cls._validate_ulid, | ||
core_schema.union_schema( | ||
[ | ||
core_schema.is_instance_schema(_ULID), | ||
core_schema.int_schema(), | ||
core_schema.bytes_schema(), | ||
core_schema.str_schema(), | ||
] | ||
), | ||
) | ||
|
||
@classmethod | ||
def validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> 'ULID': | ||
return cls(__input_value) | ||
|
||
@classmethod | ||
def validate_ulid(cls, value: UlidType) -> _ULID: | ||
def _validate_ulid(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler) -> Any: | ||
ulid: _ULID | ||
try: | ||
if isinstance(value, int): | ||
ulid = _ULID.from_int(value) | ||
elif isinstance(value, str): | ||
ulid = _ULID.from_str(value) | ||
elif isinstance(value, _ULID): | ||
ulid = value | ||
else: | ||
ulid = _ULID.from_bytes(value) | ||
except ValueError as e: | ||
raise PydanticCustomError('ulid_format', 'Unrecognized format') from e | ||
return ulid | ||
|
||
@property | ||
def hex(self) -> str: | ||
return self._ulid.hex | ||
|
||
@property | ||
def timestamp(self) -> float: | ||
return self._ulid.timestamp | ||
|
||
@property | ||
def datetime(self) -> datetime: | ||
return self._ulid.datetime | ||
except ValueError: | ||
raise PydanticCustomError('ulid_format', 'Unrecognized format') | ||
return handler(ulid) |
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