From f79df2374a3225abac45601b7de26604538199b2 Mon Sep 17 00:00:00 2001 From: Scoder12 <34356756+Scoder12@users.noreply.github.com> Date: Tue, 11 Aug 2020 15:58:20 -0700 Subject: [PATCH] Add getitem and setitem to sync JSONKey --- src/replit/database/__init__.py | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/replit/database/__init__.py b/src/replit/database/__init__.py index 28a16d0..1afb619 100644 --- a/src/replit/database/__init__.py +++ b/src/replit/database/__init__.py @@ -429,6 +429,42 @@ class JSONKey(AsyncJSONKey): raise TypeError(self._type_mismatch_msg(data)) self.db[self.key] = json.dumps(data) + def __getitem__(self, name: str) -> JSON_TYPE: + """Retrieve a key from the JSONKey's value if it is a dict. + + Args: + name (str): The name to retrieve. + + Raises: + TypeError: The dtype attribute is not dict. + + Returns: + JSON_TYPE: The value of the key. + """ + if self.dtype is not dict: + raise TypeError( + "Dictionary syntax can only be used if the datatype is dict" + ) + return self.get()[name] + + def __setitem__(self, name: str, value: JSON_TYPE) -> None: + """Sets a key inside the JSONKey's value if it is a dict. + + Args: + name (str): The key to set. + value (JSON_TYPE): The value to set it to. + + Raises: + TypeError: The dtype attribute is not dict. + """ + if self.dtype is not dict: + raise TypeError( + "Dictionary syntax can only be used if the datatype is dict" + ) + data = self.get() + data[name] = value + self.set(data) + class ReplitDb(dict): """Interface with the Replit Database."""