1- from typing import Any , Callable , Optional , TypeVar , Union
2-
31"""
42RediSearch versions below 2.10 don't support indexing and querying
53empty strings, so we use a sentinel value to represent empty strings.
86sentinel values are from the first run of the graph, so this should
97generally be correct.
108"""
9+
1110EMPTY_STRING_SENTINEL = "__empty__"
1211EMPTY_ID_SENTINEL = "00000000-0000-0000-0000-000000000000"
1312
1413
1514def to_storage_safe_str (value : str ) -> str :
1615 """
17- Convert any empty string to an empty string sentinel if found,
18- otherwise return the value unchanged.
16+ Prepare a value for storage in Redis as a string.
17+
18+ Convert an empty string to a sentinel value, otherwise return the
19+ value as a string.
1920
2021 Args:
2122 value (str): The value to convert.
@@ -26,13 +27,13 @@ def to_storage_safe_str(value: str) -> str:
2627 if value == "" :
2728 return EMPTY_STRING_SENTINEL
2829 else :
29- return value
30+ return str ( value )
3031
3132
3233def from_storage_safe_str (value : str ) -> str :
3334 """
34- Convert a value from an empty string sentinel to an empty string
35- if found, otherwise return the value unchanged.
35+ Convert a value from a sentinel value to an empty string if present,
36+ otherwise return the value unchanged.
3637
3738 Args:
3839 value (str): The value to convert.
@@ -48,8 +49,10 @@ def from_storage_safe_str(value: str) -> str:
4849
4950def to_storage_safe_id (value : str ) -> str :
5051 """
51- Convert any empty ID string to an empty ID sentinel if found,
52- otherwise return the value unchanged.
52+ Prepare a value for storage in Redis as an ID.
53+
54+ Convert an empty string to a sentinel value for empty ID strings, otherwise
55+ return the value as a string.
5356
5457 Args:
5558 value (str): The value to convert.
@@ -60,13 +63,13 @@ def to_storage_safe_id(value: str) -> str:
6063 if value == "" :
6164 return EMPTY_ID_SENTINEL
6265 else :
63- return value
66+ return str ( value )
6467
6568
6669def from_storage_safe_id (value : str ) -> str :
6770 """
68- Convert a value from an empty ID sentinel to an empty ID
69- if found , otherwise return the value unchanged.
71+ Convert a value from a sentinel value for empty ID strings to an empty
72+ ID string if present , otherwise return the value unchanged.
7073
7174 Args:
7275 value (str): The value to convert.
@@ -78,36 +81,3 @@ def from_storage_safe_id(value: str) -> str:
7881 return ""
7982 else :
8083 return value
81-
82-
83- def storage_safe_get (
84- doc : dict [str , Any ], key : str , default : Any = None
85- ) -> Optional [Any ]:
86- """
87- Get a value from a Redis document or dictionary, using a sentinel
88- value to represent empty strings.
89-
90- If the sentinel value is found, it is converted back to an empty string.
91-
92- Args:
93- doc (dict[str, Any]): The document to get the value from.
94- key (str): The key to get the value from.
95- default (Any): The default value to return if the key is not found.
96- Returns:
97- Optional[Any]: None if the key is not found, or else the value from
98- the document or dictionary, with empty strings converted
99- to the empty string sentinel and the sentinel converted
100- back to an empty string.
101- """
102- try :
103- # NOTE: The Document class that comes back from `search()` support
104- # [key] access but not `get()` for some reason, so we use direct
105- # key access with an exception guard.
106- value = doc [key ]
107- except KeyError :
108- value = None
109-
110- if value is None :
111- return default
112-
113- return to_storage_safe_str (value )
0 commit comments