Schema loading from the wire

Cap'n Proto provides a schema loader, which can be used to dynamically
load schemas during runtime. To port this functionality to pycapnp,
a new class is provided `C_SchemaLoader`, which exposes the Cap'n
Proto C++ interface, and `SchemaLoader`, which is part of the pycapnp
library.

The specific use case for this is when a capnp message contains
a Node.Reader: The schema for a yet unseen message can be loaded
dynamically, allowing the future message to be properly processed.

If the message is a struct containing other structs, all the schemas for
every struct must be loaded to correctly parse the message. See
https://github.com/DaneSlattery/capnp_generic_poc for a
proof-of-concept.

Add docs and cleanup

Add more docs

Reduce changes

Fix flake8 formatting

Fix get datatype
This commit is contained in:
Rowan Reeve
2023-02-21 08:31:12 +02:00
committed by DaneSlattery
parent b439993b1f
commit a5c29a74d2
9 changed files with 106 additions and 4 deletions

View File

@@ -5,3 +5,16 @@ struct Foo {
name @1 :Text;
}
struct Baz{
text @0 :Text;
qux @1 :Qux;
}
struct Qux{
id @0 :UInt64;
}
interface Wrapper {
wrapped @0 (object :AnyPointer);
}

View File

@@ -119,3 +119,30 @@ def test_bundled_import_hook():
# stream.capnp should be bundled, or provided by the system capnproto
capnp.add_import_hook()
import stream_capnp # noqa: F401
def test_load_capnp(foo):
# test dynamically loading
loader = capnp.SchemaLoader()
loader.load(foo.Baz.schema.get_proto())
loader.load_dynamic(foo.Qux.schema.get_proto().node)
schema = loader.get(foo.Baz.schema.get_proto().node.id).as_struct()
assert "text" in schema.fieldnames
assert "qux" in schema.fieldnames
assert schema.fields["qux"].proto.slot.type.which == "struct"
class Wrapper(foo.Wrapper.Server):
def wrapped(self, object, **kwargs):
assert isinstance(object, capnp.lib.capnp._DynamicObjectReader)
baz_ = object.as_struct(schema)
assert baz_.text == "test"
assert baz_.qux.id == 2
# test calling into the wrapper with a Baz message.
baz_ = foo.Baz.new_message()
baz_.text = "test"
baz_.qux.id = 2
wrapper = foo.Wrapper._new_client(Wrapper())
wrapper.wrapped(baz_).wait()