1818
1919
2020# Regexp patterns for matching
21- _TASK_NAME_MATCH = re .compile (r'tasks/([\w- ]+)' )
21+ _TASK_NAME_MATCH = re .compile (r'tasks/([^/ ]+)' )
2222_TASK_PUSH_CONFIG_NAME_MATCH = re .compile (
23- r'tasks/([\w- ]+)/pushNotificationConfigs/([\w- ]+)'
23+ r'tasks/([^/ ]+)/pushNotificationConfigs/([^/ ]+)'
2424)
2525
2626
@@ -46,6 +46,86 @@ def dict_to_struct(dictionary: dict[str, Any]) -> struct_pb2.Struct:
4646 return struct
4747
4848
49+ def make_dict_serializable (value : Any ) -> Any :
50+ """Dict pre-processing utility: converts non-serializable values to serializable form.
51+
52+ Use this when you want to normalize a dictionary before dict->Struct conversion.
53+
54+ Args:
55+ value: The value to convert.
56+
57+ Returns:
58+ A serializable value.
59+ """
60+ if isinstance (value , (str , int , float , bool )) or value is None :
61+ return value
62+ if isinstance (value , dict ):
63+ return {k : make_dict_serializable (v ) for k , v in value .items ()}
64+ if isinstance (value , list | tuple ):
65+ return [make_dict_serializable (item ) for item in value ]
66+ return str (value )
67+
68+
69+ def normalize_large_integers_to_strings (
70+ value : Any , max_safe_digits : int = 15
71+ ) -> Any :
72+ """Integer preprocessing utility: converts large integers to strings.
73+
74+ Use this when you want to convert large integers to strings considering
75+ JavaScript's MAX_SAFE_INTEGER (2^53 - 1) limitation.
76+
77+ Args:
78+ value: The value to convert.
79+ max_safe_digits: Maximum safe integer digits (default: 15).
80+
81+ Returns:
82+ A normalized value.
83+ """
84+ max_safe_int = 10 ** max_safe_digits - 1
85+
86+ def _normalize (item : Any ) -> Any :
87+ if isinstance (item , int ) and abs (item ) > max_safe_int :
88+ return str (item )
89+ if isinstance (item , dict ):
90+ return {k : _normalize (v ) for k , v in item .items ()}
91+ if isinstance (item , list | tuple ):
92+ return [_normalize (i ) for i in item ]
93+ return item
94+
95+ return _normalize (value )
96+
97+
98+ def parse_string_integers_in_dict (value : Any , max_safe_digits : int = 15 ) -> Any :
99+ """String post-processing utility: converts large integer strings back to integers.
100+
101+ Use this when you want to restore large integer strings to integers
102+ after Struct->dict conversion.
103+
104+ Args:
105+ value: The value to convert.
106+ max_safe_digits: Maximum safe integer digits (default: 15).
107+
108+ Returns:
109+ A parsed value.
110+ """
111+ if isinstance (value , dict ):
112+ return {
113+ k : parse_string_integers_in_dict (v , max_safe_digits )
114+ for k , v in value .items ()
115+ }
116+ if isinstance (value , list | tuple ):
117+ return [
118+ parse_string_integers_in_dict (item , max_safe_digits )
119+ for item in value
120+ ]
121+ if isinstance (value , str ):
122+ # Handle potential negative numbers.
123+ stripped_value = value .lstrip ('-' )
124+ if stripped_value .isdigit () and len (stripped_value ) > max_safe_digits :
125+ return int (value )
126+ return value
127+
128+
49129class ToProto :
50130 """Converts Python types to proto types."""
51131
0 commit comments