Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic binding decorators/Modify existing decorators to enable decorator extensibility under new programming model #108

Merged
merged 3 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions azure/functions/decorators/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def __init__(self,
name: str,
path: str,
connection: str,
data_type: Optional[DataType] = None):
data_type: Optional[DataType] = None,
**kwargs):
self.path = path
self.connection = connection
super().__init__(name=name, data_type=data_type)
Expand All @@ -27,7 +28,8 @@ def __init__(self,
name: str,
path: str,
connection: str,
data_type: Optional[DataType] = None):
data_type: Optional[DataType] = None,
**kwargs):
self.path = path
self.connection = connection
super().__init__(name=name, data_type=data_type)
Expand All @@ -42,7 +44,8 @@ def __init__(self,
name: str,
path: str,
connection: str,
data_type: Optional[DataType] = None):
data_type: Optional[DataType] = None,
**kwargs):
self.path = path
self.connection = connection
super().__init__(name=name, data_type=data_type)
Expand Down
44 changes: 31 additions & 13 deletions azure/functions/decorators/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from abc import ABC, abstractmethod
from typing import Dict, Optional
from typing import Dict, Optional, Type

from azure.functions.decorators.utils import to_camel_case, \
from .utils import to_camel_case, \
ABCBuildDictMeta, StringifyEnum

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

EXCLUDED_INIT_PARAMS = {'self', 'kwargs', 'type', 'data_type', 'direction'}

@staticmethod
@abstractmethod
def get_binding_name() -> str:
pass

def __init__(self, name: str,
direction: BindingDirection,
is_trigger: bool,
data_type: Optional[DataType] = None):
self.type = self.get_binding_name()
self.is_trigger = is_trigger
vrdmr marked this conversation as resolved.
Show resolved Hide resolved
data_type: Optional[DataType] = None,
type: Optional[str] = None): # NoQa
# For natively supported bindings, get_binding_name is always
# implemented, and for generic bindings, type is a required argument
# in decorator functions.
self.type = self.get_binding_name() \
if self.get_binding_name() is not None else type
YunchuWang marked this conversation as resolved.
Show resolved Hide resolved
self.name = name
self._direction = direction
self._data_type = data_type
Expand Down Expand Up @@ -111,7 +116,7 @@ def get_dict_repr(self) -> Dict:
:return: Dictionary representation of the binding.
"""
for p in getattr(self, 'init_params', []):
if p not in ['data_type', 'self']:
if p not in Binding.EXCLUDED_INIT_PARAMS:
self._dict[to_camel_case(p)] = getattr(self, p, None)

return self._dict
Expand All @@ -121,24 +126,37 @@ class Trigger(Binding, ABC, metaclass=ABCBuildDictMeta):
"""Class representation of Azure Function Trigger. \n
Ref: https://aka.ms/functions-triggers-bindings-overview
"""
def __init__(self, name, data_type) -> None:

@staticmethod
def is_supported_trigger_type(trigger_instance: 'Trigger',
trigger_type: Type['Trigger']):
return isinstance(trigger_instance,
trigger_type) or trigger_instance.type == \
trigger_type.get_binding_name()

def __init__(self, name: str, data_type: Optional[DataType] = None,
type: Optional[str] = None) -> None:
super().__init__(direction=BindingDirection.IN,
name=name, data_type=data_type, is_trigger=True)
name=name, data_type=data_type, type=type)
YunchuWang marked this conversation as resolved.
Show resolved Hide resolved


class InputBinding(Binding, ABC, metaclass=ABCBuildDictMeta):
"""Class representation of Azure Function Input Binding. \n
Ref: https://aka.ms/functions-triggers-bindings-overview
"""
def __init__(self, name, data_type) -> None:

def __init__(self, name: str, data_type: Optional[DataType] = None,
type: Optional[str] = None) -> None:
super().__init__(direction=BindingDirection.IN,
name=name, data_type=data_type, is_trigger=False)
name=name, data_type=data_type, type=type)


class OutputBinding(Binding, ABC, metaclass=ABCBuildDictMeta):
"""Class representation of Azure Function Output Binding. \n
Ref: https://aka.ms/functions-triggers-bindings-overview
"""
def __init__(self, name, data_type) -> None:

def __init__(self, name: str, data_type: Optional[DataType] = None,
type: Optional[str] = None) -> None:
super().__init__(direction=BindingDirection.OUT,
name=name, data_type=data_type, is_trigger=False)
name=name, data_type=data_type, type=type)
8 changes: 5 additions & 3 deletions azure/functions/decorators/cosmosdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self,
data_type: Optional[DataType] = None,
id: Optional[str] = None,
sql_query: Optional[str] = None,
partition_key: Optional[str] = None):
partition_key: Optional[str] = None,
**kwargs):
self.database_name = database_name
self.collection_name = collection_name
self.connection_string_setting = connection_string_setting
Expand All @@ -45,7 +46,8 @@ def __init__(self,
use_multiple_write_locations: Optional[bool] = None,
preferred_locations: Optional[str] = None,
partition_key: Optional[str] = None,
data_type: Optional[DataType] = None):
data_type: Optional[DataType] = None,
**kwargs):
self.database_name = database_name
self.collection_name = collection_name
self.connection_string_setting = connection_string_setting
Expand Down Expand Up @@ -83,7 +85,7 @@ def __init__(self,
lease_connection_string_setting: Optional[str] = None,
lease_database_name: Optional[str] = None,
lease_collection_prefix: Optional[str] = None,
):
**kwargs):
self.lease_collection_name = lease_collection_name
self.lease_connection_string_setting = lease_connection_string_setting
self.lease_database_name = lease_database_name
Expand Down
6 changes: 4 additions & 2 deletions azure/functions/decorators/eventhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self,
event_hub_name: str,
data_type: Optional[DataType] = None,
cardinality: Optional[Cardinality] = None,
consumer_group: Optional[str] = None):
consumer_group: Optional[str] = None,
**kwargs):
self.connection = connection
self.event_hub_name = event_hub_name
self.cardinality = cardinality
Expand All @@ -37,7 +38,8 @@ def __init__(self,
name: str,
connection: str,
event_hub_name: str,
data_type: Optional[DataType] = None):
data_type: Optional[DataType] = None,
**kwargs):
self.connection = connection
self.event_hub_name = event_hub_name
super().__init__(name=name, data_type=data_type)
Loading