Add handling of constants in schemas

This commit is contained in:
Jason Paryani
2013-08-25 17:07:08 -07:00
parent ea1f22e5c6
commit c6250962e6
4 changed files with 34 additions and 3 deletions

View File

@@ -87,6 +87,12 @@ cdef class _NodeReader:
property nestedNodes: property nestedNodes:
def __get__(self): def __get__(self):
return _List_NestedNode_Reader()._init(self.thisptr.getNestedNodes()) 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 class _NestedNodeReader:
cdef C_Node.NestedNode.Reader thisptr cdef C_Node.NestedNode.Reader thisptr
@@ -364,6 +370,9 @@ cdef class Schema:
self.thisptr = other self.thisptr = other
return self return self
cpdef asConstValue(self):
return _DynamicValueReader()._init(<C_DynamicValue.Reader>self.thisptr.asConst(), self).toPython()
cpdef asStruct(self): cpdef asStruct(self):
return StructSchema()._init(self.thisptr.asStruct()) return StructSchema()._init(self.thisptr.asStruct())
@@ -385,6 +394,9 @@ cdef class ParsedSchema:
self.thisptr = other self.thisptr = other
return self return self
cpdef asConstValue(self):
return _DynamicValueReader()._init(<C_DynamicValue.Reader>self.thisptr.asConst(), self).toPython()
cpdef asStruct(self): cpdef asStruct(self):
return StructSchema()._init(self.thisptr.asStruct()) return StructSchema()._init(self.thisptr.asStruct())
@@ -506,10 +518,11 @@ def _load(nodeSchema, module):
module.__dict__[node.name] = local_module module.__dict__[node.name] = local_module
schema = nodeSchema.getNested(node.name) schema = nodeSchema.getNested(node.name)
try: proto = schema.getProto()
if proto.isStruct:
local_module.Schema = schema.asStruct() local_module.Schema = schema.asStruct()
except: elif proto.isConst:
pass module.__dict__[node.name] = schema.asConstValue()
_load(schema, local_module) _load(schema, local_module)

View File

@@ -29,6 +29,7 @@ cdef extern from "capnp/schema.h" namespace " ::capnp":
Node.Reader getProto() except + Node.Reader getProto() except +
StructSchema asStruct() except + StructSchema asStruct() except +
EnumSchema asEnum() except + EnumSchema asEnum() except +
ConstSchema asConst() except +
Schema getDependency(uint64_t id) except + Schema getDependency(uint64_t id) except +
#InterfaceSchema asInterface() const; #InterfaceSchema asInterface() const;
@@ -64,6 +65,9 @@ cdef extern from "capnp/schema.h" namespace " ::capnp":
Enumerant getEnumerantByName(char * name) Enumerant getEnumerantByName(char * name)
Node.Reader getProto() Node.Reader getProto()
cdef cppclass ConstSchema:
pass
cdef extern from "capnp/schema-loader.h" namespace " ::capnp": cdef extern from "capnp/schema-loader.h" namespace " ::capnp":
cdef cppclass SchemaLoader: cdef cppclass SchemaLoader:
SchemaLoader() SchemaLoader()

View File

@@ -438,6 +438,12 @@ cdef extern from "capnp/schema.capnp.h" namespace " ::capnp::schema":
UInt64 getScopeId() UInt64 getScopeId()
List[Node.Node.NestedNode].Reader getNestedNodes() List[Node.Node.NestedNode].Reader getNestedNodes()
UInt64 getId() UInt64 getId()
bint isFile()
bint isStruct()
bint isEnum()
bint isInterface()
bint isConst()
bint isAnnotation()
cppclass Builder: cppclass Builder:
Node.Body getBody() Node.Body getBody()
@@ -452,6 +458,12 @@ cdef extern from "capnp/schema.capnp.h" namespace " ::capnp::schema":
List[Node.Node.NestedNode].Builder initNestedNodes(int) List[Node.Node.NestedNode].Builder initNestedNodes(int)
UInt64 getId() UInt64 getId()
void setId(UInt64) void setId(UInt64)
bint isFile()
bint isStruct()
bint isEnum()
bint isInterface()
bint isConst()
bint isAnnotation()
cdef cppclass AnnotationNode: cdef cppclass AnnotationNode:

View File

@@ -1,5 +1,7 @@
@0x934efea7f017fff0; @0x934efea7f017fff0;
const qux :UInt32 = 123;
struct Person { struct Person {
id @0 :UInt32; id @0 :UInt32;
name @1 :Text; name @1 :Text;