Skip to content

Commit

Permalink
Merge pull request #278 from henon/master
Browse files Browse the repository at this point in the history
Better error message on impossible cast
  • Loading branch information
mbdavid authored Aug 22, 2016
2 parents 3b9a81d + 8424975 commit 91a7472
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
10 changes: 5 additions & 5 deletions LiteDB.Tests/Tests/ShrinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class ShrinkTest : TestBase

public ShrinkTest()
{
_customerName = $"Jo{new string('h', 1000)}n Doe";
_productName = $"Prod{new string('u', 1000)}ct";
_customerName = "Jo"+new string('h', 1000)+"n Doe";
_productName = "Prod"+new string('u', 1000)+"ct";
}

[TestMethod]
Expand Down Expand Up @@ -51,7 +51,7 @@ public void Shrink_One_Test()
{
var customers = db.GetCollection<Customer>("customers");

Create(customers, id => new Customer { Id = id, Name = $"{_customerName}{id}" });
Create(customers, id => new Customer { Id = id, Name = ""+_customerName+id });

sizeAfterInserts = GetLocalDbSizeMegabytes(tmp.Filename);

Expand Down Expand Up @@ -79,8 +79,8 @@ public void Shrink_Many_Test()
var products = db.GetCollection<Product>("products");
var customers = db.GetCollection<Customer>("customers");

Create(products, id => new Product { ProductId = id, Name = $"{_customerName}{id}" });
Create(customers, id => new Customer { Id = id, Name = $"{_productName}{id}" });
Create(products, id => new Product { ProductId = id, Name = ""+_customerName+id });
Create(customers, id => new Customer { Id = id, Name = "" + _customerName + id });

sizeAfterInserts = GetLocalDbSizeMegabytes(tmp.Filename);

Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Query/Linq/QueryVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ private BsonValue VisitValue(Expression expr, Expression left)
{
// check if left side is an enum and convert to string before return
Func<Type, object, BsonValue> convert = (type, value) =>
{
var enumType = (left as UnaryExpression)?.Operand.Type;
{
var enumType = (left as UnaryExpression) == null ? null : (left as UnaryExpression).Operand.Type;
if (enumType != null && enumType.GetTypeInfo().IsEnum)
{
Expand Down
42 changes: 28 additions & 14 deletions LiteDB/Serializer/Mapper/BsonMapper.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,41 @@ private void DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocumen
private void DeserializeObject(Type type, object obj, BsonDocument value)
{
var props = this.GetPropertyMapper(type);

foreach (var prop in props.Values)
PropertyMapper property=null;
BsonValue bson_value=null;
try
{
// property is read only
if (prop.Setter == null) continue;

var val = value[prop.FieldName];

if (!val.IsNull)
foreach (var prop in props.Values)
{
// check if has a custom deserialize function
if (prop.Deserialize != null)
{
prop.Setter(obj, prop.Deserialize(val, this));
}
else
property = prop;
// property is read only
if (prop.Setter == null) continue;

var val = value[prop.FieldName];
bson_value = val;

if (!val.IsNull)
{
prop.Setter(obj, this.Deserialize(prop.PropertyType, val));
// check if has a custom deserialize function
if (prop.Deserialize != null)
{
prop.Setter(obj, prop.Deserialize(val, this));

}
else
{
prop.Setter(obj, this.Deserialize(prop.PropertyType, val));
}
}
}
}
catch (InvalidCastException e)
{
var typename = property == null ? null : property.PropertyType.Name;
var msg = string.Format("Cast from '{0}' to '{1}' failed in document: {2}", bson_value, typename, value);
throw new InvalidCastException(msg, e);
}
}
}
}

0 comments on commit 91a7472

Please sign in to comment.