Add as_interface/as_list methods to _DynamicObject*
This commit is contained in:
@@ -336,30 +336,11 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
||||
Maybe[StructSchema.Field] which()
|
||||
RemotePromise send()
|
||||
|
||||
cdef extern from "capnp/any.h" namespace " ::capnp":
|
||||
cdef cppclass AnyPointer:
|
||||
cppclass Reader:
|
||||
DynamicStruct.Reader getAs"getAs< ::capnp::DynamicStruct>"(StructSchema)
|
||||
StringPtr getAsText"getAs< ::capnp::Text>"()
|
||||
cppclass Builder:
|
||||
Builder(Builder)
|
||||
DynamicStruct_Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema)
|
||||
StringPtr getAsText"getAs< ::capnp::Text>"()
|
||||
void setAsStruct"setAs< ::capnp::DynamicStruct>"(DynamicStruct.Reader&) except +reraise_kj_exception
|
||||
void setAsText"setAs< ::capnp::Text>"(char*) except +reraise_kj_exception
|
||||
|
||||
cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
||||
cdef cppclass DynamicEnum:
|
||||
uint16_t getRaw()
|
||||
Maybe[EnumSchema.Enumerant] getEnumerant()
|
||||
|
||||
cdef cppclass DynamicObject:
|
||||
cppclass Reader:
|
||||
DynamicStruct.Reader as(StructSchema schema)
|
||||
cppclass Builder:
|
||||
DynamicObject.Reader asReader()
|
||||
# DynamicList::Reader as(ListSchema schema) const;
|
||||
|
||||
cdef cppclass DynamicList:
|
||||
cppclass Reader:
|
||||
DynamicValueForward.Reader operator[](uint) except +reraise_kj_exception
|
||||
@@ -375,6 +356,28 @@ cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
||||
DynamicOrphan disown(uint)
|
||||
StructSchema getStructElementType'getSchema().getStructElementType'()
|
||||
|
||||
cdef extern from "capnp/any.h" namespace " ::capnp":
|
||||
cdef cppclass AnyPointer:
|
||||
cppclass Reader:
|
||||
DynamicStruct.Reader getAs"getAs< ::capnp::DynamicStruct>"(StructSchema) except +reraise_kj_exception
|
||||
DynamicCapability.Client getAsCapability"getAs< ::capnp::DynamicCapability>"(InterfaceSchema) except +reraise_kj_exception
|
||||
DynamicList.Reader getAsList"getAs< ::capnp::DynamicList>"(ListSchema) except +reraise_kj_exception
|
||||
StringPtr getAsText"getAs< ::capnp::Text>"() except +reraise_kj_exception
|
||||
cppclass Builder:
|
||||
Builder(Builder)
|
||||
DynamicStruct_Builder getAs"getAs< ::capnp::DynamicStruct>"(StructSchema) except +reraise_kj_exception
|
||||
DynamicCapability.Client getAsCapability"getAs< ::capnp::DynamicCapability>"(InterfaceSchema) except +reraise_kj_exception
|
||||
DynamicList.Builder getAsList"getAs< ::capnp::DynamicList>"(ListSchema) except +reraise_kj_exception
|
||||
StringPtr getAsText"getAs< ::capnp::Text>"() except +reraise_kj_exception
|
||||
void setAsStruct"setAs< ::capnp::DynamicStruct>"(DynamicStruct.Reader&) except +reraise_kj_exception
|
||||
void setAsText"setAs< ::capnp::Text>"(char*) except +reraise_kj_exception
|
||||
AnyPointer.Reader asReader() except +reraise_kj_exception
|
||||
void set(AnyPointer.Reader) except +reraise_kj_exception
|
||||
DynamicStruct_Builder initAsStruct"initAs< ::capnp::DynamicStruct>"(StructSchema) except +reraise_kj_exception
|
||||
DynamicList.Builder initAsList"initAs< ::capnp::DynamicList>"(ListSchema, uint) except +reraise_kj_exception
|
||||
|
||||
|
||||
cdef extern from "capnp/dynamic.h" namespace " ::capnp":
|
||||
cdef cppclass DynamicValue:
|
||||
cppclass Reader:
|
||||
Reader()
|
||||
|
||||
@@ -1491,6 +1491,24 @@ cdef class _DynamicObjectReader:
|
||||
|
||||
return _DynamicStructReader()._init(self.thisptr.getAs(s.thisptr), self._parent)
|
||||
|
||||
cpdef as_interface(self, schema) except +reraise_kj_exception:
|
||||
cdef _InterfaceSchema s
|
||||
if hasattr(schema, 'schema'):
|
||||
s = schema.schema
|
||||
else:
|
||||
s = schema
|
||||
|
||||
return _DynamicCapabilityClient()._init(self.thisptr.getAsCapability(s.thisptr), self._parent)
|
||||
|
||||
cpdef as_list(self, schema) except +reraise_kj_exception:
|
||||
cdef ListSchema s
|
||||
if hasattr(schema, 'schema'):
|
||||
s = schema.schema
|
||||
else:
|
||||
s = schema
|
||||
|
||||
return _DynamicListReader()._init(self.thisptr.getAsList(s.thisptr), self._parent)
|
||||
|
||||
cpdef as_text(self) except +reraise_kj_exception:
|
||||
return (<char*>self.thisptr.getAsText().cStr())[:]
|
||||
|
||||
@@ -1515,12 +1533,47 @@ cdef class _DynamicObjectBuilder:
|
||||
|
||||
return _DynamicStructBuilder()._init(self.thisptr.getAs(s.thisptr), self._parent)
|
||||
|
||||
cpdef as_interface(self, schema) except +reraise_kj_exception:
|
||||
cdef _InterfaceSchema s
|
||||
if hasattr(schema, 'schema'):
|
||||
s = schema.schema
|
||||
else:
|
||||
s = schema
|
||||
|
||||
return _DynamicCapabilityClient()._init(self.thisptr.getAsCapability(s.thisptr), self._parent)
|
||||
|
||||
cpdef as_list(self, schema) except +reraise_kj_exception:
|
||||
cdef ListSchema s
|
||||
if hasattr(schema, 'schema'):
|
||||
s = schema.schema
|
||||
else:
|
||||
s = schema
|
||||
|
||||
return _DynamicListBuilder()._init(self.thisptr.getAsList(s.thisptr), self._parent)
|
||||
|
||||
cpdef set(self, other):
|
||||
"Set value of this object with the value of another AnyPointer::Reader. Don't use this for structs"
|
||||
cdef _DynamicObjectReader reader = other
|
||||
self.thisptr.set(reader.thisptr)
|
||||
|
||||
cpdef set_as_text(self, text):
|
||||
self.thisptr.setAsText(text)
|
||||
|
||||
cpdef init_as_list(self, schema, size):
|
||||
cdef ListSchema s
|
||||
if hasattr(schema, 'schema'):
|
||||
s = schema.schema
|
||||
else:
|
||||
s = schema
|
||||
|
||||
return _DynamicListBuilder()._init(self.thisptr.initAsList(s.thisptr, size), self._parent)
|
||||
|
||||
cpdef as_text(self) except +reraise_kj_exception:
|
||||
return (<char*>self.thisptr.getAsText().cStr())[:]
|
||||
|
||||
cpdef as_reader(self):
|
||||
return _DynamicObjectReader()._init(self.thisptr.asReader(), self._parent)
|
||||
|
||||
cdef class _EventLoop:
|
||||
cdef capnp.AsyncIoContext * thisptr
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
@0x8186ddb142b58556;
|
||||
|
||||
struct Person {
|
||||
id @0 :UInt32;
|
||||
name @1 :Text;
|
||||
}
|
||||
|
||||
struct Place {
|
||||
id @0 :UInt32;
|
||||
name @1 :Text;
|
||||
}
|
||||
|
||||
struct Thing {
|
||||
id @0 :UInt64;
|
||||
value @1 :UInt64;
|
||||
}
|
||||
|
||||
struct TestObject {
|
||||
object @0 :AnyPointer;
|
||||
}
|
||||
|
||||
@@ -4,21 +4,48 @@ import os
|
||||
|
||||
this_dir = os.path.dirname(__file__)
|
||||
|
||||
@pytest.fixture
|
||||
def object():
|
||||
return capnp.load(os.path.join(this_dir, 'object.capnp'))
|
||||
|
||||
def test_object_basic(object):
|
||||
obj = object.TestObject.new_message()
|
||||
person = obj.object.as_struct(object.Person)
|
||||
@pytest.fixture
|
||||
def addressbook():
|
||||
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
||||
|
||||
|
||||
def test_object_basic(addressbook):
|
||||
obj = capnp._MallocMessageBuilder().get_root_as_any()
|
||||
person = obj.as_struct(addressbook.Person)
|
||||
person.name = 'test'
|
||||
person.id = 1000
|
||||
|
||||
same_person = obj.object.as_struct(object.Person)
|
||||
same_person = obj.as_struct(addressbook.Person)
|
||||
assert same_person.name == 'test'
|
||||
assert same_person.id == 1000
|
||||
|
||||
obj_r = obj.as_reader()
|
||||
same_person = obj_r.object.as_struct(object.Person)
|
||||
same_person = obj_r.as_struct(addressbook.Person)
|
||||
assert same_person.name == 'test'
|
||||
assert same_person.id == 1000
|
||||
|
||||
|
||||
def test_object_list(addressbook):
|
||||
obj = capnp._MallocMessageBuilder().get_root_as_any()
|
||||
listSchema = capnp.ListSchema(addressbook.Person)
|
||||
people = obj.init_as_list(listSchema, 2)
|
||||
person = people[0]
|
||||
person.name = 'test'
|
||||
person.id = 1000
|
||||
person = people[1]
|
||||
person.name = 'test2'
|
||||
person.id = 1001
|
||||
|
||||
same_person = obj.as_list(listSchema)
|
||||
assert same_person[0].name == 'test'
|
||||
assert same_person[0].id == 1000
|
||||
assert same_person[1].name == 'test2'
|
||||
assert same_person[1].id == 1001
|
||||
|
||||
obj_r = obj.as_reader()
|
||||
same_person = obj_r.as_list(listSchema)
|
||||
assert same_person[0].name == 'test'
|
||||
assert same_person[0].id == 1000
|
||||
assert same_person[1].name == 'test2'
|
||||
assert same_person[1].id == 1001
|
||||
|
||||
Reference in New Issue
Block a user