Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for FIXED64, and ammendment to FIXED32 type #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/fastpb/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
40 changes: 37 additions & 3 deletions src/fastpb/template/module.jinjacc
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/fastpb/template/test.jinjapy
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down