2020import typing as _typing
2121import datetime as _dt
2222import firebase_functions .private .util as _util
23+ import firebase_functions .private .path_pattern as _path_pattern
2324import firebase_functions .core as _core
2425import cloudevents .http as _ce
2526
@@ -91,12 +92,12 @@ class Event(_core.CloudEvent[_core.T]):
9192def _db_endpoint_handler (
9293 func : _C1 | _C2 ,
9394 event_type : str ,
95+ ref_pattern : _path_pattern .PathPattern ,
96+ instance_pattern : _path_pattern .PathPattern ,
9497 raw : _ce .CloudEvent ,
9598) -> None :
9699 event_attributes = raw ._get_attributes ()
97100 event_data : _typing .Any = raw .get_data ()
98- # TODO Params are built locally via path pattern which is currently unimplemented
99- params : dict [str , str ] = {}
100101 database_event_data = event_data
101102 if event_type == _event_type_deleted :
102103 database_event_data = database_event_data ["data" ]
@@ -112,10 +113,16 @@ def _db_endpoint_handler(
112113 before = before ,
113114 after = after ,
114115 )
116+ event_instance = event_attributes ["instance" ]
117+ event_ref = event_attributes ["ref" ]
118+ params : dict [str , str ] = {
119+ ** ref_pattern .extract_matches (event_ref ),
120+ ** instance_pattern .extract_matches (event_instance ),
121+ }
115122 database_event = Event (
116123 firebase_database_host = event_attributes ["firebasedatabasehost" ],
117- instance = event_attributes [ "instance" ] ,
118- reference = event_attributes [ "ref" ] ,
124+ instance = event_instance ,
125+ reference = event_ref ,
119126 location = event_attributes ["location" ],
120127 specversion = event_attributes ["specversion" ],
121128 id = event_attributes ["id" ],
@@ -155,15 +162,27 @@ def example(event: Event[Change[object]]) -> None:
155162 options = DatabaseOptions (** kwargs )
156163
157164 def on_value_written_inner_decorator (func : _C1 ):
165+ ref_pattern = _path_pattern .PathPattern (options .reference )
166+ instance_pattern = _path_pattern .PathPattern (
167+ options .instance if options .instance is not None else "*" )
158168
159169 @_functools .wraps (func )
160170 def on_value_written_wrapped (raw : _ce .CloudEvent ):
161- return _db_endpoint_handler (func , _event_type_written , raw )
171+ return _db_endpoint_handler (
172+ func ,
173+ _event_type_written ,
174+ ref_pattern ,
175+ instance_pattern ,
176+ raw ,
177+ )
162178
163179 _util .set_func_endpoint_attr (
164180 on_value_written_wrapped ,
165- options ._endpoint (event_type = _event_type_written ,
166- func_name = func .__name__ ),
181+ options ._endpoint (
182+ event_type = _event_type_written ,
183+ func_name = func .__name__ ,
184+ instance_pattern = instance_pattern ,
185+ ),
167186 )
168187 return on_value_written_wrapped
169188
@@ -193,15 +212,27 @@ def example(event: Event[Change[object]]) -> None:
193212 options = DatabaseOptions (** kwargs )
194213
195214 def on_value_updated_inner_decorator (func : _C1 ):
215+ ref_pattern = _path_pattern .PathPattern (options .reference )
216+ instance_pattern = _path_pattern .PathPattern (
217+ options .instance if options .instance is not None else "*" )
196218
197219 @_functools .wraps (func )
198220 def on_value_updated_wrapped (raw : _ce .CloudEvent ):
199- return _db_endpoint_handler (func , _event_type_updated , raw )
221+ return _db_endpoint_handler (
222+ func ,
223+ _event_type_updated ,
224+ ref_pattern ,
225+ instance_pattern ,
226+ raw ,
227+ )
200228
201229 _util .set_func_endpoint_attr (
202230 on_value_updated_wrapped ,
203- options ._endpoint (event_type = _event_type_updated ,
204- func_name = func .__name__ ),
231+ options ._endpoint (
232+ event_type = _event_type_updated ,
233+ func_name = func .__name__ ,
234+ instance_pattern = instance_pattern ,
235+ ),
205236 )
206237 return on_value_updated_wrapped
207238
@@ -231,15 +262,27 @@ def example(event: Event[object]):
231262 options = DatabaseOptions (** kwargs )
232263
233264 def on_value_created_inner_decorator (func : _C2 ):
265+ ref_pattern = _path_pattern .PathPattern (options .reference )
266+ instance_pattern = _path_pattern .PathPattern (
267+ options .instance if options .instance is not None else "*" )
234268
235269 @_functools .wraps (func )
236270 def on_value_created_wrapped (raw : _ce .CloudEvent ):
237- return _db_endpoint_handler (func , _event_type_created , raw )
271+ return _db_endpoint_handler (
272+ func ,
273+ _event_type_created ,
274+ ref_pattern ,
275+ instance_pattern ,
276+ raw ,
277+ )
238278
239279 _util .set_func_endpoint_attr (
240280 on_value_created_wrapped ,
241- options ._endpoint (event_type = _event_type_created ,
242- func_name = func .__name__ ),
281+ options ._endpoint (
282+ event_type = _event_type_created ,
283+ func_name = func .__name__ ,
284+ instance_pattern = instance_pattern ,
285+ ),
243286 )
244287 return on_value_created_wrapped
245288
@@ -269,15 +312,27 @@ def example(event: Event[object]) -> None:
269312 options = DatabaseOptions (** kwargs )
270313
271314 def on_value_deleted_inner_decorator (func : _C2 ):
315+ ref_pattern = _path_pattern .PathPattern (options .reference )
316+ instance_pattern = _path_pattern .PathPattern (
317+ options .instance if options .instance is not None else "*" )
272318
273319 @_functools .wraps (func )
274320 def on_value_deleted_wrapped (raw : _ce .CloudEvent ):
275- return _db_endpoint_handler (func , _event_type_deleted , raw )
321+ return _db_endpoint_handler (
322+ func ,
323+ _event_type_deleted ,
324+ ref_pattern ,
325+ instance_pattern ,
326+ raw ,
327+ )
276328
277329 _util .set_func_endpoint_attr (
278330 on_value_deleted_wrapped ,
279- options ._endpoint (event_type = _event_type_deleted ,
280- func_name = func .__name__ ),
331+ options ._endpoint (
332+ event_type = _event_type_deleted ,
333+ func_name = func .__name__ ,
334+ instance_pattern = instance_pattern ,
335+ ),
281336 )
282337 return on_value_deleted_wrapped
283338
0 commit comments