diff --git a/capnp/includes/capnp_cpp.pxd b/capnp/includes/capnp_cpp.pxd index b301472..b766731 100644 --- a/capnp/includes/capnp_cpp.pxd +++ b/capnp/includes/capnp_cpp.pxd @@ -133,8 +133,32 @@ cdef extern from "kj/async-io.h" namespace " ::kj": AsyncIoContext setupAsyncIo() +cdef extern from "capnp/schema.capnp.h" namespace " ::capnp": + enum TypeWhich" ::capnp::schema::Type::Which": + TypeWhichVOID " ::capnp::schema::Type::Which::VOID" + TypeWhichBOOL " ::capnp::schema::Type::Which::BOOL" + TypeWhichINT8 " ::capnp::schema::Type::Which::INT8" + TypeWhichINT16 " ::capnp::schema::Type::Which::INT16" + TypeWhichINT32 " ::capnp::schema::Type::Which::INT32" + TypeWhichINT64 " ::capnp::schema::Type::Which::INT64" + TypeWhichUINT8 " ::capnp::schema::Type::Which::UINT8" + TypeWhichUINT16 " ::capnp::schema::Type::Which::UINT16" + TypeWhichUINT32 " ::capnp::schema::Type::Which::UINT32" + TypeWhichUINT64 " ::capnp::schema::Type::Which::UINT64" + TypeWhichFLOAT32 " ::capnp::schema::Type::Which::FLOAT32" + TypeWhichFLOAT64 " ::capnp::schema::Type::Which::FLOAT64" + TypeWhichTEXT " ::capnp::schema::Type::Which::TEXT" + TypeWhichDATA " ::capnp::schema::Type::Which::DATA" + TypeWhichLIST " ::capnp::schema::Type::Which::LIST" + TypeWhichENUM " ::capnp::schema::Type::Which::ENUM" + TypeWhichSTRUCT " ::capnp::schema::Type::Which::STRUCT" + TypeWhichINTERFACE " ::capnp::schema::Type::Which::INTERFACE" + TypeWhichANY_POINTER " ::capnp::schema::Type::Which::ANY_POINTER" + cdef extern from "capnp/schema.h" namespace " ::capnp": cdef cppclass SchemaType" ::capnp::Type": + SchemaType() + SchemaType(TypeWhich) cbool isList() cbool isEnum() cbool isStruct() @@ -221,6 +245,7 @@ cdef extern from "capnp/schema.h" namespace " ::capnp": ListSchema listSchemaOfEnum" ::capnp::ListSchema::of"(EnumSchema) ListSchema listSchemaOfInterface" ::capnp::ListSchema::of"(InterfaceSchema) ListSchema listSchemaOfList" ::capnp::ListSchema::of"(ListSchema) + ListSchema listSchemaOfType" ::capnp::ListSchema::of"(SchemaType) cdef cppclass ConstSchema: pass diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index c69dca2..cfd7a42 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -2619,6 +2619,86 @@ cdef class _EnumSchema: def __get__(self): return _DynamicStructReader()._init(self.thisptr.getProto(), self) +cdef class _SchemaType: + cdef capnp.SchemaType thisptr + +types = _ModuleType('capnp.types') +cdef _SchemaType _void = _SchemaType() +_void.thisptr = capnp.SchemaType(capnp.TypeWhichVOID) +types.Void = _void + +cdef _SchemaType _bool = _SchemaType() +_bool.thisptr = capnp.SchemaType(capnp.TypeWhichBOOL) +types.Bool = _bool + +cdef _SchemaType _int8 = _SchemaType() +_int8.thisptr = capnp.SchemaType(capnp.TypeWhichINT8) +types.Int8 = _int8 + +cdef _SchemaType _int16 = _SchemaType() +_int16.thisptr = capnp.SchemaType(capnp.TypeWhichINT16) +types.Int16 = _int16 + +cdef _SchemaType _int32 = _SchemaType() +_int32.thisptr = capnp.SchemaType(capnp.TypeWhichINT32) +types.Int32 = _int32 + +cdef _SchemaType _int64 = _SchemaType() +_int64.thisptr = capnp.SchemaType(capnp.TypeWhichINT64) +types.Int64 = _int64 + +cdef _SchemaType _uint8 = _SchemaType() +_uint8.thisptr = capnp.SchemaType(capnp.TypeWhichUINT8) +types.UInt8 = _uint8 + +cdef _SchemaType _uint16 = _SchemaType() +_uint16.thisptr = capnp.SchemaType(capnp.TypeWhichUINT16) +types.UInt16 = _uint16 + +cdef _SchemaType _uint32 = _SchemaType() +_uint32.thisptr = capnp.SchemaType(capnp.TypeWhichUINT32) +types.UInt32 = _uint32 + +cdef _SchemaType _uint64 = _SchemaType() +_uint64.thisptr = capnp.SchemaType(capnp.TypeWhichUINT64) +types.UInt64 = _uint64 + +cdef _SchemaType _float32 = _SchemaType() +_float32.thisptr = capnp.SchemaType(capnp.TypeWhichFLOAT32) +types.Float32 = _float32 + +cdef _SchemaType _float64 = _SchemaType() +_float64.thisptr = capnp.SchemaType(capnp.TypeWhichFLOAT64) +types.Float64 = _float64 + +cdef _SchemaType _text = _SchemaType() +_text.thisptr = capnp.SchemaType(capnp.TypeWhichTEXT) +types.Text = _text + +cdef _SchemaType _data = _SchemaType() +_data.thisptr = capnp.SchemaType(capnp.TypeWhichDATA) +types.Data = _data + +# cdef _SchemaType _list = _SchemaType() +# _list.thisptr = capnp.SchemaType(capnp.TypeWhichLIST) +# types.list = _list + +cdef _SchemaType _enum = _SchemaType() +_enum.thisptr = capnp.SchemaType(capnp.TypeWhichENUM) +types.Enum = _enum + +# cdef _SchemaType _struct = _SchemaType() +# _struct.thisptr = capnp.SchemaType(capnp.TypeWhichSTRUCT) +# types.struct = _struct + +# cdef _SchemaType _interface = _SchemaType() +# _interface.thisptr = capnp.SchemaType(capnp.TypeWhichINTERFACE) +# types.interface = _interface + +cdef _SchemaType _any_pointer = _SchemaType() +_any_pointer.thisptr = capnp.SchemaType(capnp.TypeWhichANY_POINTER) +types.AnyPointer = _any_pointer + cdef class ListSchema: cdef C_ListSchema thisptr @@ -2627,6 +2707,7 @@ cdef class ListSchema: cdef _EnumSchema es cdef _InterfaceSchema iis cdef ListSchema ls + cdef _SchemaType st if schema is not None: if hasattr(schema, 'schema'): @@ -2647,6 +2728,9 @@ cdef class ListSchema: elif typeSchema is ListSchema: ls = s self.thisptr = capnp.listSchemaOfList(ls.thisptr) + elif typeSchema is _SchemaType: + st = s + self.thisptr = capnp.listSchemaOfType(st.thisptr) else: raise ValueError("Unknown schema type") diff --git a/test/annotations.capnp b/test/annotations.capnp index b372eda..bc41fa8 100644 --- a/test/annotations.capnp +++ b/test/annotations.capnp @@ -7,9 +7,11 @@ struct AnnotationStruct { annotation test1(*): Text; annotation test2(*): AnnotationStruct; annotation test3(*): List(AnnotationStruct); +annotation test4(*): List(UInt16); $test1("TestFile"); struct TestAnnotationOne $test1("Test") { } struct TestAnnotationTwo $test2(test = 100) { } struct TestAnnotationThree $test3([(test=100), (test=101)]) { } +struct TestAnnotationFour $test4([200, 201]) { } diff --git a/test/test_schema.py b/test/test_schema.py index eff28c1..0e472dd 100644 --- a/test/test_schema.py +++ b/test/test_schema.py @@ -44,3 +44,7 @@ def test_annotations(annotations): assert annotation_list[0].test == 100 assert annotation_list[1].test == 101 + annotation = annotations.TestAnnotationFour.schema.node.annotations[0] + annotation_list = annotation.value.list.as_list(capnp.ListSchema(capnp.types.UInt16)) + assert annotation_list[0] == 200 + assert annotation_list[1] == 201