1414
1515"""cel-expr-python registration for protovalidate's custom CEL functions."""
1616
17+ from __future__ import annotations
18+
1719from cel_expr_python import cel
18- from google .protobuf import descriptor as google_descriptor
19- from google .protobuf import message as google_message
20- from google .protobuf import wrappers_pb2 as google_wrappers_pb2
20+ from google .protobuf import (
21+ descriptor as google_descriptor ,
22+ message as google_message ,
23+ wrappers_pb2 as google_wrappers_pb2 ,
24+ )
2125
22- from protovalidate . internal import _funcs
26+ from protovalidate import _funcs
2327
2428from ._utils import is_repeated
2529
2630
2731def cel_get_field (message : object , field_name : object ) -> object :
2832 if not isinstance (message , google_message .Message ):
2933 msg = "invalid argument, expected message"
30- raise ValueError (msg )
34+ raise TypeError (msg )
3135 if not isinstance (field_name , str ):
3236 msg = "invalid argument, expected string"
33- raise ValueError (msg )
37+ raise TypeError (msg )
3438 if field_name not in message .DESCRIPTOR .fields_by_name :
3539 msg = f"no such field: { field_name } "
3640 raise ValueError (msg )
@@ -50,7 +54,7 @@ def cel_get_field(message: object, field_name: object) -> object:
5054def cel_unique (val : object ) -> bool :
5155 if not isinstance (val , list ):
5256 msg = "invalid argument, expected list"
53- raise ValueError (msg )
57+ raise TypeError (msg )
5458 # Track seen values keyed by (type, value) so that distinct CEL types that
5559 # are equal in Python (notably bool vs int: ``True == 1``) are not treated
5660 # as duplicates, and so that bytes are never confused with strings.
@@ -97,59 +101,157 @@ def make_extension() -> cel.CelExtension:
97101 return cel .CelExtension (
98102 "protovalidate" ,
99103 [
100- cel .FunctionDecl ("getField" , [cel .Overload ("get_field" , _dyn , [_dyn , _s ], impl = cel_get_field )]),
101104 cel .FunctionDecl (
102- "isNan" , [cel .Overload ("double_is_nan" , _b , [_d ], is_member = True , impl = _funcs .cel_is_nan )]
105+ "getField" ,
106+ [cel .Overload ("get_field" , _dyn , [_dyn , _s ], impl = cel_get_field )],
107+ ),
108+ cel .FunctionDecl (
109+ "isNan" ,
110+ [
111+ cel .Overload (
112+ "double_is_nan" ,
113+ _b ,
114+ [_d ],
115+ is_member = True ,
116+ impl = _funcs .cel_is_nan ,
117+ )
118+ ],
103119 ),
104120 cel .FunctionDecl (
105121 "isInf" ,
106122 [
107- cel .Overload ("double_is_inf" , _b , [_d ], is_member = True , impl = _funcs .cel_is_inf ),
108- cel .Overload ("double_int_is_inf" , _b , [_d , _i ], is_member = True , impl = _funcs .cel_is_inf ),
123+ cel .Overload (
124+ "double_is_inf" ,
125+ _b ,
126+ [_d ],
127+ is_member = True ,
128+ impl = _funcs .cel_is_inf ,
129+ ),
130+ cel .Overload (
131+ "double_int_is_inf" ,
132+ _b ,
133+ [_d , _i ],
134+ is_member = True ,
135+ impl = _funcs .cel_is_inf ,
136+ ),
109137 ],
110138 ),
111139 cel .FunctionDecl (
112140 "isIp" ,
113141 [
114- cel .Overload ("string_is_ip" , _b , [_s ], is_member = True , impl = _funcs .cel_is_ip ),
115- cel .Overload ("string_int_is_ip" , _b , [_s , _i ], is_member = True , impl = _funcs .cel_is_ip ),
142+ cel .Overload (
143+ "string_is_ip" , _b , [_s ], is_member = True , impl = _funcs .cel_is_ip
144+ ),
145+ cel .Overload (
146+ "string_int_is_ip" ,
147+ _b ,
148+ [_s , _i ],
149+ is_member = True ,
150+ impl = _funcs .cel_is_ip ,
151+ ),
116152 ],
117153 ),
118154 cel .FunctionDecl (
119155 "isIpPrefix" ,
120156 [
121- cel .Overload ("string_is_ip_prefix" , _b , [_s ], is_member = True , impl = _funcs .cel_is_ip_prefix ),
122- cel .Overload ("string_int_is_ip_prefix" , _b , [_s , _i ], is_member = True , impl = _funcs .cel_is_ip_prefix ),
123157 cel .Overload (
124- "string_bool_is_ip_prefix" , _b , [_s , _b ], is_member = True , impl = _funcs .cel_is_ip_prefix
158+ "string_is_ip_prefix" ,
159+ _b ,
160+ [_s ],
161+ is_member = True ,
162+ impl = _funcs .cel_is_ip_prefix ,
125163 ),
126164 cel .Overload (
127- "string_int_bool_is_ip_prefix" , _b , [_s , _i , _b ], is_member = True , impl = _funcs .cel_is_ip_prefix
165+ "string_int_is_ip_prefix" ,
166+ _b ,
167+ [_s , _i ],
168+ is_member = True ,
169+ impl = _funcs .cel_is_ip_prefix ,
170+ ),
171+ cel .Overload (
172+ "string_bool_is_ip_prefix" ,
173+ _b ,
174+ [_s , _b ],
175+ is_member = True ,
176+ impl = _funcs .cel_is_ip_prefix ,
177+ ),
178+ cel .Overload (
179+ "string_int_bool_is_ip_prefix" ,
180+ _b ,
181+ [_s , _i , _b ],
182+ is_member = True ,
183+ impl = _funcs .cel_is_ip_prefix ,
128184 ),
129185 ],
130186 ),
131187 cel .FunctionDecl (
132- "isEmail" , [cel .Overload ("string_is_email" , _b , [_s ], is_member = True , impl = _funcs .cel_is_email )]
188+ "isEmail" ,
189+ [
190+ cel .Overload (
191+ "string_is_email" ,
192+ _b ,
193+ [_s ],
194+ is_member = True ,
195+ impl = _funcs .cel_is_email ,
196+ )
197+ ],
133198 ),
134199 cel .FunctionDecl (
135- "isUri" , [cel .Overload ("string_is_uri" , _b , [_s ], is_member = True , impl = _funcs .cel_is_uri )]
200+ "isUri" ,
201+ [
202+ cel .Overload (
203+ "string_is_uri" ,
204+ _b ,
205+ [_s ],
206+ is_member = True ,
207+ impl = _funcs .cel_is_uri ,
208+ )
209+ ],
136210 ),
137211 cel .FunctionDecl (
138- "isUriRef" , [cel .Overload ("string_is_uri_ref" , _b , [_s ], is_member = True , impl = _funcs .cel_is_uri_ref )]
212+ "isUriRef" ,
213+ [
214+ cel .Overload (
215+ "string_is_uri_ref" ,
216+ _b ,
217+ [_s ],
218+ is_member = True ,
219+ impl = _funcs .cel_is_uri_ref ,
220+ )
221+ ],
139222 ),
140223 cel .FunctionDecl (
141224 "isHostname" ,
142- [cel .Overload ("string_is_hostname" , _b , [_s ], is_member = True , impl = _funcs .cel_is_hostname )],
225+ [
226+ cel .Overload (
227+ "string_is_hostname" ,
228+ _b ,
229+ [_s ],
230+ is_member = True ,
231+ impl = _funcs .cel_is_hostname ,
232+ )
233+ ],
143234 ),
144235 cel .FunctionDecl (
145236 "isHostAndPort" ,
146237 [
147238 cel .Overload (
148- "string_bool_is_host_and_port" , _b , [_s , _b ], is_member = True , impl = _funcs .cel_is_host_and_port
239+ "string_bool_is_host_and_port" ,
240+ _b ,
241+ [_s , _b ],
242+ is_member = True ,
243+ impl = _funcs .cel_is_host_and_port ,
244+ )
245+ ],
246+ ),
247+ cel .FunctionDecl (
248+ "unique" ,
249+ [
250+ cel .Overload (
251+ "list_unique" , _b , [_l ], is_member = True , impl = cel_unique
149252 )
150253 ],
151254 ),
152- cel .FunctionDecl ("unique" , [cel .Overload ("list_unique" , _b , [_l ], is_member = True , impl = cel_unique )]),
153255 cel .FunctionDecl (
154256 "startsWith" ,
155257 [
@@ -166,15 +268,23 @@ def make_extension() -> cel.CelExtension:
166268 "endsWith" ,
167269 [
168270 cel .Overload (
169- "bytes_ends_with" , _b , [cel .Type .BYTES , cel .Type .BYTES ], is_member = True , impl = _bytes_ends_with
271+ "bytes_ends_with" ,
272+ _b ,
273+ [cel .Type .BYTES , cel .Type .BYTES ],
274+ is_member = True ,
275+ impl = _bytes_ends_with ,
170276 )
171277 ],
172278 ),
173279 cel .FunctionDecl (
174280 "contains" ,
175281 [
176282 cel .Overload (
177- "bytes_contains" , _b , [cel .Type .BYTES , cel .Type .BYTES ], is_member = True , impl = _bytes_contains
283+ "bytes_contains" ,
284+ _b ,
285+ [cel .Type .BYTES , cel .Type .BYTES ],
286+ is_member = True ,
287+ impl = _bytes_contains ,
178288 )
179289 ],
180290 ),
0 commit comments