Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git://github.com/lanwin/mongodb-csharp

  • Loading branch information...
commit 6fd8ac414d0ed699a4875ae37b1263bba7d8f39a 2 parents 83750c0 + 53b848a
@samus authored
View
8 source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryTests.cs
@@ -37,7 +37,7 @@ public override void TestSetup()
FirstName = "Jane",
LastName = "McJane",
Age = 35,
- PrimaryAddress = new Address { City = "Paris", IsInternational = true, AddressType = AddressType.Private },
+ PrimaryAddress = new Address { City = "Paris", IsInternational = false, AddressType = AddressType.Private },
Addresses = new List<Address>
{
new Address { City = "Paris", AddressType = AddressType.Private }
@@ -76,7 +76,7 @@ public void Boolean()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => x.PrimaryAddress.IsInternational));
- Assert.AreEqual(3, people.Count);
+ Assert.AreEqual(2, people.Count);
}
[Test]
@@ -84,7 +84,7 @@ public void Boolean_Inverse()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => !x.PrimaryAddress.IsInternational));
- Assert.AreEqual(0, people.Count);
+ Assert.AreEqual(1, people.Count);
}
[Test]
@@ -92,7 +92,7 @@ public void Boolean_In_Conjunction()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => x.PrimaryAddress.IsInternational && x.Age > 21));
- Assert.AreEqual(2, people.Count);
+ Assert.AreEqual(1, people.Count);
}
[Test]
View
47 source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs
@@ -314,5 +314,52 @@ public void CanReadEmbeddedDocument()
Assert.AreEqual(1, embedded.Count);
Assert.AreEqual(10, embedded["value"]);
}
+
+ public class DictionaryWithEnumAsKeyHelper
+ {
+ public Dictionary<DateTimeKind, int> Dict { get; set; }
+ }
+
+ [Test]
+ public void SerializesAnEnumAsIntWhenItsUsedAsDictionaryKey()
+ {
+ var obj = new DictionaryWithEnumAsKeyHelper { Dict = new Dictionary<DateTimeKind, int> { { DateTimeKind.Utc, 9 } } };
+ var bson = Serialize<DictionaryWithEnumAsKeyHelper>(obj);
+ var doc = Deserialize<Document>(bson);
+
+ Assert.IsNotNull(doc);
+ var dict = doc["Dict"] as Document;
+ Assert.IsNotNull(dict);
+ Assert.AreEqual(1, dict.Count);
+ Assert.AreEqual(9, dict[Convert.ToString((int)DateTimeKind.Utc)]);
+ }
+
+ [Test]
+ public void CanDeserializeADictionaryWithEnumAsKey()
+ {
+ var bson = Serialize<Document>(new Document("Dict", new Document(( (int)DateTimeKind.Utc ).ToString(), 9)));
+ var prop = Deserialize<DictionaryWithEnumAsKeyHelper>(bson);
+
+ Assert.IsNotNull(prop);
+ Assert.IsNotNull(prop.Dict);
+ Assert.AreEqual(1,prop.Dict.Count);
+ Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
+ }
+
+ public class NullDictionaryPropertyHelper
+ {
+ public Dictionary<string, string> Dict { get; set; }
+ }
+
+ [Test]
+ public void CanDeserializeAndNullDictionaryProperty()
+ {
+ var bson = Serialize<Document>(new Document("Dict", null));
+ var prop = Deserialize<NullDictionaryPropertyHelper>(bson);
+
+ Assert.IsNotNull(prop);
+ Assert.IsNull(prop.Dict);
+ }
+
}
}
View
1  source/MongoDB/Attributes/MongoDefaultAttribute.cs
@@ -21,6 +21,7 @@ public MongoDefaultAttribute(object value)
/// Initializes a new instance of the <see cref="MongoDefaultAttribute"/> class.
/// </summary>
/// <param name="value">The value.</param>
+ /// <param name="persistDefaultValue">if set to <c>true</c> [persist default value].</param>
public MongoDefaultAttribute(object value, bool persistDefaultValue)
{
Value = value;
View
9 source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -34,7 +35,10 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
- return document.ToDictionary(pair => (TKey)Convert.ChangeType(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
+ if(document==null)
+ return null;
+
+ return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
}
/// <summary>
@@ -52,9 +56,10 @@ public Document GetDocument(object dictionary)
var doc = new Document();
foreach (var e in instance)
- doc.Add(e.Key.ToString(), e.Value);
+ doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
return doc;
}
+
}
}
View
8 source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -33,10 +34,13 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var list = new SortedList<TKey, TValue>();
foreach(var pair in document)
- list.Add((TKey)Convert.ChangeType(pair.Key, typeof(TKey)), (TValue)pair.Value);
+ list.Add((TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), (TValue)pair.Value);
return list;
}
@@ -56,7 +60,7 @@ public Document GetDocument(object dictionary)
var doc = new Document();
foreach (var e in instance)
- doc.Add(e.Key.ToString(), e.Value);
+ doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
return doc;
}
View
6 source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -33,6 +34,9 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var hashtable = new Hashtable();
foreach (var pair in document)
@@ -54,7 +58,7 @@ public Document GetDocument(object collection)
var doc = new Document();
foreach (DictionaryEntry entry in hashtable)
- doc.Add(entry.Key.ToString(), entry.Value);
+ doc.Add(ValueConverter.ConvertKey(entry.Key), entry.Value);
return doc;
}
View
16 source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
@@ -17,7 +17,10 @@ public static object Convert(object value, Type type)
var code = System.Convert.GetTypeCode(value);
if(type.IsEnum)
- value = Enum.ToObject(type, value);
+ if(value is string)
+ value = Enum.Parse(type, (string)value);
+ else
+ value = Enum.ToObject(type, value);
else if(type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>))
value = System.Convert.ChangeType(value, Nullable.GetUnderlyingType(type));
@@ -47,5 +50,16 @@ public static Array ConvertArray(object[] elements, Type type)
return array;
}
+
+ public static string ConvertKey(object key)
+ {
+ if(key == null)
+ throw new ArgumentNullException("key");
+
+ if(key is Enum)
+ return System.Convert.ToInt64(key).ToString();
+
+ return key.ToString();
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.