Skip to content

Commit

Permalink
Rename toPlain to toBsonEncodable
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel Kramer committed Oct 29, 2019
1 parent 8a2b7da commit 2882566
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
11 changes: 5 additions & 6 deletions jsonmodels/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# it is a completely valid default value.
NotSet = object()

# BSON compatible types, which can be returned by toPlain.
# BSON compatible types, which can be returned by toBsonEncodable.
BsonEncodable = Union[
float, str, object, Dict, List, bytes, bool, datetime.datetime, None,
Pattern, int, bytes
Expand Down Expand Up @@ -131,13 +131,13 @@ def _get_embed_type(value, models):
return matching_models[0]
return models[0]

def toPlain(self, value: types) -> BsonEncodable:
"""Optionally return a python object instead of JSON compatible value.
def toBsonEncodable(self, value: types) -> BsonEncodable:
"""Optionally return a bson encodable python object.
Returned object should be BSON compatible. By default uses the
`to_struct` method, which creates JSON compatible types. JSON is
compatible with bson. When required, this method should cast the value
to supported bson type.
compatible with bson, but only has support for limited types. When
required, this method should cast the value to supported bson type.
See: https://api.mongodb.com/python/current/api/bson/index.html
For example: when a value is a datetime object return it as a datetime
Expand All @@ -146,7 +146,6 @@ def toPlain(self, value: types) -> BsonEncodable:
:param value: Value
:return: a value which should be bson encodable
"""
return self.to_struct(value=value)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


@mock.patch('jsonmodels.fields.BaseField.to_struct')
def test_toPlain_calls_to_struct(f):
"""Test if default implementation of toPlain calls to_struct."""
def test_toBsonEncodable_calls_to_struct(f):
"""Test if default implementation of toBsonEncodable calls to_struct."""
field = fields.StringField()
field.toPlain(value="text")
field.toBsonEncodable(value="text")
f.assert_called_once()

0 comments on commit 2882566

Please sign in to comment.