Skip to content

Commit

Permalink
Add helper method to retrieve declared type (#18)
Browse files Browse the repository at this point in the history
* Add helper function to retrieve declared type

* Use helper in HierarkeyProxy
  • Loading branch information
luelista authored May 3, 2024
1 parent c81ace0 commit d834d5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions hierarkey/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def add_default(self, key: str, value: Optional[str], default_type: type = str)
"""
self.defaults[key] = HierarkeyDefault(value, default_type)

def get_declared_type(self, key: str) -> type:
"""
Returns the type that is declared for a key using add_default.
:param key: Key
"""
return self.defaults[key].type if key in self.defaults else None

def add_type(self, type: type, serialize: Callable[[Any], str], unserialize: Callable[[str], Any]) -> None:
"""
Adds serialization support for a new type.
Expand Down
4 changes: 2 additions & 2 deletions hierarkey/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def get(self, key: str, default=None, as_type: type = None, binary_file: bool =
If you receive a ``File`` object, it will already be opened. You can specify the ``binary_file``
flag to indicate that it should be opened in binary mode.
"""
if as_type is None and key in self._h.defaults:
as_type = self._h.defaults[key].type
if as_type is None:
as_type = self._h.get_declared_type(key)

if key in self._cache():
value = self._cache()[key]
Expand Down

0 comments on commit d834d5a

Please sign in to comment.