Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/api/serializers.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""Decoder and encoders for swh-model objects.""" | """Decoder and encoders for swh-model objects.""" | ||||
from typing import Callable, Dict, List, Tuple | from typing import Any, Callable, Dict, List, Tuple | ||||
from swh.model.identifiers import SWHID, parse_swhid | from swh.model.identifiers import SWHID, parse_swhid | ||||
import swh.model.model as model | import swh.model.model as model | ||||
def _encode_model_object(obj): | def _encode_model_object(obj: model.BaseModel) -> Dict[str, Any]: | ||||
d = obj.to_dict() | d = obj.to_dict() | ||||
d["__type__"] = type(obj).__name__ | d["__type__"] = type(obj).__name__ | ||||
return d | return d | ||||
def _decode_model_object(d: Dict[str, Any]) -> model.BaseModel: | |||||
return getattr(model, d.pop("__type__")).from_dict(d) | |||||
def _encode_model_enum(obj): | def _encode_model_enum(obj): | ||||
return { | return { | ||||
"value": obj.value, | "value": obj.value, | ||||
ardumont: turns out that can be simplified to
```
"results": obj.results,
```
the remaining part of the… | |||||
"__type__": type(obj).__name__, | "__type__": type(obj).__name__, | ||||
} | } | ||||
ENCODERS: List[Tuple[type, str, Callable]] = [ | ENCODERS: List[Tuple[type, str, Callable]] = [ | ||||
Not Done Inline Actionsmake it check __type__is "PagedResult" vlorentz: make it check `__type__`is `"PagedResult"` | |||||
(model.BaseModel, "model", _encode_model_object), | (model.BaseModel, "model", _encode_model_object), | ||||
Not Done Inline Actions"storage" instead of "model_paged_result" vlorentz: `"storage"` instead of `"model_paged_result"` | |||||
Not Done Inline Actionsor "storage_class". I can't find a satisfying name. vlorentz: or `"storage_class"`. I can't find a satisfying name. | |||||
Done Inline Actions(I saw that when doing D3629 but waited on you to make a remark ;) well that's storage_model really. ardumont: (I saw that when doing D3629 but waited on you to make a remark ;)
well that's `storage_model`… | |||||
(SWHID, "swhid", str), | (SWHID, "swhid", str), | ||||
Done Inline Actionsturns out that can be simplified to results=obj["results"] ardumont: turns out that can be simplified to
```
results=obj["results"]
``` | |||||
(model.MetadataTargetType, "model_enum", _encode_model_enum), | (model.MetadataTargetType, "model_enum", _encode_model_enum), | ||||
(model.MetadataAuthorityType, "model_enum", _encode_model_enum), | (model.MetadataAuthorityType, "model_enum", _encode_model_enum), | ||||
] | ] | ||||
DECODERS: Dict[str, Callable] = { | DECODERS: Dict[str, Callable] = { | ||||
"swhid": parse_swhid, | "swhid": parse_swhid, | ||||
"model": lambda d: getattr(model, d.pop("__type__")).from_dict(d), | "model": _decode_model_object, | ||||
Not Done Inline Actionssame vlorentz: same | |||||
"model_enum": lambda d: getattr(model, d.pop("__type__"))(d["value"]), | "model_enum": lambda d: getattr(model, d.pop("__type__"))(d["value"]), | ||||
} | } |
turns out that can be simplified to
the remaining part of the serialization will do the rest.