Fix problem with uninitialized unions in _from_dict

This commit is contained in:
Jason Paryani
2014-02-18 17:34:34 -08:00
parent 110b454e2c
commit 307fcbf4e7
3 changed files with 18 additions and 2 deletions

View File

@@ -741,7 +741,12 @@ cdef _to_dict(msg, bint verbose):
cdef _from_dict(_DynamicStructBuilder msg, dict d): cdef _from_dict(_DynamicStructBuilder msg, dict d):
for key, val in d.iteritems(): for key, val in d.iteritems():
if key != 'which': if key != 'which':
msg._set(key, val) try:
msg._set(key, val)
except Exception as e:
if 'expected isSetInUnion(field)' in str(e):
msg.init(key)
msg._set(key, val)
cdef _from_list(_DynamicListBuilder msg, list d): cdef _from_list(_DynamicListBuilder msg, list d):
cdef size_t count = 0 cdef size_t count = 0

View File

@@ -21,13 +21,18 @@ struct Person {
employment :union { employment :union {
unemployed @4 :Void; unemployed @4 :Void;
employer @5 :Text; employer @5 :Employer;
school @6 :Text; school @6 :Text;
selfEmployed @7 :Void; selfEmployed @7 :Void;
# We assume that a person is only one of these. # We assume that a person is only one of these.
} }
} }
struct Employer {
name @0 :Text;
boss @1 :Person;
}
struct AddressBook { struct AddressBook {
people @0 :List(Person); people @0 :List(Person);
} }

View File

@@ -164,3 +164,9 @@ def test_set_dict(all_types):
msg.structList[0] = {'int32Field': 102} msg.structList[0] = {'int32Field': 102}
assert msg.structList[0].int32Field == 102 assert msg.structList[0].int32Field == 102
def test_set_dict_union(addressbook):
person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}})
assert person.employment.employer.name == 'foo'