diff --git a/README.md b/README.md index f09781c..3e10b7d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Python's json serializer. This is a very early stage project. It works for our needs. We haven't verified it works beyond that. Issue reports and patches are very much appreciated! -For example, it only supports strint, int32, int64, double, and sub message members at this time. +For example, it only supports string, int32, int64, double, fixed32, fixed64, and sub message members at this time. ### Pre-requisites: diff --git a/src/fastpb/generator.py b/src/fastpb/generator.py index 71d83fe..b997715 100755 --- a/src/fastpb/generator.py +++ b/src/fastpb/generator.py @@ -44,6 +44,7 @@ 'BOOL': descriptor_pb2.FieldDescriptorProto.TYPE_BOOL, 'ENUM': descriptor_pb2.FieldDescriptorProto.TYPE_ENUM, 'FIXED32': descriptor_pb2.FieldDescriptorProto.TYPE_FIXED32, + 'FIXED64': descriptor_pb2.FieldDescriptorProto.TYPE_FIXED64, # TODO(robbyw): More types. } diff --git a/src/fastpb/template/module.jinjacc b/src/fastpb/template/module.jinjacc index eb51fbe..54895c0 100644 --- a/src/fastpb/template/module.jinjacc +++ b/src/fastpb/template/module.jinjacc @@ -44,9 +44,15 @@ fastpb_convert{{ TYPE.UINT32 }}(::google::protobuf::uint32 value) } static PyObject * -fastpb_convert{{ TYPE.FIXED32 }}(::google::protobuf::int32 value) +fastpb_convert{{ TYPE.FIXED32 }}(::google::protobuf::uint32 value) { - return PyLong_FromLong(value); + return PyLong_FromUnsignedLong(value); +} + +static PyObject * +fastpb_convert{{ TYPE.FIXED64 }}(::google::protobuf::uint64 value) +{ + return PyLong_FromUnsignedLongLong(value); } static PyObject * @@ -419,7 +425,7 @@ namespace { return -1; } - {% elif member.type == TYPE.INT32 or member.type == TYPE.SINT32 or member.type == TYPE.FIXED32%} + {% elif member.type == TYPE.INT32 or member.type == TYPE.SINT32 %} ::google::protobuf::int32 protoValue; // int32 @@ -431,6 +437,34 @@ namespace { return -1; } + {% elif member.type == TYPE.FIXED64 %} + ::google::protobuf::uint64 protoValue; + + // uint64 + if (PyInt_Check(value)) { + protoValue = PyInt_AsUnsignedLongLongMask(value); + } else if (PyLong_Check(value)) { + protoValue = PyLong_AsUnsignedLongLong(value); + } else { + PyErr_SetString(PyExc_TypeError, + "The {{ member.name }} attribute value must be an integer"); + return -1; + } + + {% elif member.type == TYPE.FIXED32 %} + ::google::protobuf::uint32 protoValue; + + // uint32 + if (PyInt_Check(value)) { + protoValue = PyInt_AsUnsignedLongMask(value); + } else if (PyLong_Check(value)) { + protoValue = PyLong_AsUnsignedLong(value); + } else { + PyErr_SetString(PyExc_TypeError, + "The {{ member.name }} attribute value must be an integer"); + return -1; + } + {% elif member.type == TYPE.ENUM %} // {{ member.type_name }} {{ member.type_name.replace('.', '::') }} protoValue; diff --git a/src/fastpb/template/test.jinjapy b/src/fastpb/template/test.jinjapy index f6d4439..4f3fd01 100644 --- a/src/fastpb/template/test.jinjapy +++ b/src/fastpb/template/test.jinjapy @@ -24,7 +24,9 @@ import {{ file.package }} or field.type == TYPE.UINT32 or field.type == TYPE.INT64 or field.type == TYPE.SINT64 - or field.type == TYPE.UINT64 -%} + or field.type == TYPE.UINT64 + or field.type == TYPE.FIXED32 + or field.type == TYPE.FIXED64 -%} {{ index }} {%- elif field.type == TYPE.DOUBLE -%} {{ index }}.{{ index }}