1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
from abc import ABC , abstractmethod
4
- from typing import Dict , Optional
4
+ from typing import Dict , Optional , Type
5
5
6
- from azure . functions . decorators .utils import to_camel_case , \
6
+ from .utils import to_camel_case , \
7
7
ABCBuildDictMeta , StringifyEnum
8
8
9
9
SCRIPT_FILE_NAME = "function_app.py"
@@ -73,17 +73,22 @@ class Binding(ABC):
73
73
attribute in function.json when new binding classes are created.
74
74
Ref: https://aka.ms/azure-function-binding-http """
75
75
76
+ EXCLUDED_INIT_PARAMS = {'self' , 'kwargs' , 'type' , 'data_type' , 'direction' }
77
+
76
78
@staticmethod
77
79
@abstractmethod
78
80
def get_binding_name () -> str :
79
81
pass
80
82
81
83
def __init__ (self , name : str ,
82
84
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
87
92
self .name = name
88
93
self ._direction = direction
89
94
self ._data_type = data_type
@@ -111,7 +116,7 @@ def get_dict_repr(self) -> Dict:
111
116
:return: Dictionary representation of the binding.
112
117
"""
113
118
for p in getattr (self , 'init_params' , []):
114
- if p not in [ 'data_type' , 'self' ] :
119
+ if p not in Binding . EXCLUDED_INIT_PARAMS :
115
120
self ._dict [to_camel_case (p )] = getattr (self , p , None )
116
121
117
122
return self ._dict
@@ -121,24 +126,37 @@ class Trigger(Binding, ABC, metaclass=ABCBuildDictMeta):
121
126
"""Class representation of Azure Function Trigger. \n
122
127
Ref: https://aka.ms/functions-triggers-bindings-overview
123
128
"""
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 :
125
139
super ().__init__ (direction = BindingDirection .IN ,
126
- name = name , data_type = data_type , is_trigger = True )
140
+ name = name , data_type = data_type , type = type )
127
141
128
142
129
143
class InputBinding (Binding , ABC , metaclass = ABCBuildDictMeta ):
130
144
"""Class representation of Azure Function Input Binding. \n
131
145
Ref: https://aka.ms/functions-triggers-bindings-overview
132
146
"""
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 :
134
150
super ().__init__ (direction = BindingDirection .IN ,
135
- name = name , data_type = data_type , is_trigger = False )
151
+ name = name , data_type = data_type , type = type )
136
152
137
153
138
154
class OutputBinding (Binding , ABC , metaclass = ABCBuildDictMeta ):
139
155
"""Class representation of Azure Function Output Binding. \n
140
156
Ref: https://aka.ms/functions-triggers-bindings-overview
141
157
"""
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 :
143
161
super ().__init__ (direction = BindingDirection .OUT ,
144
- name = name , data_type = data_type , is_trigger = False )
162
+ name = name , data_type = data_type , type = type )
0 commit comments