From 3a0eaacc9d9efb3702685e3553b4209c3dd6d22f Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Thu, 26 Jun 2014 00:53:16 -0700 Subject: [PATCH] Fix `to_dict` not converting enums to strings Fixes #28 --- capnp/lib/capnp.pyx | 3 +++ test/test_struct.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 6ec166d..6071c02 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -735,6 +735,9 @@ cdef _to_dict(msg, bint verbose): return ret + if msg_type is _DynamicEnum: + return str(msg) + return msg diff --git a/test/test_struct.py b/test/test_struct.py index ce71f40..c292c6b 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -171,3 +171,11 @@ def test_set_dict_union(addressbook): person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}}) assert person.employment.employer.name == 'foo' + + +def test_to_dict_enum(addressbook): + person = addressbook.Person.new_message(**{'phones': [{'number': '999-9999', 'type': 'mobile'}]}) + + field = person.to_dict()['phones'][0]['type'] + assert isinstance(field, basestring) + assert field == 'mobile'