diff --git a/Bson/ObjectModel/BsonArray.cs b/Bson/ObjectModel/BsonArray.cs index 1c7bf99741..b652d1b7df 100644 --- a/Bson/ObjectModel/BsonArray.cs +++ b/Bson/ObjectModel/BsonArray.cs @@ -139,6 +139,17 @@ namespace MongoDB.Bson { AddRange(values); } + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IEnumerable values + ) + : this(0) { + AddRange(values); + } + /// /// Initializes a new instance of the BsonArray class. /// @@ -339,6 +350,21 @@ namespace MongoDB.Bson { } } + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IEnumerable values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + /// /// Creates a new BsonArray. /// @@ -530,6 +556,22 @@ namespace MongoDB.Bson { return this; } + /// + /// Adds multiple elements to the array. + /// + /// A list of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IEnumerable values + ) { + if (values != null) { + foreach (var value in values) { + this.values.Add(BsonValue.Create(value)); + } + } + return this; + } + /// /// Creates a shallow clone of the array (see also DeepClone). /// diff --git a/Bson/ObjectModel/BsonTypeMapper.cs b/Bson/ObjectModel/BsonTypeMapper.cs index 94730a3c87..87e58049b4 100644 --- a/Bson/ObjectModel/BsonTypeMapper.cs +++ b/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, diff --git a/BsonUnitTests/BsonUnitTests.csproj b/BsonUnitTests/BsonUnitTests.csproj index fd1670af66..21681ae479 100644 --- a/BsonUnitTests/BsonUnitTests.csproj +++ b/BsonUnitTests/BsonUnitTests.csproj @@ -126,6 +126,7 @@ + diff --git a/BsonUnitTests/Jira/CSharp276Tests.cs b/BsonUnitTests/Jira/CSharp276Tests.cs new file mode 100644 index 0000000000..44ddc08c58 --- /dev/null +++ b/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); + } + } +} diff --git a/BsonUnitTests/ObjectModel/BsonArrayTests.cs b/BsonUnitTests/ObjectModel/BsonArrayTests.cs index ccc3eda544..cb20cfcee6 100644 --- a/BsonUnitTests/ObjectModel/BsonArrayTests.cs +++ b/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); diff --git a/BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs b/BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs index 384cc54047..dda064fd06 100644 --- a/BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs +++ b/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); }