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

Handle postgres arrays #6

Merged
merged 3 commits into from Jun 1, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 25 additions & 33 deletions marshmallow_sqlalchemy/convert.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import inspect
import functools

import marshmallow as ma
from marshmallow import validate, fields
Expand All @@ -21,51 +22,37 @@ def get_pk_from_identity(obj):
else: # Compund primary key
return ':'.join(text_type(x) for x in key)

def _is_field(value):
return (
isinstance(value, type) and
issubclass(value, fields.Field)
)

def _postgres_array_factory(converter, data_type):
return functools.partial(
fields.List,
converter._get_field_class_for_data_type(data_type.item_type),
)

class ModelConverter(object):
"""Class that converts a SQLAlchemy model into a dictionary of corresponding
marshmallow `Fields <marshmallow.fields.Field>`.
"""

SQLA_TYPE_MAPPING = {
sa.String: fields.String,
sa.Unicode: fields.String,
sa.Boolean: fields.Boolean,
sa.Unicode: fields.String,
sa.Binary: fields.String,
sa.Enum: fields.Field,
sa.Numeric: fields.Decimal,
sa.Float: fields.Decimal,
sa.Date: fields.Date,

postgresql.BIT: fields.Integer,
postgresql.UUID: fields.UUID,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably should've put this into its own PR, but the mappings that I deleted aren't necessary. Because mysql.TINYINT().python_type is int, the mappings in Schema.TYPE_MAPPING already handle this case. I think mappings are only needed here for columns that either don't define python_type or that need special logic, like postgres.UUID or postgres.ARRAY.

Semi-documented in commit message at jmcarp@67b2eb7.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thanks for the clarification.

postgresql.MACADDR: fields.String,
postgresql.INET: fields.String,
postgresql.JSON: fields.Raw,
postgresql.JSONB: fields.Raw,
postgresql.HSTORE: fields.Raw,
postgresql.ARRAY: _postgres_array_factory,

mysql.BIT: fields.Integer,
mysql.TINYINT: fields.Integer,
mysql.SMALLINT: fields.Integer,
mysql.INTEGER: fields.Integer,
mysql.BIGINT: fields.Integer,

mysql.NUMERIC: fields.Decimal,
mysql.DECIMAL: fields.Decimal,

mysql.DATETIME: fields.DateTime,
mysql.DATE: fields.Date,
mysql.TIME: fields.Time,
mysql.YEAR: fields.Integer,

mysql.TEXT: fields.String,
mysql.TINYTEXT: fields.String,
mysql.MEDIUMTEXT: fields.String,
mysql.LONGTEXT: fields.String,

mysql.BLOB: fields.String,
mysql.TINYBLOB: fields.String,
mysql.MEDIUMBLOB: fields.String,
mysql.LONGBLOB: fields.String,

mysql.SET: fields.List,
mysql.ENUM: fields.Field,
}
Expand Down Expand Up @@ -113,17 +100,22 @@ def field_for(self, model, property_name, **kwargs):
return self.property2field(prop, **kwargs)

def _get_field_class_for_column(self, column):
return self._get_field_class_for_data_type(column.type)

def _get_field_class_for_data_type(self, data_type):
field_cls = None
types = inspect.getmro(type(column.type))
types = inspect.getmro(type(data_type))
# First search for a field class from self.SQLA_TYPE_MAPPING
for col_type in types:
if col_type in self.SQLA_TYPE_MAPPING:
field_cls = self.SQLA_TYPE_MAPPING[col_type]
if callable(field_cls) and not _is_field(field_cls):
field_cls = field_cls(self, data_type)
break
else:
# Try to find a field class based on the column's python_type
if column.type.python_type in ma.Schema.TYPE_MAPPING:
field_cls = ma.Schema.TYPE_MAPPING[column.type.python_type]
if data_type.python_type in ma.Schema.TYPE_MAPPING:
field_cls = ma.Schema.TYPE_MAPPING[data_type.python_type]
else:
raise ModelConversionError(
'Could not find field column of type {0}.'.format(types[0]))
Expand Down
14 changes: 13 additions & 1 deletion tests/test_marshmallow_sqlalchemy.py
Expand Up @@ -265,7 +265,7 @@ def test_convert_Numeric(self, converter):
def test_convert_Float(self, converter):
prop = make_property(sa.Float(scale=2))
field = converter.property2field(prop)
assert type(field) == fields.Decimal
assert type(field) == fields.Float

def test_convert_SmallInteger(self, converter):
prop = make_property(sa.SmallInteger())
Expand All @@ -287,6 +287,18 @@ def test_convert_INET(self, converter):
field = converter.property2field(prop)
assert type(field) == fields.Str

def test_convert_ARRAY_String(self, converter):
prop = make_property(postgresql.ARRAY(sa.String()))
field = converter.property2field(prop)
assert type(field) == fields.List
assert type(field.container) == fields.Str

def test_convert_ARRAY_Integer(self, converter):
prop = make_property(postgresql.ARRAY(sa.Integer))
field = converter.property2field(prop)
assert type(field) == fields.List
assert type(field.container) == fields.Int

class TestPropToFieldClass:

def test_property2field(self):
Expand Down