|
4 | 4 | import threading |
5 | 5 | from abc import ABC |
6 | 6 | from enum import Enum |
7 | | -from typing import Union, Optional, TYPE_CHECKING, List, Dict |
| 7 | +from typing import Union, Optional, TYPE_CHECKING, List, Dict, Generic, TypeVar |
8 | 8 |
|
9 | 9 | from ravendb.http.misc import LoadBalanceBehavior |
10 | 10 |
|
|
18 | 18 | from ravendb.documents import DocumentStore |
19 | 19 |
|
20 | 20 |
|
| 21 | +_T_Key = TypeVar("_T_Key") |
| 22 | +_T_Value = TypeVar("_T_Value") |
| 23 | + |
21 | 24 | class TransactionMode(Enum): |
22 | 25 | SINGLE_NODE = "single_node" |
23 | 26 | CLUSTER_WIDE = "cluster_wide" |
@@ -210,33 +213,36 @@ def remove_at(self, index: int) -> JavaScriptArray: |
210 | 213 | return self |
211 | 214 |
|
212 | 215 |
|
213 | | -class JavaScriptMap: |
| 216 | +class JavaScriptMap(Generic[_T_Key, _T_Value]): |
214 | 217 | def __init__(self, suffix: int, path_to_map: str): |
215 | | - self.__suffix = suffix |
216 | | - self.__path_to_map = path_to_map |
217 | | - self.__arg_counter = 0 |
218 | | - self.__script_lines = [] |
219 | | - self.__parameters: Dict[str, object] = {} |
| 218 | + self._suffix = suffix |
| 219 | + self._path_to_map = path_to_map |
| 220 | + self._arg_counter = 0 |
| 221 | + self._script_lines = [] |
| 222 | + self._parameters: Dict[str, object] = {} |
220 | 223 |
|
221 | 224 | @property |
222 | 225 | def script(self) -> str: |
223 | | - return "\r".join(self.__script_lines) |
| 226 | + return "\r".join(self._script_lines) |
224 | 227 |
|
225 | 228 | @property |
226 | 229 | def parameters(self) -> Dict[str, object]: |
227 | | - return self.__parameters |
| 230 | + return self._parameters |
228 | 231 |
|
229 | | - def __get_next_argument_name(self) -> str: |
230 | | - self.__arg_counter += 1 |
231 | | - return f"val_{self.__arg_counter - 1}_{self.__suffix}" |
| 232 | + def _get_next_argument_name(self) -> str: |
| 233 | + self._arg_counter += 1 |
| 234 | + return f"val_{self._arg_counter - 1}_{self._suffix}" |
232 | 235 |
|
233 | | - def put(self, key, value) -> JavaScriptMap: |
234 | | - argument_name = self.__get_next_argument_name() |
| 236 | + def put(self, key: _T_Key, value:_T_Value) -> JavaScriptMap[_T_Key, _T_Value]: |
| 237 | + argument_name = self._get_next_argument_name() |
235 | 238 |
|
236 | | - self.__script_lines.append(f"this.{self.__path_to_map}.{key} = args.{argument_name};") |
| 239 | + self._script_lines.append(f"this.{self._path_to_map}.{key} = args.{argument_name};") |
237 | 240 | self.parameters[argument_name] = value |
238 | 241 | return self |
239 | 242 |
|
| 243 | + def remove(self, key: _T_Key) -> JavaScriptMap[_T_Key, _T_Value]: |
| 244 | + self._script_lines.append(f"delete this.{self._path_to_map}.{key};") |
| 245 | + |
240 | 246 |
|
241 | 247 | class MethodCall(ABC): |
242 | 248 | def __init__(self, args: List[object] = None, access_path: str = None): |
|
0 commit comments