From c6250962e68021eef5f67761c5302ecc4aa05236 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sun, 25 Aug 2013 17:07:08 -0700 Subject: [PATCH] Add handling of constants in schemas --- capnp/capnp.pyx | 19 ++++++++++++++++--- capnp/capnp_cpp.pxd | 4 ++++ capnp/schema_cpp.pxd | 12 ++++++++++++ examples/addressbook.capnp | 2 ++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index fab0831..e219fa6 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -87,6 +87,12 @@ cdef class _NodeReader: property nestedNodes: def __get__(self): return _List_NestedNode_Reader()._init(self.thisptr.getNestedNodes()) + property isStruct: + def __get__(self): + return self.thisptr.isStruct() + property isConst: + def __get__(self): + return self.thisptr.isConst() cdef class _NestedNodeReader: cdef C_Node.NestedNode.Reader thisptr @@ -364,6 +370,9 @@ cdef class Schema: self.thisptr = other return self + cpdef asConstValue(self): + return _DynamicValueReader()._init(self.thisptr.asConst(), self).toPython() + cpdef asStruct(self): return StructSchema()._init(self.thisptr.asStruct()) @@ -385,6 +394,9 @@ cdef class ParsedSchema: self.thisptr = other return self + cpdef asConstValue(self): + return _DynamicValueReader()._init(self.thisptr.asConst(), self).toPython() + cpdef asStruct(self): return StructSchema()._init(self.thisptr.asStruct()) @@ -506,10 +518,11 @@ def _load(nodeSchema, module): module.__dict__[node.name] = local_module schema = nodeSchema.getNested(node.name) - try: + proto = schema.getProto() + if proto.isStruct: local_module.Schema = schema.asStruct() - except: - pass + elif proto.isConst: + module.__dict__[node.name] = schema.asConstValue() _load(schema, local_module) diff --git a/capnp/capnp_cpp.pxd b/capnp/capnp_cpp.pxd index 4ed5917..efa3f3a 100644 --- a/capnp/capnp_cpp.pxd +++ b/capnp/capnp_cpp.pxd @@ -29,6 +29,7 @@ cdef extern from "capnp/schema.h" namespace " ::capnp": Node.Reader getProto() except + StructSchema asStruct() except + EnumSchema asEnum() except + + ConstSchema asConst() except + Schema getDependency(uint64_t id) except + #InterfaceSchema asInterface() const; @@ -64,6 +65,9 @@ cdef extern from "capnp/schema.h" namespace " ::capnp": Enumerant getEnumerantByName(char * name) Node.Reader getProto() + cdef cppclass ConstSchema: + pass + cdef extern from "capnp/schema-loader.h" namespace " ::capnp": cdef cppclass SchemaLoader: SchemaLoader() diff --git a/capnp/schema_cpp.pxd b/capnp/schema_cpp.pxd index 41157cf..c779541 100644 --- a/capnp/schema_cpp.pxd +++ b/capnp/schema_cpp.pxd @@ -438,6 +438,12 @@ cdef extern from "capnp/schema.capnp.h" namespace " ::capnp::schema": UInt64 getScopeId() List[Node.Node.NestedNode].Reader getNestedNodes() UInt64 getId() + bint isFile() + bint isStruct() + bint isEnum() + bint isInterface() + bint isConst() + bint isAnnotation() cppclass Builder: Node.Body getBody() @@ -452,6 +458,12 @@ cdef extern from "capnp/schema.capnp.h" namespace " ::capnp::schema": List[Node.Node.NestedNode].Builder initNestedNodes(int) UInt64 getId() void setId(UInt64) + bint isFile() + bint isStruct() + bint isEnum() + bint isInterface() + bint isConst() + bint isAnnotation() cdef cppclass AnnotationNode: diff --git a/examples/addressbook.capnp b/examples/addressbook.capnp index 02e626a..e1cd77c 100644 --- a/examples/addressbook.capnp +++ b/examples/addressbook.capnp @@ -1,5 +1,7 @@ @0x934efea7f017fff0; +const qux :UInt32 = 123; + struct Person { id @0 :UInt32; name @1 :Text;