Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message on impossible cast #278

Merged
merged 2 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}