Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c85f968

Browse filesBrowse files
committed
Replaced Binary.TypeCode.Uuid with true support for Guids
1 parent b99e647 commit c85f968
Copy full SHA for c85f968

File tree

4 files changed

+70
-23
lines changed
Filter options

4 files changed

+70
-23
lines changed

‎MongoDB.Net-Tests/Bson/TestRoundTrips.cs

Copy file name to clipboardExpand all lines: MongoDB.Net-Tests/Bson/TestRoundTrips.cs
+28-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ public void TestDateUTC(){
7979

8080
Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
8181

82-
}
82+
}
83+
84+
[Test]
85+
public void TestGUID()
86+
{
87+
MemoryStream ms = new MemoryStream();
88+
BsonWriter writer = new BsonWriter(ms);
89+
90+
Guid guid = Guid.NewGuid();
91+
92+
Document source = new Document();
93+
source.Append("uuid", guid);
94+
95+
/*Binary b = new Binary(guid.ToByteArray());
96+
b.Subtype = Binary.TypeCode.Uuid;
97+
source.Append("uuid", b);*/
98+
99+
writer.Write(source);
100+
writer.Flush();
101+
ms.Seek(0, SeekOrigin.Begin);
102+
103+
BsonReader reader = new BsonReader(ms);
104+
Document copy = reader.Read();
105+
106+
Guid read = (Guid)copy["uuid"];
107+
108+
Assert.AreEqual(guid, read, "UUID did not round trip right.");
109+
}
83110
}
84111
}

‎MongoDBDriver/Binary.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Binary.cs
+3-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ public class Binary{
88
public enum TypeCode:byte{
99
Unknown = 0,
1010
General = 2,
11-
12-
// Uuid is now replaced by Binary
13-
//Uuid = 3,
14-
15-
Md5 = 5,
11+
// Uuid is now replaced by Guid
12+
//Uuid = 3,
13+
Md5 = 5,
1614
UserDefined = 80
1715
}
1816

‎MongoDBDriver/Bson/BsonReader.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Bson/BsonReader.cs
+9-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ public Object ReadElementType (sbyte typeNum){
148148
}
149149
byte[] bytes = reader.ReadBytes (size);
150150
position += size;
151-
Binary b = new Binary ();
151+
152+
// From http://en.wikipedia.org/wiki/Universally_Unique_Identifier
153+
// The most widespread use of this standard is in Microsoft's Globally Unique Identifiers (GUIDs).
154+
if (subtype == 3 && 16 == size)
155+
{
156+
return new Guid(bytes);
157+
}
158+
159+
Binary b = new Binary();
152160
b.Bytes = bytes;
153161
b.Subtype = (Binary.TypeCode)subtype;
154162
return b;

‎MongoDBDriver/Bson/BsonWriter.cs

Copy file name to clipboardExpand all lines: MongoDBDriver/Bson/BsonWriter.cs
+30-16
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,22 @@ public void WriteValue(BsonDataType dt, Object obj){
117117
return;
118118
}
119119
case BsonDataType.Binary:{
120-
Binary b = (Binary)obj;
121-
if(b.Subtype == Binary.TypeCode.General){
122-
writer.Write(b.Bytes.Length + 4);
123-
writer.Write((byte)b.Subtype);
124-
writer.Write(b.Bytes.Length);
125-
}else{
126-
writer.Write(b.Bytes.Length);
127-
writer.Write((byte)b.Subtype);
120+
if (obj is Guid) {
121+
writer.Write((int)16);
122+
writer.Write((byte)3);
123+
writer.Write(((Guid)obj).ToByteArray());
124+
} else {
125+
Binary b = (Binary)obj;
126+
if(b.Subtype == Binary.TypeCode.General){
127+
writer.Write(b.Bytes.Length + 4);
128+
writer.Write((byte)b.Subtype);
129+
writer.Write(b.Bytes.Length);
130+
}else{
131+
writer.Write(b.Bytes.Length);
132+
writer.Write((byte)b.Subtype);
133+
}
134+
writer.Write(b.Bytes);
128135
}
129-
writer.Write(b.Bytes);
130136
return;
131137
}
132138
default:
@@ -202,14 +208,20 @@ public int CalculateSize(Object val){
202208
return size;
203209
}
204210
case BsonDataType.Binary:{
205-
Binary b = (Binary)val;
206-
int size = 4; //size int
207-
size += 1; //subtype
208-
if(b.Subtype == Binary.TypeCode.General){
209-
size += 4; //embedded size int
211+
if (val is Guid)
212+
return 21;
213+
else
214+
{
215+
Binary b = (Binary)val;
216+
int size = 4; //size int
217+
size += 1; //subtype
218+
if (b.Subtype == Binary.TypeCode.General)
219+
{
220+
size += 4; //embedded size int
221+
}
222+
size += b.Bytes.Length;
223+
return size;
210224
}
211-
size += b.Bytes.Length;
212-
return size;
213225
}
214226
default:
215227
throw new NotImplementedException(String.Format("Calculating size of {0} is not implemented.",val.GetType().Name));
@@ -295,6 +307,8 @@ protected BsonDataType TranslateToBsonType(Object val){
295307
ret = BsonDataType.Null;
296308
}else if(t == typeof(Binary)){
297309
ret = BsonDataType.Binary;
310+
}else if(t == typeof(Guid)){
311+
ret = BsonDataType.Binary;
298312
}else if(t == typeof(MongoMinKey)){
299313
ret = BsonDataType.MinKey;
300314
}else if(t == typeof(MongoMaxKey)){

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.