|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Linq.Expressions; |
| 17 | +using System.Reflection; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Bson.Serialization.Options; |
| 20 | +using MongoDB.Bson.Serialization.Serializers; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators |
| 24 | +{ |
| 25 | + internal static class ContainsValueMethodToAggregationExpressionTranslator |
| 26 | + { |
| 27 | + // public methods |
| 28 | + public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) |
| 29 | + { |
| 30 | + var method = expression.Method; |
| 31 | + var arguments = expression.Arguments; |
| 32 | + |
| 33 | + if (IsContainsValueMethod(method)) |
| 34 | + { |
| 35 | + var dictionaryExpression = expression.Object; |
| 36 | + var valueExpression = arguments[0]; |
| 37 | + |
| 38 | + var dictionaryTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, dictionaryExpression); |
| 39 | + var dictionarySerializer = GetDictionarySerializer(expression, dictionaryTranslation); |
| 40 | + var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation; |
| 41 | + |
| 42 | + var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); |
| 43 | + var (valueBinding, valueAst) = AstExpression.UseVarIfNotSimple("value", valueTranslation.Ast); |
| 44 | + |
| 45 | + AstExpression ast; |
| 46 | + switch (dictionaryRepresentation) |
| 47 | + { |
| 48 | + case DictionaryRepresentation.Document: |
| 49 | + ast = AstExpression.Let( |
| 50 | + var: valueBinding, |
| 51 | + @in: AstExpression.Reduce( |
| 52 | + input: AstExpression.ObjectToArray(dictionaryTranslation.Ast), |
| 53 | + initialValue: false, |
| 54 | + @in: AstExpression.Cond( |
| 55 | + @if: AstExpression.Var("value"), |
| 56 | + @then: true, |
| 57 | + @else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst)))); |
| 58 | + break; |
| 59 | + |
| 60 | + case DictionaryRepresentation.ArrayOfArrays: |
| 61 | + ast = AstExpression.Let( |
| 62 | + var: valueBinding, |
| 63 | + @in: AstExpression.Reduce( |
| 64 | + input: dictionaryTranslation.Ast, |
| 65 | + initialValue: false, |
| 66 | + @in: AstExpression.Cond( |
| 67 | + @if: AstExpression.Var("value"), |
| 68 | + @then: true, |
| 69 | + @else: AstExpression.Eq(AstExpression.ArrayElemAt(AstExpression.Var("this"), 1), valueAst)))); |
| 70 | + break; |
| 71 | + |
| 72 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 73 | + ast = AstExpression.Let( |
| 74 | + var: valueBinding, |
| 75 | + @in: AstExpression.Reduce( |
| 76 | + input: dictionaryTranslation.Ast, |
| 77 | + initialValue: false, |
| 78 | + @in: AstExpression.Cond( |
| 79 | + @if: AstExpression.Var("value"), |
| 80 | + @then: true, |
| 81 | + @else: AstExpression.Eq(AstExpression.GetField(AstExpression.Var("this"), "v"), valueAst)))); |
| 82 | + break; |
| 83 | + |
| 84 | + default: |
| 85 | + throw new ExpressionNotSupportedException(expression, because: $"ContainsValue is not supported when DictionaryRepresentation is: {dictionaryRepresentation}"); |
| 86 | + } |
| 87 | + |
| 88 | + return new AggregationExpression(expression, ast, BooleanSerializer.Instance); |
| 89 | + } |
| 90 | + |
| 91 | + throw new ExpressionNotSupportedException(expression); |
| 92 | + } |
| 93 | + |
| 94 | + private static IBsonDictionarySerializer GetDictionarySerializer(Expression expression, AggregationExpression dictionaryTranslation) |
| 95 | + { |
| 96 | + if (dictionaryTranslation.Serializer is IBsonDictionarySerializer dictionarySerializer) |
| 97 | + { |
| 98 | + return dictionarySerializer; |
| 99 | + } |
| 100 | + |
| 101 | + throw new ExpressionNotSupportedException(expression, because: $"class {dictionaryTranslation.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface"); |
| 102 | + } |
| 103 | + |
| 104 | + private static bool IsContainsValueMethod(MethodInfo method) |
| 105 | + { |
| 106 | + return |
| 107 | + !method.IsStatic && |
| 108 | + method.IsPublic && |
| 109 | + method.ReturnType == typeof(bool) && |
| 110 | + method.Name == "ContainsValue" && |
| 111 | + method.GetParameters() is var parameters && |
| 112 | + parameters.Length == 1; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments