Browse Source

Implemented CSHARP-276. Added overloads of the BsonArray constructor and AddRange and Create methods that take a non-generic IEnumerable parameter. Also removed the BsonDocumentToBsonArray mapping in BsonTypeMapper because it conflicts with the new IEnumerable overloads (and it was a rather far-fetched mapping anyway).

pull/63/head
rstam 14 years ago
parent
commit
a4b69e0001
  1. 42
      Bson/ObjectModel/BsonArray.cs
  2. 3
      Bson/ObjectModel/BsonTypeMapper.cs
  3. 1
      BsonUnitTests/BsonUnitTests.csproj
  4. 50
      BsonUnitTests/Jira/CSharp276Tests.cs
  5. 6
      BsonUnitTests/ObjectModel/BsonArrayTests.cs
  6. 2
      BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs

42
Bson/ObjectModel/BsonArray.cs

@ -139,6 +139,17 @@ namespace MongoDB.Bson {
AddRange(values);
}
/// <summary>
/// Initializes a new instance of the BsonArray class.
/// </summary>
/// <param name="values">A list of values to add to the array.</param>
public BsonArray(
IEnumerable values
)
: this(0) {
AddRange(values);
}
/// <summary>
/// Initializes a new instance of the BsonArray class.
/// </summary>
@ -339,6 +350,21 @@ namespace MongoDB.Bson {
}
}
/// <summary>
/// Creates a new BsonArray.
/// </summary>
/// <param name="values">A list of values to add to the array.</param>
/// <returns>A BsonArray or null.</returns>
public static BsonArray Create(
IEnumerable values
) {
if (values != null) {
return new BsonArray(values);
} else {
return null;
}
}
/// <summary>
/// Creates a new BsonArray.
/// </summary>
@ -530,6 +556,22 @@ namespace MongoDB.Bson {
return this;
}
/// <summary>
/// Adds multiple elements to the array.
/// </summary>
/// <param name="values">A list of values to add to the array.</param>
/// <returns>The array (so method calls can be chained).</returns>
public BsonArray AddRange(
IEnumerable values
) {
if (values != null) {
foreach (var value in values) {
this.values.Add(BsonValue.Create(value));
}
}
return this;
}
/// <summary>
/// Creates a shallow clone of the array (see also DeepClone).
/// </summary>

3
Bson/ObjectModel/BsonTypeMapper.cs

@ -75,7 +75,6 @@ namespace MongoDB.Bson {
{ Mapping.FromTo(typeof(BsonBinaryData), BsonType.Binary), Conversion.None },
{ Mapping.FromTo(typeof(BsonBoolean), BsonType.Boolean), Conversion.None },
{ Mapping.FromTo(typeof(BsonDateTime), BsonType.DateTime), Conversion.None },
{ Mapping.FromTo(typeof(BsonDocument), BsonType.Array), Conversion.BsonDocumentToBsonArray },
{ Mapping.FromTo(typeof(BsonDocument), BsonType.Document), Conversion.None },
{ Mapping.FromTo(typeof(BsonDouble), BsonType.Double), Conversion.None },
{ Mapping.FromTo(typeof(BsonInt32), BsonType.Int32), Conversion.None },
@ -319,7 +318,6 @@ namespace MongoDB.Bson {
case Conversion.CharToBsonInt64: return new BsonInt64((long) (char) value);
case Conversion.DateTimeOffsetToBsonDateTime: return new BsonDateTime(((DateTimeOffset) value).UtcDateTime);
case Conversion.DateTimeToBsonDateTime: return new BsonDateTime((DateTime) value);
case Conversion.BsonDocumentToBsonArray: return new BsonArray(((BsonDocument) value).Values);
case Conversion.DoubleToBsonBoolean: var d = (double) value; return BsonBoolean.Create(!(double.IsNaN(d) || d == 0.0));
case Conversion.GuidToBsonBinary: return new BsonBinaryData((Guid) value);
case Conversion.Int16ToBsonBoolean: return BsonBoolean.Create((short) value != 0);
@ -399,7 +397,6 @@ namespace MongoDB.Bson {
CharToBsonInt64,
DateTimeOffsetToBsonDateTime,
DateTimeToBsonDateTime,
BsonDocumentToBsonArray,
DoubleToBsonBoolean,
GuidToBsonBinary,
Int16ToBsonBoolean,

1
BsonUnitTests/BsonUnitTests.csproj

@ -126,6 +126,7 @@
<Compile Include="Jira\CSharp260Tests.cs" />
<Compile Include="Jira\CSharp261Tests.cs" />
<Compile Include="Jira\CSharp270Tests.cs" />
<Compile Include="Jira\CSharp276Tests.cs" />
<Compile Include="Jira\CSharp70Tests.cs" />
<Compile Include="Jira\CSharp71Tests.cs" />
<Compile Include="Jira\CSharp74Tests.cs" />

50
BsonUnitTests/Jira/CSharp276Tests.cs

@ -0,0 +1,50 @@
/* Copyright 2010-2011 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
namespace MongoDB.BsonUnitTests.Jira {
[TestFixture]
public class CSharp276Tests {
[Test]
public void TestConstructorWithNonGenericIEnumerable() {
IEnumerable values = new object[] { 1, "a" };
var array = new BsonArray(values);
Assert.AreEqual(2, array.Count);
Assert.AreEqual(BsonType.Int32, array[0].BsonType);
Assert.AreEqual(BsonType.String, array[1].BsonType);
Assert.AreEqual(1, array[0].AsInt32);
Assert.AreEqual("a", array[1].AsString);
}
[Test]
public void TestCreateWithNonGenericIEnumerable() {
IEnumerable values = new object[] { 1, "a" };
var array = BsonArray.Create(values);
Assert.AreEqual(2, array.Count);
Assert.AreEqual(BsonType.Int32, array[0].BsonType);
Assert.AreEqual(BsonType.String, array[1].BsonType);
Assert.AreEqual(1, array[0].AsInt32);
Assert.AreEqual("a", array[1].AsString);
}
}
}

6
BsonUnitTests/ObjectModel/BsonArrayTests.cs

@ -143,9 +143,9 @@ namespace MongoDB.BsonUnitTests {
[Test]
public void TestCreateFromObject() {
var values = new BsonDocument {
{ "a", 1 },
{ "b", 1.5 }
var values = (object) new object[] {
1,
1.5
};
var array = BsonArray.Create(values);
Assert.AreEqual(2, array.Count);

2
BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs

@ -107,8 +107,6 @@ namespace MongoDB.BsonUnitTests {
var value = new BsonDocument();
var bsonValue = (BsonDocument) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
var bsonArray = (BsonArray) BsonTypeMapper.MapToBsonValue(value, BsonType.Array);
Assert.AreEqual(0, bsonArray.Count);
var bsonDocument = (BsonDocument) BsonTypeMapper.MapToBsonValue(value, BsonType.Document);
Assert.AreSame(value, bsonDocument);
}

Loading…
Cancel
Save