Skip to content

Commit

Permalink
Bson: Adds std.uuid.UUID support (#1404)
Browse files Browse the repository at this point in the history
Add Bson to and from UUID convertion support.
  • Loading branch information
denizzzka authored and s-ludwig committed Nov 5, 2017
1 parent 30a8ac9 commit 9a8b026
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions data/vibe/data/bson.d
Expand Up @@ -67,6 +67,7 @@ import std.base64;
import std.bitmanip;
import std.conv;
import std.datetime;
import std.uuid: UUID;
import std.exception;
import std.range;
import std.traits;
Expand Down Expand Up @@ -216,6 +217,8 @@ struct Bson {
this(long value) { opAssign(value); }
/// ditto
this(in Json value) { opAssign(value); }
/// ditto
this(in UUID value) { opAssign(value); }

/**
Assigns a D type to a BSON value.
Expand Down Expand Up @@ -346,6 +349,11 @@ struct Bson {
m_type = writeBson(app, value);
m_data = app.data;
}
/// ditto
void opAssign(in UUID value)
{
opAssign(BsonBinData(BsonBinData.Type.uuid, value.data.idup));
}

/**
Returns the BSON type of this value.
Expand Down Expand Up @@ -429,6 +437,13 @@ struct Bson {
pragma(msg, "Bson.get!Json() and Bson.opCast!Json() will soon be removed. Please use Bson.toJson() instead.");
return this.toJson();
}
else static if( is(T == UUID) ){
checkType(Type.binData);
auto bbd = this.get!BsonBinData();
enforce(bbd.type == BsonBinData.Type.uuid, "BsonBinData value is type '"~to!string(bbd.type)~"', expected to be uuid");
const ubyte[16] b = bbd.rawData;
return UUID(b);
}
else static assert(false, "Cannot cast "~typeof(this).stringof~" to '"~T.stringof~"'.");
}

Expand Down Expand Up @@ -589,6 +604,18 @@ struct Bson {
assert(value["c"] == Bson("foo"));
}

///
unittest {
auto srcUuid = UUID("00010203-0405-0607-0809-0a0b0c0d0e0f");

Bson b = srcUuid;
auto u = b.get!UUID();

assert(b.type == Bson.Type.binData);
assert(b.get!BsonBinData().type == BsonBinData.Type.uuid);
assert(u == srcUuid);
}

/** Allows index based access of a BSON array value.
Returns a null value if the index is out of bounds.
Expand Down

0 comments on commit 9a8b026

Please sign in to comment.