|
| 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 MongoDB.Bson; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Bson.Serialization.Options; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
| 24 | +using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionTranslators |
| 27 | +{ |
| 28 | + internal static class DictionaryIndexerComparisonExpressionToFilterTranslator |
| 29 | + { |
| 30 | + public static bool CanTranslate(Expression leftExpression, Expression rightExpression) |
| 31 | + { |
| 32 | + return leftExpression is MethodCallExpression methodCallExpression && |
| 33 | + rightExpression is ConstantExpression && |
| 34 | + DictionaryMethod.IsGetItemWithKeyMethod(methodCallExpression.Method); |
| 35 | + } |
| 36 | + |
| 37 | + public static AstFilter Translate(TranslationContext context, Expression containingExpression, AstComparisonFilterOperator comparisonOperator, MethodCallExpression indexerExpression, ConstantExpression valueExpression) |
| 38 | + { |
| 39 | + var dictionaryExpression = indexerExpression.Object; |
| 40 | + var keyExpression = indexerExpression.Arguments[0]; |
| 41 | + |
| 42 | + var fieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, dictionaryExpression); |
| 43 | + |
| 44 | + if (fieldTranslation.Serializer is not IBsonDictionarySerializer dictionarySerializer) |
| 45 | + { |
| 46 | + throw new ExpressionNotSupportedException(containingExpression, because: $"class {fieldTranslation.Serializer.GetType().FullName} does not implement the IBsonDictionarySerializer interface"); |
| 47 | + } |
| 48 | + |
| 49 | + var dictionaryRepresentation = dictionarySerializer.DictionaryRepresentation; |
| 50 | + var key = GetKeyStringConstant(containingExpression, keyExpression, dictionarySerializer.KeySerializer); |
| 51 | + var serializedValue = SerializationHelper.SerializeValue(dictionarySerializer.ValueSerializer, valueExpression, containingExpression); |
| 52 | + |
| 53 | + switch (dictionaryRepresentation) |
| 54 | + { |
| 55 | + case DictionaryRepresentation.Document: |
| 56 | + { |
| 57 | + var subField = fieldTranslation.SubField(key, dictionarySerializer.ValueSerializer); |
| 58 | + return AstFilter.Compare(subField.Ast, comparisonOperator, serializedValue); |
| 59 | + } |
| 60 | + |
| 61 | + case DictionaryRepresentation.ArrayOfArrays: |
| 62 | + case DictionaryRepresentation.ArrayOfDocuments: |
| 63 | + { |
| 64 | + var keyFieldName = dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays ? "0" : "k"; |
| 65 | + var valueFieldName = dictionaryRepresentation == DictionaryRepresentation.ArrayOfArrays ? "1" : "v"; |
| 66 | + |
| 67 | + var keyField = AstFilter.Field(keyFieldName); |
| 68 | + var valueField = AstFilter.Field(valueFieldName); |
| 69 | + var keyMatchFilter = AstFilter.Eq(keyField, key); |
| 70 | + var valueMatchFilter = AstFilter.Compare(valueField, comparisonOperator, serializedValue); |
| 71 | + var combinedFilter = AstFilter.And(keyMatchFilter, valueMatchFilter); |
| 72 | + |
| 73 | + return AstFilter.ElemMatch(fieldTranslation.Ast, combinedFilter); |
| 74 | + } |
| 75 | + |
| 76 | + default: |
| 77 | + throw new ExpressionNotSupportedException(containingExpression, because: $"Unexpected dictionary representation: {dictionaryRepresentation}"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static string GetKeyStringConstant(Expression expression, Expression keyExpression, IBsonSerializer keySerializer) |
| 82 | + { |
| 83 | + var key = keyExpression.GetConstantValue<object>(containingExpression: expression); |
| 84 | + var serializedKey = SerializationHelper.SerializeValue(keySerializer, key); |
| 85 | + if (serializedKey is not BsonString) |
| 86 | + { |
| 87 | + throw new ExpressionNotSupportedException(expression, because: "key did not serialize as a string"); |
| 88 | + } |
| 89 | + |
| 90 | + return serializedKey.AsString; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments