flatten of mapping produces dot-prefixed keys #37

Closed
mcdonc opened this Issue Feb 16, 2012 · 3 comments

Projects

None yet

2 participants

Owner
mcdonc commented Feb 16, 2012
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

Member
kiorky commented Feb 16, 2012

May this introduce regressions in code from the wild ?

Member
kiorky commented Feb 16, 2012

Appart from that, the fix is just fine for me

Owner
mcdonc commented Feb 18, 2012

Merged to trunk, don't think this was ever intended.

@mcdonc mcdonc closed this Feb 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment