Pylons/colander — Issue #37: flatten of mapping produces dot-prefixed keys
Author: mcdonc
Created: 2012-02-16 02:42:52+00:00
State: open

```python

import colander
import pprint

class Friend(colander.TupleSchema):
rank = colander.SchemaNode(colander.Int(),
validator=colander.Range(0, 9999))
name = colander.SchemaNode(colander.String())

class Phone(colander.MappingSchema):
location = colander.SchemaNode(colander.String(),
validator=colander.OneOf(['home', 'work']))
number = colander.SchemaNode(colander.String())

class Friends(colander.SequenceSchema):
friend = Friend()

class Phones(colander.SequenceSchema):
phone = Phone()

class Person(colander.MappingSchema):
name = colander.SchemaNode(colander.String())
age = colander.SchemaNode(colander.Int(),
validator=colander.Range(0, 200))
friends = Friends()
phones = Phones()

schema = Person()
appstruct = {
'name':'keith',
'age':20,
'friends':[(1, 'jim'),(2, 'bob'), (3, 'joe'), (4, 'fred')],
'phones':[{'location':'home', 'number':'555-1212'},
{'location':'work', 'number':'555-8989'},],
}

pprint.pprint(schema.flatten(appstruct))

# produces:

# {'.age': 20,
# '.friends.0.name': 'jim',
# '.friends.0.rank': 1,
# '.friends.1.name': 'bob',
# '.friends.1.rank': 2,
# '.friends.2.name': 'joe',
# '.friends.2.rank': 3,
# '.friends.3.name': 'fred',
# '.friends.3.rank': 4,
# '.name': 'keith',
# '.phones.0.location': 'home',
# '.phones.0.number': '555-1212',
# '.phones.1.location': 'work',
# '.phones.1.number': '555-8989'}

# should produce?:

# {'age': 20,
# 'friends.0.name': 'jim',
# 'friends.0.rank': 1,
# 'friends.1.name': 'bob',
# 'friends.1.rank': 2,
# 'friends.2.name': 'joe',
# 'friends.2.rank': 3,
# 'friends.3.name': 'fred',
# 'friends.3.rank': 4,
# 'name': 'keith',
# 'phones.0.location': 'home',
# 'phones.0.number': '555-1212',
# 'phones.1.location': 'work',
# 'phones.1.number': '555-8989'}
```

I think this is the right fix, but it'd be nice to get some confirmation from @chrisrossi:

https://github.com/Pylons/colander/compare/fix.rootflatten