Skip to content

Commit

Permalink
Fix .Contains on an IList<T> and List<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenniak committed Apr 13, 2015
1 parent 0ead16c commit 1969700
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
42 changes: 42 additions & 0 deletions rethinkdb-net-test/Integration/MultiObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,5 +1031,47 @@ public void EnumerableContains()
}
Assert.That(count, Is.EqualTo(4));
}

[Test]
public void EnumerableClientSideArrayContains()
{
var array = new string[] { "2" };
var enumerable = connection.Run(testTable.Filter(o => array.Contains(o.Id)));
int count = 0;
foreach (var row in enumerable)
{
Assert.That(row.Id, Is.EqualTo("2"));
count += 1;
}
Assert.That(count, Is.EqualTo(1));
}

[Test]
public void EnumerableClientSideListContains()
{
var array = new List<string>() { "2" };
var enumerable = connection.Run(testTable.Filter(o => array.Contains(o.Id)));
int count = 0;
foreach (var row in enumerable)
{
Assert.That(row.Id, Is.EqualTo("2"));
count += 1;
}
Assert.That(count, Is.EqualTo(1));
}

[Test]
public void EnumerableClientSideIListContains()
{
IList<string> array = new List<string>() { "2" };
var enumerable = connection.Run(testTable.Filter(o => array.Contains(o.Id)));
int count = 0;
foreach (var row in enumerable)
{
Assert.That(row.Id, Is.EqualTo("2"));
count += 1;
}
Assert.That(count, Is.EqualTo(1));
}
}
}
32 changes: 28 additions & 4 deletions rethinkdb-net/Expressions/DefaultExpressionConverterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,35 @@ public void Reset()
memberAccessMappingRegistry.Clear();
}

public void RegisterMethodCallMapping(MethodInfo method, ExpressionMappingDelegate<MethodCallExpression> methodCallMapping)
private MethodInfo GetMostGenericVersionOfMethod(MethodInfo method)
{
if (method.IsGenericMethod)
if (method.DeclaringType.IsGenericType)
{
Type declaringType = method.DeclaringType;
Type genericTypeDefinition = declaringType.GetGenericTypeDefinition();
MethodInfo[] possibleGenericMethods =
genericTypeDefinition.GetMethods()
.Where(m => m.Name == method.Name)
.Where(m => m.GetParameters().Length == method.GetParameters().Length)
.ToArray();

if (possibleGenericMethods.Length == 0)
throw new InvalidOperationException("Failed to find any generic version of method");
else if (possibleGenericMethods.Length > 1)
throw new InvalidOperationException("Ambiguous method found in GetMostGenericVersionOfMethod");

method = possibleGenericMethods[0];
}
else if (method.IsGenericMethod)
{
method = method.GetGenericMethodDefinition();
}
return method;
}

public void RegisterMethodCallMapping(MethodInfo method, ExpressionMappingDelegate<MethodCallExpression> methodCallMapping)
{
method = GetMostGenericVersionOfMethod(method);
methodCallMappingRegistry[method] = methodCallMapping;
}

Expand Down Expand Up @@ -81,8 +106,7 @@ public void RegisterMemberAccessMapping(Type targetType, string memberName, Expr

public bool TryGetMethodCallMapping(MethodInfo method, out ExpressionMappingDelegate<MethodCallExpression> methodCallMapping)
{
if (method.IsGenericMethod)
method = method.GetGenericMethodDefinition();
method = GetMostGenericVersionOfMethod(method);
return methodCallMappingRegistry.TryGetValue(method, out methodCallMapping);
}

Expand Down
16 changes: 16 additions & 0 deletions rethinkdb-net/Expressions/LinqExpressionConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ public static void RegisterOnConverterFactory(DefaultExpressionConverterFactory
args = { list, arg }
});

expressionConverterFactory.RegisterTemplateMapping<IList<int>, int, bool>(
(list, arg) => list.Contains(arg),
(list, arg) => new Term()
{
type = Term.TermType.CONTAINS,
args = { list, arg }
});

expressionConverterFactory.RegisterTemplateMapping<List<int>, int, bool>(
(list, arg) => list.Contains(arg),
(list, arg) => new Term()
{
type = Term.TermType.CONTAINS,
args = { list, arg }
});

expressionConverterFactory.RegisterTemplateMapping<IEnumerable<int>, int>(
list => list.Count(),
list => Count(list)
Expand Down

0 comments on commit 1969700

Please sign in to comment.