Commits

Rune Halvorsen committed aba90fc

Added support for default values in enums

Comments (0)

Files changed (2)

     def to_wiretype(self, value):
         return struct.pack("<I", value)
 
+#Why does the type checker not run when parsing?
 #    def check_type(self, value):
  #       print "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
 
     def __get__(self, obj, typ):
         return typ.__name__ #  fixme: Need to deal with META for alternate typename?
 
+class DefaultValueDescriptor(object):
+
+    def __get__(self, obj, typ):
+        for key, val in typ.__dict__.items():
+            if key.isupper() and isinstance(val, int):
+                return val
+        return 0
+
 
 class ProtoBufEnum(VarIntField):
     typename = TypeNameDescriptor()
+    default = DefaultValueDescriptor()
 
     @classmethod
     def get_proto_def(cls):

tests/test_fields.py

     assert m1 is not m2
     assert m1.s == m2.s
     assert m1.e == m2.e
-    assert m1.i == m2.i
+    assert m1.i == m2.i
+
+    m3 = Msg1()
+    print m3.e
+    assert m3.e == Test1.FOO  # default value