Skip to content

Commit d26ea0e

Browse files
committed
CSHARP-1362: Support new Find server command.
1 parent 3d304f6 commit d26ea0e

File tree

44 files changed

+4784
-302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4784
-302
lines changed

src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
<Compile Include="Serialization\Serializers\KeyValuePairSerializerTests.cs" />
155155
<Compile Include="Serialization\Serializers\LazyBsonArraySerializerTests.cs" />
156156
<Compile Include="Serialization\Serializers\LazyBsonDocumentSerializerTests.cs" />
157+
<Compile Include="Serialization\Serializers\PartiallyRawBsonDocumentSerializerTests.cs" />
157158
<Compile Include="Serialization\Serializers\RawBsonArraySerializerTests.cs" />
158159
<Compile Include="Serialization\Serializers\RawBsonDocumentSerializerTests.cs" />
159160
<Compile Include="Serialization\Serializers\SerializeFlagsTests.cs" />
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.IO;
18+
using FluentAssertions;
19+
using MongoDB.Bson.IO;
20+
using MongoDB.Bson.Serialization;
21+
using MongoDB.Bson.Serialization.Serializers;
22+
using NUnit.Framework;
23+
24+
namespace MongoDB.Bson.Tests.Serialization.Serializers
25+
{
26+
[TestFixture]
27+
public class PartiallyRawBsonDocumentSerializerTests
28+
{
29+
[Test]
30+
public void constructor_should_throw_when_name_is_null()
31+
{
32+
Action action = () => new PartiallyRawBsonDocumentSerializer(null, BsonDocumentSerializer.Instance);
33+
34+
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("name");
35+
}
36+
37+
[Test]
38+
public void constructor_should_throw_when_rawSerializer_is_null()
39+
{
40+
Action action = () => new PartiallyRawBsonDocumentSerializer("name", null);
41+
42+
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("rawSerializer");
43+
}
44+
45+
[Test]
46+
public void constructor_should_throw_when_rawSerializer_is_not_a_BsonValue_serializer()
47+
{
48+
Action action = () => new PartiallyRawBsonDocumentSerializer("name", new Int32Serializer());
49+
50+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("rawSerializer");
51+
}
52+
53+
[Test]
54+
public void Deserialize_should_return_partially_raw_BsonDocument()
55+
{
56+
var document = new BsonDocument
57+
{
58+
{ "a", new BsonDocument("x", 1) },
59+
{ "b", new BsonDocument("x", 2) },
60+
{ "c", new BsonDocument("x", 3) }
61+
};
62+
var bson = document.ToBson();
63+
var subject = new PartiallyRawBsonDocumentSerializer("b", RawBsonDocumentSerializer.Instance);
64+
65+
var result = Deserialize(bson, subject);
66+
67+
result["a"].Should().BeOfType<BsonDocument>();
68+
result["b"].Should().BeOfType<RawBsonDocument>();
69+
result["c"].Should().BeOfType<BsonDocument>();
70+
}
71+
72+
[Test]
73+
public void Deserialize_should_return_nested_partially_raw_BsonDocument()
74+
{
75+
var document = new BsonDocument
76+
{
77+
{ "a", new BsonDocument("x", 1) },
78+
{ "b", new BsonDocument
79+
{
80+
{ "d", new BsonDocument("z", 1) },
81+
{ "e", new BsonDocument("z", 2) },
82+
{ "f", new BsonDocument("z", 3) },
83+
}
84+
},
85+
{ "c", new BsonDocument("x", 3) }
86+
};
87+
var bson = document.ToBson();
88+
var subject = new PartiallyRawBsonDocumentSerializer("b",
89+
new PartiallyRawBsonDocumentSerializer("e", RawBsonDocumentSerializer.Instance));
90+
91+
var result = Deserialize(bson, subject);
92+
93+
result["a"].Should().BeOfType<BsonDocument>();
94+
result["b"].Should().BeOfType<BsonDocument>();
95+
result["c"].Should().BeOfType<BsonDocument>();
96+
result["b"]["d"].Should().BeOfType<BsonDocument>();
97+
result["b"]["e"].Should().BeOfType<RawBsonDocument>();
98+
result["b"]["f"].Should().BeOfType<BsonDocument>();
99+
}
100+
101+
// private methods
102+
private BsonDocument Deserialize(byte[] bson, PartiallyRawBsonDocumentSerializer serializer)
103+
{
104+
using (var stream = new MemoryStream(bson))
105+
using (var reader = new BsonBinaryReader(stream))
106+
{
107+
var context = BsonDeserializationContext.CreateRoot(reader);
108+
return serializer.Deserialize(context);
109+
}
110+
}
111+
}
112+
}

src/MongoDB.Bson/MongoDB.Bson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<Compile Include="Serialization\Serializers\BsonBinaryDataSerializer.cs" />
218218
<Compile Include="Serialization\Serializers\BsonBooleanSerializer.cs" />
219219
<Compile Include="Serialization\IBsonSerializerExtensions.cs" />
220+
<Compile Include="Serialization\Serializers\PartiallyRawBsonDocumentSerializer.cs" />
220221
<Compile Include="Serialization\Serializers\ProjectingDeserializer.cs" />
221222
<Compile Include="Serialization\Serializers\SerializerHelper.cs" />
222223
<Compile Include="Serialization\Serializers\DiscriminatedWrapperSerializer.cs" />
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Copyright 2015 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson.IO;
18+
19+
namespace MongoDB.Bson.Serialization.Serializers
20+
{
21+
/// <summary>
22+
/// Represents a serializer for a BsonDocument with some parts raw.
23+
/// </summary>
24+
public class PartiallyRawBsonDocumentSerializer : SerializerBase<BsonDocument>
25+
{
26+
// private fields
27+
private readonly string _name;
28+
private readonly IBsonSerializer _rawSerializer;
29+
30+
// constructors
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="PartiallyRawBsonDocumentSerializer"/> class.
33+
/// </summary>
34+
/// <param name="name">The name.</param>
35+
/// <param name="rawSerializer">The raw serializer.</param>
36+
public PartiallyRawBsonDocumentSerializer(string name, IBsonSerializer rawSerializer)
37+
{
38+
if (name == null)
39+
{
40+
throw new ArgumentNullException("name");
41+
}
42+
if (rawSerializer == null)
43+
{
44+
throw new ArgumentNullException("rawSerializer");
45+
}
46+
if (!typeof(BsonValue).IsAssignableFrom(rawSerializer.ValueType))
47+
{
48+
throw new ArgumentException("RawSerializer ValueType must be a BsonValue.", "rawSerializer");
49+
}
50+
51+
_name = name;
52+
_rawSerializer = rawSerializer;
53+
}
54+
55+
// public methods
56+
/// <inheritdoc/>
57+
public override BsonDocument Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
58+
{
59+
var document = new BsonDocument();
60+
61+
var reader = context.Reader;
62+
reader.ReadStartDocument();
63+
while (reader.ReadBsonType() != 0)
64+
{
65+
var name = reader.ReadName();
66+
var serializer = ChooseSerializer(name);
67+
var value = (BsonValue)serializer.Deserialize(context);
68+
document[name] = value;
69+
}
70+
reader.ReadEndDocument();
71+
72+
return document;
73+
}
74+
75+
private IBsonSerializer ChooseSerializer(string name)
76+
{
77+
if (name == _name)
78+
{
79+
return _rawSerializer;
80+
}
81+
else
82+
{
83+
return BsonValueSerializer.Instance;
84+
}
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)