Skip to content

Commit 990bb95

Browse files
YunchuWangpeterstone2017
and
peterstone2017
authored
Add generic binding decorators/Modify existing decorators to enable decorator extensibility under new programming model (#108)
* Add generic binding decorators * address pr comments * update prog spec Co-authored-by: peterstone2017 <[email protected]>
1 parent 04ea33b commit 990bb95

24 files changed

+1076
-222
lines changed

azure/functions/decorators/blob.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def __init__(self,
1212
name: str,
1313
path: str,
1414
connection: str,
15-
data_type: Optional[DataType] = None):
15+
data_type: Optional[DataType] = None,
16+
**kwargs):
1617
self.path = path
1718
self.connection = connection
1819
super().__init__(name=name, data_type=data_type)
@@ -27,7 +28,8 @@ def __init__(self,
2728
name: str,
2829
path: str,
2930
connection: str,
30-
data_type: Optional[DataType] = None):
31+
data_type: Optional[DataType] = None,
32+
**kwargs):
3133
self.path = path
3234
self.connection = connection
3335
super().__init__(name=name, data_type=data_type)
@@ -42,7 +44,8 @@ def __init__(self,
4244
name: str,
4345
path: str,
4446
connection: str,
45-
data_type: Optional[DataType] = None):
47+
data_type: Optional[DataType] = None,
48+
**kwargs):
4649
self.path = path
4750
self.connection = connection
4851
super().__init__(name=name, data_type=data_type)

azure/functions/decorators/core.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
from abc import ABC, abstractmethod
4-
from typing import Dict, Optional
4+
from typing import Dict, Optional, Type
55

6-
from azure.functions.decorators.utils import to_camel_case, \
6+
from .utils import to_camel_case, \
77
ABCBuildDictMeta, StringifyEnum
88

99
SCRIPT_FILE_NAME = "function_app.py"
@@ -73,17 +73,22 @@ class Binding(ABC):
7373
attribute in function.json when new binding classes are created.
7474
Ref: https://aka.ms/azure-function-binding-http """
7575

76+
EXCLUDED_INIT_PARAMS = {'self', 'kwargs', 'type', 'data_type', 'direction'}
77+
7678
@staticmethod
7779
@abstractmethod
7880
def get_binding_name() -> str:
7981
pass
8082

8183
def __init__(self, name: str,
8284
direction: BindingDirection,
83-
is_trigger: bool,
84-
data_type: Optional[DataType] = None):
85-
self.type = self.get_binding_name()
86-
self.is_trigger = is_trigger
85+
data_type: Optional[DataType] = None,
86+
type: Optional[str] = None): # NoQa
87+
# For natively supported bindings, get_binding_name is always
88+
# implemented, and for generic bindings, type is a required argument
89+
# in decorator functions.
90+
self.type = self.get_binding_name() \
91+
if self.get_binding_name() is not None else type
8792
self.name = name
8893
self._direction = direction
8994
self._data_type = data_type
@@ -111,7 +116,7 @@ def get_dict_repr(self) -> Dict:
111116
:return: Dictionary representation of the binding.
112117
"""
113118
for p in getattr(self, 'init_params', []):
114-
if p not in ['data_type', 'self']:
119+
if p not in Binding.EXCLUDED_INIT_PARAMS:
115120
self._dict[to_camel_case(p)] = getattr(self, p, None)
116121

117122
return self._dict
@@ -121,24 +126,37 @@ class Trigger(Binding, ABC, metaclass=ABCBuildDictMeta):
121126
"""Class representation of Azure Function Trigger. \n
122127
Ref: https://aka.ms/functions-triggers-bindings-overview
123128
"""
124-
def __init__(self, name, data_type) -> None:
129+
130+
@staticmethod
131+
def is_supported_trigger_type(trigger_instance: 'Trigger',
132+
trigger_type: Type['Trigger']):
133+
return isinstance(trigger_instance,
134+
trigger_type) or trigger_instance.type == \
135+
trigger_type.get_binding_name()
136+
137+
def __init__(self, name: str, data_type: Optional[DataType] = None,
138+
type: Optional[str] = None) -> None:
125139
super().__init__(direction=BindingDirection.IN,
126-
name=name, data_type=data_type, is_trigger=True)
140+
name=name, data_type=data_type, type=type)
127141

128142

129143
class InputBinding(Binding, ABC, metaclass=ABCBuildDictMeta):
130144
"""Class representation of Azure Function Input Binding. \n
131145
Ref: https://aka.ms/functions-triggers-bindings-overview
132146
"""
133-
def __init__(self, name, data_type) -> None:
147+
148+
def __init__(self, name: str, data_type: Optional[DataType] = None,
149+
type: Optional[str] = None) -> None:
134150
super().__init__(direction=BindingDirection.IN,
135-
name=name, data_type=data_type, is_trigger=False)
151+
name=name, data_type=data_type, type=type)
136152

137153

138154
class OutputBinding(Binding, ABC, metaclass=ABCBuildDictMeta):
139155
"""Class representation of Azure Function Output Binding. \n
140156
Ref: https://aka.ms/functions-triggers-bindings-overview
141157
"""
142-
def __init__(self, name, data_type) -> None:
158+
159+
def __init__(self, name: str, data_type: Optional[DataType] = None,
160+
type: Optional[str] = None) -> None:
143161
super().__init__(direction=BindingDirection.OUT,
144-
name=name, data_type=data_type, is_trigger=False)
162+
name=name, data_type=data_type, type=type)

azure/functions/decorators/cosmosdb.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def __init__(self,
2020
data_type: Optional[DataType] = None,
2121
id: Optional[str] = None,
2222
sql_query: Optional[str] = None,
23-
partition_key: Optional[str] = None):
23+
partition_key: Optional[str] = None,
24+
**kwargs):
2425
self.database_name = database_name
2526
self.collection_name = collection_name
2627
self.connection_string_setting = connection_string_setting
@@ -45,7 +46,8 @@ def __init__(self,
4546
use_multiple_write_locations: Optional[bool] = None,
4647
preferred_locations: Optional[str] = None,
4748
partition_key: Optional[str] = None,
48-
data_type: Optional[DataType] = None):
49+
data_type: Optional[DataType] = None,
50+
**kwargs):
4951
self.database_name = database_name
5052
self.collection_name = collection_name
5153
self.connection_string_setting = connection_string_setting
@@ -83,7 +85,7 @@ def __init__(self,
8385
lease_connection_string_setting: Optional[str] = None,
8486
lease_database_name: Optional[str] = None,
8587
lease_collection_prefix: Optional[str] = None,
86-
):
88+
**kwargs):
8789
self.lease_collection_name = lease_collection_name
8890
self.lease_connection_string_setting = lease_connection_string_setting
8991
self.lease_database_name = lease_database_name

azure/functions/decorators/eventhub.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def __init__(self,
1919
event_hub_name: str,
2020
data_type: Optional[DataType] = None,
2121
cardinality: Optional[Cardinality] = None,
22-
consumer_group: Optional[str] = None):
22+
consumer_group: Optional[str] = None,
23+
**kwargs):
2324
self.connection = connection
2425
self.event_hub_name = event_hub_name
2526
self.cardinality = cardinality
@@ -37,7 +38,8 @@ def __init__(self,
3738
name: str,
3839
connection: str,
3940
event_hub_name: str,
40-
data_type: Optional[DataType] = None):
41+
data_type: Optional[DataType] = None,
42+
**kwargs):
4143
self.connection = connection
4244
self.event_hub_name = event_hub_name
4345
super().__init__(name=name, data_type=data_type)

0 commit comments

Comments
 (0)