-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-2509: Support Dictionary.ContainsValue in LINQ queries.
- Loading branch information
Showing
6 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...sionTranslators/MethodTranslators/ContainsValueMethodToAggregationExpressionTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* Copyright 2010-present MongoDB 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.Linq.Expressions; | ||
using System.Reflection; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Options; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators | ||
{ | ||
internal static class ContainsValueMethodToAggregationExpressionTranslator | ||
{ | ||
// public methods | ||
public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (IsContainsValueMethod(method)) | ||
{ | ||
var dictionaryExpression = expression.Object; | ||
var valueExpression = arguments[0]; | ||
|
||
var dictionaryTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, dictionaryExpression); | ||
var dictionarySerializer = GetDictionarySerializer(expression, dictionaryTranslation); | ||
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation; | ||
|
||
var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); | ||
var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", valueTranslation.Ast); | ||
|
||
AstExpression ast; | ||
switch (dictionaryRepresentation) | ||
{ | ||
case DictionaryRepresentation.Document: | ||
ast = AstExpression.Let( | ||
var: valueBinding, | ||
@in: AstExpression.Reduce( | ||
input: AstExpression.ObjectToArray(dictionaryTranslation.Ast), | ||
initialValue: false, | ||
@in: AstExpression.Cond( | ||
@if: AstExpression.Var("value"), | ||
@then: true, | ||
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst)))); | ||
break; | ||
|
||
case DictionaryRepresentation.ArrayOfArrays: | ||
ast = AstExpression.Let( | ||
var: valueBinding, | ||
@in: AstExpression.Reduce( | ||
input: dictionaryTranslation.Ast, | ||
initialValue: false, | ||
@in: AstExpression.Cond( | ||
@if: AstExpression.Var("value"), | ||
@then: true, | ||
@else: AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("this"), 1), valueAst)))); | ||
break; | ||
|
||
case DictionaryRepresentation.ArrayOfDocuments: | ||
ast = AstExpression.Let( | ||
var: valueBinding, | ||
@in: AstExpression.Reduce( | ||
input: dictionaryTranslation.Ast, | ||
initialValue: false, | ||
@in: AstExpression.Cond( | ||
@if: AstExpression.Var("value"), | ||
@then: true, | ||
@else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst)))); | ||
break; | ||
|
||
default: | ||
throw new ExpressionNotSupportedException(expression, because: $"ContainsValue is not supported when DictionaryRepresentation is: {dictionaryRepresentation}"); | ||
} | ||
|
||
return new AggregationExpression(expression, ast, BooleanSerializer.Instance); | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
|
||
private static IBsonDictionarySerializer GetDictionarySerializer(Expression expression, AggregationExpression dictionaryTranslation) | ||
{ | ||
if (dictionaryTranslation.Serializer is IBsonDictionarySerializer dictionarySerializer) | ||
{ | ||
return dictionarySerializer; | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression, because: $"class {dictionaryTranslation.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface"); | ||
} | ||
|
||
private static bool IsContainsValueMethod(MethodInfo method) | ||
{ | ||
return | ||
!method.IsStatic && | ||
method.IsPublic && | ||
method.ReturnType == typeof(bool) && | ||
method.Name == "ContainsValue" && | ||
method.GetParameters() is var parameters && | ||
parameters.Length == 1; | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
.../ExpressionToFilterTranslators/MethodTranslators/ContainsValueMethodToFilterTranslator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* Copyright 2010-present MongoDB 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.Linq.Expressions; | ||
using System.Reflection; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Options; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.MethodTranslators | ||
{ | ||
internal static class ContainsValueMethodToFilterTranslator | ||
{ | ||
public static AstFilter Translate(TranslationContext context, MethodCallExpression expression) | ||
{ | ||
var method = expression.Method; | ||
var arguments = expression.Arguments; | ||
|
||
if (IsContainsValueMethod(method)) | ||
{ | ||
var dictionaryExpression = expression.Object; | ||
var valueExpression = arguments[0]; | ||
|
||
var dictionaryField = ExpressionToFilterFieldTranslator.Translate(context, dictionaryExpression); | ||
var dictionarySerializer = GetDictionarySerializer(expression, dictionaryField); | ||
var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation; | ||
var valueSerializer = dictionarySerializer.ValueSerializer; | ||
|
||
if (valueExpression is ConstantExpression constantValueExpression) | ||
{ | ||
var valueField = AstFilter.Field("v", valueSerializer); | ||
var value = constantValueExpression.Value; | ||
var serializedValue = SerializationHelper.SerializeValue(valueSerializer, value); | ||
|
||
switch (dictionaryRepresentation) | ||
{ | ||
case DictionaryRepresentation.ArrayOfDocuments: | ||
return AstFilter.ElemMatch(dictionaryField, AstFilter.Eq(valueField, serializedValue)); | ||
|
||
default: | ||
throw new ExpressionNotSupportedException(expression, because: $"ContainsValue is not supported when DictionaryRepresentation is: {dictionaryRepresentation}"); | ||
} | ||
} | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression); | ||
} | ||
|
||
private static IBsonDictionarySerializer GetDictionarySerializer(Expression expression, AstFilterField field) | ||
{ | ||
if (field.Serializer is IBsonDictionarySerializer dictionarySerializer) | ||
{ | ||
return dictionarySerializer; | ||
} | ||
|
||
throw new ExpressionNotSupportedException(expression, because: $"class {field.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface"); | ||
} | ||
|
||
private static bool IsContainsValueMethod(MethodInfo method) | ||
{ | ||
return | ||
!method.IsStatic && | ||
method.IsPublic && | ||
method.ReturnType == typeof(bool) && | ||
method.Name == "ContainsValue" && | ||
method.GetParameters() is var parameters && | ||
parameters.Length == 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.