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 serialization support for std.typecons.Typedef!T. #1617

Merged
merged 1 commit into from
May 12, 2017
Merged
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
18 changes: 16 additions & 2 deletions data/vibe/data/serialization.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
and will always be serialized as a string.)
$(LI Any `Nullable!T` will be serialized as either `null`, or
as the contained value (subject to these rules again).)
$(LI Any `Typedef!T` will be serialized as if it were just `T`.)
$(LI Any `BitFlags!T` value will be serialized as `T[]`)
$(LI Types satisfying the `isPolicySerializable` trait for the
supplied `Policy` will be serialized as the value returned
Expand Down Expand Up @@ -339,7 +340,7 @@ private template serializeValueImpl(Serializer, alias Policy) {

private void serializeValue(T, ATTRIBUTES...)(ref Serializer ser, T value)
{
import std.typecons : BitFlags, Nullable, Tuple, tuple;
import std.typecons : BitFlags, Nullable, Tuple, Typedef, TypedefType, tuple;

alias TU = Unqual!T;

Expand Down Expand Up @@ -413,6 +414,8 @@ private template serializeValueImpl(Serializer, alias Policy) {
} else static if (/*isInstanceOf!(Nullable, TU)*/is(T == Nullable!TPS, TPS...)) {
if (value.isNull()) ser.serializeValue!(typeof(null))(null);
else ser.serializeValue!(typeof(value.get()), ATTRIBUTES)(value.get());
} else static if (isInstanceOf!(Typedef, TU)) {
ser.serializeValue!(TypedefType!TU, ATTRIBUTES)(cast(TypedefType!TU)value);
} else static if (is(TU == BitFlags!E, E)) {
alias STraits = SubTraits!(Traits, E);

Expand Down Expand Up @@ -525,7 +528,7 @@ private template deserializeValueImpl(Serializer, alias Policy) {

T deserializeValue(T, ATTRIBUTES...)(ref Serializer ser) if(isMutable!T)
{
import std.typecons : BitFlags, Nullable;
import std.typecons : BitFlags, Nullable, Typedef, TypedefType;

static struct Traits {
alias Type = T;
Expand Down Expand Up @@ -589,6 +592,8 @@ private template deserializeValueImpl(Serializer, alias Policy) {
} else static if (isInstanceOf!(Nullable, T)) {
if (ser.tryReadNull!Traits()) return T.init;
return T(ser.deserializeValue!(typeof(T.init.get()), ATTRIBUTES));
} else static if (isInstanceOf!(Typedef, T)) {
return T(ser.deserializeValue!(TypedefType!T, ATTRIBUTES));
} else static if (is(T == BitFlags!E, E)) {
alias STraits = SubTraits!(Traits, E);
T ret;
Expand Down Expand Up @@ -1579,3 +1584,12 @@ unittest {
auto deser = deserialize!(TestSerializer, Custom)(ser);
assert(deser.x == 42);
}

unittest {
import std.typecons : Typedef;
alias T = Typedef!int;
auto ser = serialize!TestSerializer(T(42));
assert(ser == "V(i)(42)", ser);
auto deser = deserialize!(TestSerializer, T)(ser);
assert(deser == 42);
}