From f1c3511cebe2f3302daf17539fdf4dc18f7178ae Mon Sep 17 00:00:00 2001 From: Steve Wagner Date: Wed, 19 May 2010 07:25:14 +0200 Subject: [PATCH] Extract TypeCode to BinarySubtype. --- source/MongoDB.Tests/MongoDB.Tests.csproj | 1 + source/MongoDB.Tests/UnitTests/TestBinary.cs | 12 ++++----- .../UnitTests/Util/TestJsonUtils.cs | 2 +- source/MongoDB/Binary.cs | 25 +++---------------- source/MongoDB/BinarySubtype.cs | 21 ++++++++++++++++ source/MongoDB/Bson/BsonReader.cs | 4 +-- source/MongoDB/Bson/BsonWriter.cs | 4 +-- 7 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 source/MongoDB/BinarySubtype.cs diff --git a/source/MongoDB.Tests/MongoDB.Tests.csproj b/source/MongoDB.Tests/MongoDB.Tests.csproj index 0f1cfe25..802b7e1f 100644 --- a/source/MongoDB.Tests/MongoDB.Tests.csproj +++ b/source/MongoDB.Tests/MongoDB.Tests.csproj @@ -142,6 +142,7 @@ + diff --git a/source/MongoDB.Tests/UnitTests/TestBinary.cs b/source/MongoDB.Tests/UnitTests/TestBinary.cs index 7c6e2b73..e09a0d2d 100644 --- a/source/MongoDB.Tests/UnitTests/TestBinary.cs +++ b/source/MongoDB.Tests/UnitTests/TestBinary.cs @@ -10,14 +10,14 @@ public class TestBinary public void CanCreateBinary(){ var binary = new Binary(); Assert.IsNull(binary.Bytes); - Assert.AreEqual(Binary.TypeCode.Unknown, binary.Subtype); + Assert.AreEqual(BinarySubtype.Unknown, binary.Subtype); } [Test] public void CanCreateBinaryFromNull(){ var binary = new Binary(null); Assert.IsNull(binary.Bytes); - Assert.AreEqual(Binary.TypeCode.General, binary.Subtype); + Assert.AreEqual(BinarySubtype.General, binary.Subtype); } [Test] @@ -25,15 +25,15 @@ public class TestBinary var bytes = new byte[] { 10 }; var binary = new Binary(bytes); Assert.AreEqual(bytes,binary.Bytes); - Assert.AreEqual(Binary.TypeCode.General, binary.Subtype); + Assert.AreEqual(BinarySubtype.General, binary.Subtype); } [Test] public void CanCreateBinaryFromBytesAndSubtype(){ var bytes = new byte[] {10}; - var binary = new Binary(bytes,Binary.TypeCode.UserDefined); + var binary = new Binary(bytes,BinarySubtype.UserDefined); Assert.AreEqual(bytes, binary.Bytes); - Assert.AreEqual(Binary.TypeCode.UserDefined, binary.Subtype); + Assert.AreEqual(BinarySubtype.UserDefined, binary.Subtype); } [Test] @@ -55,7 +55,7 @@ public class TestBinary [Test] public void CanBeCloned(){ - var binarySource = new Binary(new byte[] {10, 20}, Binary.TypeCode.UserDefined); + var binarySource = new Binary(new byte[] {10, 20}, BinarySubtype.UserDefined); var binaryDest = binarySource.Clone() as Binary; Assert.IsNotNull(binaryDest); Assert.AreEqual(binarySource.Bytes,binaryDest.Bytes); diff --git a/source/MongoDB.Tests/UnitTests/Util/TestJsonUtils.cs b/source/MongoDB.Tests/UnitTests/Util/TestJsonUtils.cs index a3ac8f89..d035ed07 100644 --- a/source/MongoDB.Tests/UnitTests/Util/TestJsonUtils.cs +++ b/source/MongoDB.Tests/UnitTests/Util/TestJsonUtils.cs @@ -134,7 +134,7 @@ public class TestJsonUtils [Test] public void TestSerializeDocWithBinary(){ var doc = new Document(){{"b", new Binary(){Bytes = new byte[]{0,1,2,3,4}, - Subtype = Binary.TypeCode.General}}}; + Subtype = BinarySubtype.General}}}; Assert.AreEqual(@"{ ""b"": { ""$binary"": ""AAECAwQ="", ""$type"" : 2 } }", JsonFormatter.Serialize(doc)); } diff --git a/source/MongoDB/Binary.cs b/source/MongoDB/Binary.cs index f5eb2efb..06b714f4 100644 --- a/source/MongoDB/Binary.cs +++ b/source/MongoDB/Binary.cs @@ -8,25 +8,6 @@ namespace MongoDB /// public class Binary : IEquatable, ICloneable, IEnumerable { - /// - /// - public enum TypeCode : byte - { - /// - /// - Unknown = 0, - /// - /// - General = 2, - // Uuid = 3 is now replaced by Guid - /// - /// - Md5 = 5, - /// - /// - UserDefined = 80 - } - /// /// Initializes a new instance of the class. /// @@ -41,7 +22,7 @@ public Binary() public Binary(byte[] bytes) { Bytes = bytes; - Subtype = TypeCode.General; + Subtype = BinarySubtype.General; } /// @@ -49,7 +30,7 @@ public Binary(byte[] bytes) /// /// The bytes. /// The subtype. - public Binary(byte[] bytes, TypeCode subtype) + public Binary(byte[] bytes, BinarySubtype subtype) { Bytes = bytes; Subtype = subtype; @@ -65,7 +46,7 @@ public Binary(byte[] bytes, TypeCode subtype) /// Gets or sets the subtype. /// /// The subtype. - public TypeCode Subtype { get; set; } + public BinarySubtype Subtype { get; set; } /// /// Creates a new object that is a copy of the current instance. diff --git a/source/MongoDB/BinarySubtype.cs b/source/MongoDB/BinarySubtype.cs new file mode 100644 index 00000000..e7665971 --- /dev/null +++ b/source/MongoDB/BinarySubtype.cs @@ -0,0 +1,21 @@ +namespace MongoDB +{ + /// + /// + public enum BinarySubtype : byte + { + /// + /// + Unknown = 0, + /// + /// + General = 2, + // Uuid = 3 is now replaced by Guid + /// + /// + Md5 = 5, + /// + /// + UserDefined = 80 + } +} \ No newline at end of file diff --git a/source/MongoDB/Bson/BsonReader.cs b/source/MongoDB/Bson/BsonReader.cs index 165fef19..9cd93020 100644 --- a/source/MongoDB/Bson/BsonReader.cs +++ b/source/MongoDB/Bson/BsonReader.cs @@ -334,7 +334,7 @@ public BsonReader(Stream stream, BsonReaderSettings settings) Position += 4; var subtype = _reader.ReadByte(); Position ++; - if(subtype == (byte)Binary.TypeCode.General){ + if(subtype == (byte)BinarySubtype.General){ size = _reader.ReadInt32(); Position += 4; } @@ -348,7 +348,7 @@ public BsonReader(Stream stream, BsonReaderSettings settings) return new Binary{ Bytes = bytes, - Subtype = (Binary.TypeCode)subtype + Subtype = (BinarySubtype)subtype }; } diff --git a/source/MongoDB/Bson/BsonWriter.cs b/source/MongoDB/Bson/BsonWriter.cs index 3910dfb3..60608407 100644 --- a/source/MongoDB/Bson/BsonWriter.cs +++ b/source/MongoDB/Bson/BsonWriter.cs @@ -143,7 +143,7 @@ private void Write(Oid id) /// The binary. private void Write(Binary binary) { - if(binary.Subtype == Binary.TypeCode.General) + if(binary.Subtype == BinarySubtype.General) { _writer.Write(binary.Bytes.Length + 4); _writer.Write((byte)binary.Subtype); @@ -421,7 +421,7 @@ public int CalculateSize(Binary binary) { var size = 4; //size int size += 1; //subtype - if(binary.Subtype == Binary.TypeCode.General) + if(binary.Subtype == BinarySubtype.General) size += 4; //embedded size int size += binary.Bytes.Length; return size;