-
Notifications
You must be signed in to change notification settings - Fork 34
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
feat: support list in non-nested mutable json #51
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from sqlalchemy.ext.mutable import Mutable, MutableDict | ||
from sqlalchemy.ext.mutable import Mutable, MutableDict, MutableList | ||
from sqlalchemy.types import JSON | ||
|
||
from .track import TrackedDict, TrackedList | ||
|
@@ -51,13 +51,28 @@ def coerce(cls, key, value): | |
return super(cls).coerce(key, value) | ||
|
||
|
||
class MutableListOrDict(Mutable): | ||
iloveitaly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""SQLAlchemy `mutable` extension with change tracking for a single-depth list or dict.""" | ||
|
||
@classmethod | ||
def coerce(cls, key, value): | ||
if value is None: | ||
return value | ||
if isinstance(value, cls): | ||
return value | ||
if isinstance(value, dict): | ||
return MutableDict.coerce(key, value) | ||
if isinstance(value, list): | ||
return MutableList.coerce(key, value) | ||
return super(cls).coerce(key, value) | ||
|
||
def mutable_json_type(dbtype=JSON, nested=False): | ||
"""Type creator for (optionally nested) mutable JSON column types. | ||
|
||
The default backend data type is sqlalchemy.types.JSON, but can be set to | ||
any other type by providing the `dbtype` parameter. | ||
""" | ||
mutable_type = NestedMutable if nested else MutableDict | ||
mutable_type = NestedMutable if nested else MutableListOrDict | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure on the name here, but that might be a bit of a wider concern, naming this Suggestions are welcome if you have any. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I couldn't come up with a better name either :/ |
||
return mutable_type.as_mutable(dbtype) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should maintain 2 empty lines between definitions (and I guess I should really include
black
orflake8
with a plugin in the CI runs..)