1
- from typing import Any , Callable , Optional , TypeVar , Union
2
-
3
1
"""
4
2
RediSearch versions below 2.10 don't support indexing and querying
5
3
empty strings, so we use a sentinel value to represent empty strings.
8
6
sentinel values are from the first run of the graph, so this should
9
7
generally be correct.
10
8
"""
9
+
11
10
EMPTY_STRING_SENTINEL = "__empty__"
12
11
EMPTY_ID_SENTINEL = "00000000-0000-0000-0000-000000000000"
13
12
14
13
15
14
def to_storage_safe_str (value : str ) -> str :
16
15
"""
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.
19
20
20
21
Args:
21
22
value (str): The value to convert.
@@ -26,13 +27,13 @@ def to_storage_safe_str(value: str) -> str:
26
27
if value == "" :
27
28
return EMPTY_STRING_SENTINEL
28
29
else :
29
- return value
30
+ return str ( value )
30
31
31
32
32
33
def from_storage_safe_str (value : str ) -> str :
33
34
"""
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.
36
37
37
38
Args:
38
39
value (str): The value to convert.
@@ -48,8 +49,10 @@ def from_storage_safe_str(value: str) -> str:
48
49
49
50
def to_storage_safe_id (value : str ) -> str :
50
51
"""
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.
53
56
54
57
Args:
55
58
value (str): The value to convert.
@@ -60,13 +63,13 @@ def to_storage_safe_id(value: str) -> str:
60
63
if value == "" :
61
64
return EMPTY_ID_SENTINEL
62
65
else :
63
- return value
66
+ return str ( value )
64
67
65
68
66
69
def from_storage_safe_id (value : str ) -> str :
67
70
"""
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.
70
73
71
74
Args:
72
75
value (str): The value to convert.
@@ -78,36 +81,3 @@ def from_storage_safe_id(value: str) -> str:
78
81
return ""
79
82
else :
80
83
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