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

Add deserialization for packed structures #85

Merged
merged 5 commits into from
Jul 16, 2016
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.sw[po]
__test__library__
dub.selections.json
dproto_dprotoc
36 changes: 26 additions & 10 deletions import/dproto/attributes.d
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ template ProtoAccessors()
alias __field = Identity!(__traits(getMember, this, __member));
alias __fieldData = getAnnotation!(__field, ProtoField);
if(__msgdata.msgNum == __fieldData.fieldNumber) {
enum wt = __fieldData.wireType;
__field.putProtoVal!wt(__r);
__r.putProtoVal!(__field)(__msgdata);
__matched = true;
}
}
Expand Down Expand Up @@ -162,15 +161,32 @@ void serializeField(alias field, R)(ref R r) const
serializer!fieldData(rawField, r);
}

void putProtoVal(string wireType, T, R)(ref T t, auto ref R r)
if(isProtoInputRange!R)
void putProtoVal(alias __field, R)(auto ref R r, ulong __msgdata)
if (isProtoInputRange!R)
{
static if(is(T : U[], U) && !(is(T : string) || is(T : const(ubyte)[]))) {
U u;
u.putSingleProtoVal!wireType(r);
t ~= u;
} else {
t.putSingleProtoVal!wireType(r);
import std.range : ElementType;
import std.traits : isSomeString, isDynamicArray;

alias T = typeof(__field);
enum wireType = getAnnotation!(__field, ProtoField).wireType;
enum isBinString(T) = Identity!(is(T : const(ubyte)[]));
static if (isDynamicArray!T && !(isSomeString!T || isBinString!T))
{
ElementType!T u;
ulong nelems = 1;
if (wireType.msgType != PACKED_MSG_TYPE && __msgdata.wireType == PACKED_MSG_TYPE)
{
nelems = r.readVarint();
}
for (auto n = 0; n < nelems; n++)
{
u.putSingleProtoVal!wireType(r);
__field ~= u;
}
}
else
{
__field.putSingleProtoVal!wireType(r);
}
}

Expand Down
38 changes: 38 additions & 0 deletions import/dproto/unittests.d
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,41 @@ unittest
}
};`)), "Malformed proto structure accepted");
}

unittest
{
mixin ProtocolBufferFromString!`
message Foo {
repeated uint32 arr = 1 [packed=true];
}
`;

Foo foo;
foo.arr = [1];

auto serialized_foo = foo.serialize();

auto foo2 = Foo(serialized_foo);
}

unittest
{
import std.algorithm;
mixin ProtocolBufferFromString!`
message FooA {
repeated uint32 arr = 1 [packed=true];
}
message FooB {
repeated uint32 arr = 1;
}
`;

FooA foo;
foo.arr = [1,3,5,7,2,4,6,8];

auto serialized_foo = foo.serialize();
auto foo2 = FooB(serialized_foo);
assert(equal(foo.arr, foo2.arr));
auto foo3 = FooA(foo2.serialize());
assert(equal(foo2.arr, foo3.arr));
}