-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
291 additions
and
113 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,9 +1,9 @@ | ||
"""querytyper package.""" | ||
from querytyper.mongo import MongoModelMetaclass, MongoQuery, exists, regex_query | ||
from querytyper.mongo import MongoFilterMeta, MongoQuery, exists, regex_query | ||
|
||
__all__ = [ | ||
"MongoQuery", | ||
"MongoModelMetaclass", | ||
"MongoFilterMeta", | ||
"regex_query", | ||
"exists", | ||
] |
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,9 +1,10 @@ | ||
"""querytyper package.""" | ||
from querytyper.mongo.query import MongoModelMetaclass, MongoQuery, exists, regex_query | ||
from querytyper.mongo.meta import MongoFilterMeta | ||
from querytyper.mongo.query import MongoQuery, exists, regex_query | ||
|
||
__all__ = [ | ||
"MongoQuery", | ||
"MongoModelMetaclass", | ||
"MongoFilterMeta", | ||
"regex_query", | ||
"exists", | ||
] |
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,53 @@ | ||
"""MongoFilterMeta implementation.""" | ||
from typing import Any, Dict, Tuple, Type | ||
|
||
from pydantic import BaseModel | ||
from pydantic.main import ModelMetaclass | ||
|
||
from querytyper.mongo.query import QueryField | ||
|
||
DictStrAny = Dict[str, Any] | ||
|
||
|
||
class MongoFilterMeta(ModelMetaclass): | ||
""" | ||
Metaclass for query models. | ||
Use this metaclass to define query models. | ||
Example | ||
------- | ||
```python | ||
class TransactionFilter(Transaction, metaclass=MongoFilterMeta): | ||
pass | ||
``` | ||
""" | ||
|
||
def __new__( | ||
cls, | ||
name: str, | ||
bases: Tuple[Type[Any]], | ||
namespace: DictStrAny, | ||
) -> Type["MongoFilterMeta"]: | ||
"""Override new class creation to set query fields as class attributes.""" | ||
# check exactly one base class | ||
if len(bases) > 1: | ||
raise TypeError("MongoFilterMeta does not support multiple inheritance") | ||
if len(bases) < 1: | ||
raise TypeError( | ||
"The class with metaclass MongoFilterMeta requires a base class that inherits from BaseModel" | ||
) | ||
# check the base class is a subclass of BaseModel | ||
if not issubclass(bases[0], BaseModel): | ||
raise TypeError(f"The base class must inherit from {BaseModel.__qualname__}") | ||
base_model_cls: Type[BaseModel] = super().__new__(cls, name, bases, namespace) | ||
for field_name, model_field in base_model_cls.__fields__.items(): | ||
setattr( | ||
cls, | ||
field_name, | ||
QueryField[model_field.type_]( # type: ignore[name-defined] | ||
name=field_name, | ||
field_type=model_field.type_, | ||
), | ||
) | ||
return cls |
Oops, something went wrong.