Skip to content
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
4 changes: 4 additions & 0 deletions azure/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .meta import get_binding_registry
from ._queue import QueueMessage
from ._servicebus import ServiceBusMessage
from ._sql import SqlRow, SqlRowList

# Import binding implementations to register them
from . import blob # NoQA
Expand All @@ -29,6 +30,7 @@
from . import servicebus # NoQA
from . import timer # NoQA
from . import durable_functions # NoQA
from . import sql # NoQA


__all__ = (
Expand All @@ -55,6 +57,8 @@
'EntityContext',
'QueueMessage',
'ServiceBusMessage',
'SqlRow',
'SqlRowList',
'TimerRequest',

# Middlewares
Expand Down
29 changes: 29 additions & 0 deletions azure/functions/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,32 @@ class OrchestrationContext(abc.ABC):
@abc.abstractmethod
def body(self) -> str:
pass


class SqlRow(abc.ABC):

@classmethod
@abc.abstractmethod
def from_json(cls, json_data: str) -> 'SqlRow':
pass

@classmethod
@abc.abstractmethod
def from_dict(cls, dct: dict) -> 'SqlRow':
pass

@abc.abstractmethod
def __getitem__(self, key):
pass

@abc.abstractmethod
def __setitem__(self, key, value):
pass

@abc.abstractmethod
def to_json(self) -> str:
pass


class SqlRowList(abc.ABC):
pass
44 changes: 44 additions & 0 deletions azure/functions/_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import collections
import json

from . import _abc


class SqlRow(_abc.SqlRow, collections.UserDict):
"""A SQL Row.

SqlRow objects are ''UserDict'' subclasses and behave like dicts.
"""

@classmethod
def from_json(cls, json_data: str) -> 'SqlRow':
"""Create a SqlRow from a JSON string."""
return cls.from_dict(json.loads(json_data))

@classmethod
def from_dict(cls, dct: dict) -> 'SqlRow':
"""Create a SqlRow from a dict object"""
return cls({k: v for k, v in dct.items()})

def to_json(self) -> str:
"""Return the JSON representation of the SqlRow"""
return json.dumps(dict(self))

def __getitem__(self, key):
return collections.UserDict.__getitem__(self, key)

def __setitem__(self, key, value):
return collections.UserDict.__setitem__(self, key, value)

def __repr__(self) -> str:
return (
f'<SqlRow at 0x{id(self):0x}>'
)


class SqlRowList(_abc.SqlRowList, collections.UserList):
"A ''UserList'' subclass containing a list of :class:'~SqlRow' objects"
pass
78 changes: 78 additions & 0 deletions azure/functions/sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import collections.abc
import json
import typing

from azure.functions import _sql as sql

from . import meta


class SqlConverter(meta.InConverter, meta.OutConverter,
binding='sql'):

@classmethod
def check_input_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, sql.SqlRowList)

@classmethod
def check_output_type_annotation(cls, pytype: type) -> bool:
return issubclass(pytype, (sql.SqlRowList, sql.SqlRow))

@classmethod
def decode(cls,
data: meta.Datum,
*,
trigger_metadata) -> typing.Optional[sql.SqlRowList]:
if data is None or data.type is None:
return None

data_type = data.type

if data_type in ['string', 'json']:
body = data.value

elif data_type == 'bytes':
body = data.value.decode('utf-8')

else:
raise NotImplementedError(
f'Unsupported payload type: {data_type}')

rows = json.loads(body)
if not isinstance(rows, list):
rows = [rows]

return sql.SqlRowList(
(None if row is None else sql.SqlRow.from_dict(row))
for row in rows)

@classmethod
def encode(cls, obj: typing.Any, *,
expected_type: typing.Optional[type]) -> meta.Datum:
if isinstance(obj, sql.SqlRow):
data = sql.SqlRowList([obj])

elif isinstance(obj, sql.SqlRowList):
data = obj

elif isinstance(obj, collections.abc.Iterable):
data = sql.SqlRowList()

for row in obj:
if not isinstance(row, sql.SqlRow):
raise NotImplementedError(
f'Unsupported list type: {type(obj)}, \
lists must contain SqlRow objects')
else:
data.append(row)

else:
raise NotImplementedError(f'Unsupported type: {type(obj)}')

return meta.Datum(
type='json',
value=json.dumps([dict(d) for d in data])
)
Loading