Add from_dict
This commit is contained in:
@@ -422,10 +422,10 @@ cdef _setDynamicField(_DynamicSetterClasses thisptr, field, value, parent):
|
|||||||
|
|
||||||
cdef _to_dict(msg):
|
cdef _to_dict(msg):
|
||||||
msg_type = type(msg)
|
msg_type = type(msg)
|
||||||
if msg_type is _DynamicListBuilder:
|
if msg_type is _DynamicListBuilder or msg_type is _DynamicListReader or msg_type is _DynamicResizableListBuilder:
|
||||||
return [_to_dict(x) for x in msg]
|
return [_to_dict(x) for x in msg]
|
||||||
|
|
||||||
if msg_type is _DynamicStructBuilder:
|
if msg_type is _DynamicStructBuilder or msg_type is _DynamicStructReader:
|
||||||
ret = {}
|
ret = {}
|
||||||
try:
|
try:
|
||||||
which = msg.which()
|
which = msg.which()
|
||||||
@@ -441,6 +441,31 @@ cdef _to_dict(msg):
|
|||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
import collections as _collections
|
||||||
|
cdef _from_dict_helper(msg, field, d):
|
||||||
|
if isinstance(d, dict):
|
||||||
|
sub_msg = getattr(msg, field)
|
||||||
|
for key, val in d.iteritems():
|
||||||
|
if key != 'which':
|
||||||
|
_from_dict_helper(sub_msg, key, val)
|
||||||
|
elif isinstance(d, _collections.Iterable) and not isinstance(d, basestring):
|
||||||
|
l = msg.init(field, len(d))
|
||||||
|
for i in range(len(d)):
|
||||||
|
if isinstance(d[i], dict):
|
||||||
|
for key, val in d[i].iteritems():
|
||||||
|
if key != 'which':
|
||||||
|
_from_dict_helper(l[i], key, val)
|
||||||
|
else:
|
||||||
|
l[i] = d[i]
|
||||||
|
else:
|
||||||
|
setattr(msg, field, d)
|
||||||
|
|
||||||
|
cdef _from_dict(msg, d):
|
||||||
|
for key, val in d.iteritems():
|
||||||
|
if key != 'which':
|
||||||
|
_from_dict_helper(msg, key, val)
|
||||||
|
|
||||||
|
|
||||||
cdef class _DynamicStructReader:
|
cdef class _DynamicStructReader:
|
||||||
"""Reads Cap'n Proto structs
|
"""Reads Cap'n Proto structs
|
||||||
|
|
||||||
@@ -894,9 +919,18 @@ cdef class SchemaParser:
|
|||||||
builder = _MallocMessageBuilder()
|
builder = _MallocMessageBuilder()
|
||||||
return builder.init_root(local_module)
|
return builder.init_root(local_module)
|
||||||
return helper
|
return helper
|
||||||
|
def from_dict(local_module):
|
||||||
|
def helper(d):
|
||||||
|
builder = _MallocMessageBuilder()
|
||||||
|
msg = builder.init_root(local_module)
|
||||||
|
_from_dict(msg, d)
|
||||||
|
return msg
|
||||||
|
return helper
|
||||||
|
|
||||||
local_module.read = read(local_module)
|
local_module.read = read(local_module)
|
||||||
local_module.read_packed = read_packed(local_module)
|
local_module.read_packed = read_packed(local_module)
|
||||||
local_module.new_message = new_message(local_module)
|
local_module.new_message = new_message(local_module)
|
||||||
|
local_module.from_dict = from_dict(local_module)
|
||||||
elif proto.isConst:
|
elif proto.isConst:
|
||||||
module.__dict__[node.name] = schema.as_const_value()
|
module.__dict__[node.name] = schema.as_const_value()
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ def test_which_builder(addressbook):
|
|||||||
|
|
||||||
assert bob.employment.which() == "unemployed"
|
assert bob.employment.which() == "unemployed"
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
addresses.which()
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
addresses.which()
|
||||||
|
|
||||||
def test_which_reader(addressbook):
|
def test_which_reader(addressbook):
|
||||||
def writeAddressBook(fd):
|
def writeAddressBook(fd):
|
||||||
message = capnp._MallocMessageBuilder()
|
message = capnp._MallocMessageBuilder()
|
||||||
@@ -53,6 +58,11 @@ def test_which_reader(addressbook):
|
|||||||
bob = people[1]
|
bob = people[1]
|
||||||
assert bob.employment.which() == "unemployed"
|
assert bob.employment.which() == "unemployed"
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
addresses.which()
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
addresses.which()
|
||||||
|
|
||||||
def test_builder_set(addressbook):
|
def test_builder_set(addressbook):
|
||||||
person = addressbook.Person.new_message()
|
person = addressbook.Person.new_message()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user