Skip to content

Commit

Permalink
infer nested_name from class name
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Aug 15, 2022
1 parent 8870262 commit 6019143
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
26 changes: 15 additions & 11 deletions xrpl/models/nested_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

from __future__ import annotations

from typing import Any, ClassVar, Dict, Type
from typing import Any, Dict, Type, Union

from xrpl.models.base_model import BaseModel
from xrpl.models.base_model import BaseModel, _key_to_json


def _get_nested_name(cls: Union[NestedModel, Type[NestedModel]]) -> str:
if isinstance(cls, NestedModel):
name = cls.__class__.__name__
else:
name = cls.__name__
return _key_to_json(name)


class NestedModel(BaseModel):
"""The base class for models that involve a nested dictionary e.g. memos."""

nested_name: ClassVar[str]

@classmethod
def is_dict_of_model(cls: Type[NestedModel], dictionary: Any) -> bool:
"""
Expand All @@ -32,8 +38,8 @@ def is_dict_of_model(cls: Type[NestedModel], dictionary: Any) -> bool:
"""
return (
isinstance(dictionary, dict)
and cls.nested_name in dictionary
and super().is_dict_of_model(dictionary[cls.nested_name])
and _get_nested_name(cls) in dictionary
and super().is_dict_of_model(dictionary[_get_nested_name(cls)])
)

@classmethod
Expand All @@ -50,9 +56,9 @@ def from_dict(cls: Type[NestedModel], value: Dict[str, Any]) -> NestedModel:
Raises:
XRPLModelException: If the dictionary provided is invalid.
"""
if cls.nested_name not in value:
if _get_nested_name(cls) not in value:
return super(NestedModel, cls).from_dict(value)
return super(NestedModel, cls).from_dict(value[cls.nested_name])
return super(NestedModel, cls).from_dict(value[_get_nested_name(cls)])

def to_dict(self: NestedModel) -> Dict[str, Any]:
"""
Expand All @@ -61,6 +67,4 @@ def to_dict(self: NestedModel) -> Dict[str, Any]:
Returns:
The dictionary representation of a NestedModel.
"""
super_dict = super().to_dict()
del super_dict["nested_name"]
return {self.nested_name: super_dict}
return {_get_nested_name(self): super().to_dict()}
4 changes: 1 addition & 3 deletions xrpl/models/transactions/signer_list_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import ClassVar, Dict, List, Optional
from typing import Dict, List, Optional

from xrpl.models.nested_model import NestedModel
from xrpl.models.required import REQUIRED
Expand All @@ -16,8 +16,6 @@
class SignerEntry(NestedModel):
"""Represents one entry in a list of multi-signers authorized to an account."""

nested_name: ClassVar[str] = "signer_entry"

account: str = REQUIRED # type: ignore
"""
This field is required.
Expand Down
6 changes: 1 addition & 5 deletions xrpl/models/transactions/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from dataclasses import dataclass
from hashlib import sha512
from typing import Any, ClassVar, Dict, List, Optional, Type, TypeVar, Union
from typing import Any, Dict, List, Optional, Type, TypeVar, Union

from typing_extensions import Final

Expand Down Expand Up @@ -88,8 +88,6 @@ class Memo(NestedModel):
``memo_type``.
"""

nested_name: ClassVar[str] = "memo"

memo_data: Optional[str] = None
"""The data of the memo, as a hexadecimal string."""

Expand Down Expand Up @@ -134,8 +132,6 @@ class Signer(NestedModel):
field.
"""

nested_name: ClassVar[str] = "signer"

account: str = REQUIRED # type: ignore
"""
The address of the Signer. This can be a funded account in the XRP
Expand Down

0 comments on commit 6019143

Please sign in to comment.