revert pull 73, add serialize-time drop feature

This commit is contained in:
Chris McDonough
2013-08-08 17:59:27 -04:00
parent 73086c8d37
commit 513d860043
3 changed files with 43 additions and 27 deletions

View File

@@ -4,6 +4,15 @@ Unreleased
Bug Fixes
~~~~~~~~~
- In 1.0a1, there was a change merged from
https://github.com/Pylons/colander/pull/73 which made it possible to supply
``None`` as the ``default`` value for a String type, and upon serialization,
the value would be rendered as ``colander.null`` if the default were used.
This confused people who were actually supplying the value ``None`` as a
default when the associated appstruct had no value, so the change has been
reverted. When you supply ``None`` as the ``default`` argument to a String,
the rendered serialize() value will again be ``'None'``. Sorry.
- Normalize ``message`` argument to ``colander.Function`` -> ``msg`` (matches
other APIs.
@@ -17,6 +26,20 @@ Features
- Add ``colander.List`` type, modeled on ``deform.List``: this type
preserves ordering, and allows duplicates.
- It is now possible to use the value ``colander.drop`` as the ``default``
value for items that are subitems of a mapping. If ``colander.drop`` is used
as the ``default`` for a subnode of a mapping schema, and the mapping
appstruct being serialized does not have a value for that schema node, the
value will be omitted from the serialized mapping. For instance, the
following script, when run would not raise an assertion error::
class What(colander.MappingSchema):
thing = colander.SchemaNode(colander.String(), default=colander.drop,
missing=None)
result = What().serialize({}) # no "thing" in mapping
assert result == {}
1.0a5 (2013-05-31)
------------------

View File

@@ -545,15 +545,16 @@ class Mapping(SchemaType):
for num, subnode in enumerate(node.children):
name = subnode.name
subval = value.pop(name, null)
try:
sub_result = callback(subnode, subval)
except Invalid as e:
if error is None:
error = Invalid(node)
error.add(e, num)
else:
if sub_result is not drop:
result[name] = sub_result
if subval is not drop:
try:
sub_result = callback(subnode, subval)
except Invalid as e:
if error is None:
error = Invalid(node)
error.add(e, num)
else:
if sub_result is not drop:
result[name] = sub_result
if self.unknown == 'raise':
if value:
@@ -1090,7 +1091,7 @@ class String(SchemaType):
self.encoding = encoding
def serialize(self, node, appstruct):
if appstruct in (null, None):
if appstruct is null:
return null
try:
@@ -1136,7 +1137,7 @@ class Number(SchemaType):
num = None
def serialize(self, node, appstruct):
if appstruct in (null, None):
if appstruct is null:
return null
try:

View File

@@ -653,6 +653,14 @@ class TestMapping(unittest.TestCase):
result = typ.serialize(node, null)
self.assertEqual(result, {'a':null})
def test_serialize_value_has_drop(self):
from colander import drop
node = DummySchemaNode(None)
node.children = [DummySchemaNode(None, name='a')]
typ = self._makeOne()
result = typ.serialize(node, {'a':drop})
self.assertEqual(result, {})
def test_flatten(self):
node = DummySchemaNode(None, name='node')
int1 = DummyType()
@@ -1350,14 +1358,6 @@ class TestString(unittest.TestCase):
result = typ.serialize(node, null)
self.assertEqual(result, null)
def test_serialize_none(self):
import colander
val = None
node = DummySchemaNode(None)
typ = self._makeOne()
result = typ.serialize(node, val)
self.assertEqual(result, colander.null)
def test_serialize_emptystring(self):
val = ''
node = DummySchemaNode(None)
@@ -1427,14 +1427,6 @@ class TestInteger(unittest.TestCase):
result = typ.serialize(node, val)
self.assertEqual(result, colander.null)
def test_serialize_none(self):
import colander
val = None
node = DummySchemaNode(None)
typ = self._makeOne()
result = typ.serialize(node, val)
self.assertEqual(result, colander.null)
def test_serialize_emptystring(self):
import colander
val = ''