-
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.
- Loading branch information
Showing
10 changed files
with
284 additions
and
163 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,45 @@ | ||
from typing import Iterable, List, Union | ||
|
||
from optimade.models import EntryResource | ||
from optimade.server.mappers.entries import ( | ||
BaseResourceMapper as OptimadeBaseResourceMapper, | ||
) | ||
|
||
|
||
class BaseResourceMapper(OptimadeBaseResourceMapper): | ||
""" | ||
Generic Resource Mapper that defines and performs the mapping | ||
between objects in the database and the resource objects defined by | ||
the specification. | ||
Note: | ||
This is a "wrapped" sub-class to make certain methods asynchronous. | ||
Attributes: | ||
ALIASES: a tuple of aliases between | ||
OPTIMADE field names and the field names in the database , | ||
e.g. `(("elements", "custom_elements_field"))`. | ||
LENGTH_ALIASES: a tuple of aliases between | ||
a field name and another field that defines its length, to be used | ||
when querying, e.g. `(("elements", "nelements"))`. | ||
e.g. `(("elements", "custom_elements_field"))`. | ||
ENTRY_RESOURCE_CLASS: The entry type that this mapper corresponds to. | ||
PROVIDER_FIELDS: a tuple of extra field names that this | ||
mapper should support when querying with the database prefix. | ||
TOP_LEVEL_NON_ATTRIBUTES_FIELDS: the set of top-level | ||
field names common to all endpoints. | ||
SUPPORTED_PREFIXES: The set of prefixes registered by this mapper. | ||
ALL_ATTRIBUTES: The set of attributes defined across the entry | ||
resource class and the server configuration. | ||
ENTRY_RESOURCE_ATTRIBUTES: A dictionary of attributes and their definitions | ||
defined by the schema of the entry resource class. | ||
ENDPOINT: The expected endpoint name for this resource, as defined by | ||
the `type` in the schema of the entry resource class. | ||
""" | ||
|
||
@classmethod | ||
async def deserialize( | ||
cls, results: Union[dict, Iterable[dict]] | ||
) -> Union[List[EntryResource], EntryResource]: | ||
return super(BaseResourceMapper, cls).deserialize(results) |
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
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,26 @@ | ||
from optimade.models.links import LinksResource | ||
|
||
from optimade_gateway.mappers.base import BaseResourceMapper | ||
|
||
__all__ = ("LinksMapper",) | ||
|
||
|
||
class LinksMapper(BaseResourceMapper): | ||
|
||
ENDPOINT = "links" | ||
ENTRY_RESOURCE_CLASS = LinksResource | ||
|
||
@classmethod | ||
def map_back(cls, doc: dict) -> dict: | ||
"""Map properties from MongoDB to OPTIMADE | ||
:param doc: A resource object in MongoDB format | ||
:type doc: dict | ||
:return: A resource object in OPTIMADE format | ||
:rtype: dict | ||
""" | ||
type_ = doc["type"] | ||
newdoc = super().map_back(doc) | ||
newdoc["type"] = type_ | ||
return newdoc |
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
Oops, something went wrong.