|
| 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; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq.Expressions; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators |
| 23 | +{ |
| 24 | + internal static class NewKeyValuePairExpressionToAggregationExpressionTranslator |
| 25 | + { |
| 26 | + public static bool CanTranslate(NewExpression expression) |
| 27 | + => expression.Type.IsConstructedGenericType && expression.Type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>); |
| 28 | + |
| 29 | + public static TranslatedExpression Translate(TranslationContext context, NewExpression expression) |
| 30 | + { |
| 31 | + var arguments = expression.Arguments; |
| 32 | + var keyExpression = arguments[0]; |
| 33 | + var valueExpression = arguments[1]; |
| 34 | + return Translate(context, expression, keyExpression, valueExpression); |
| 35 | + } |
| 36 | + |
| 37 | + public static TranslatedExpression Translate( |
| 38 | + TranslationContext context, |
| 39 | + Expression expression, |
| 40 | + Expression keyExpression, |
| 41 | + Expression valueExpression) |
| 42 | + { |
| 43 | + var keyTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, keyExpression); |
| 44 | + var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); |
| 45 | + |
| 46 | + var ast = AstExpression.ComputedDocument([ |
| 47 | + AstExpression.ComputedField("Key", keyTranslation.Ast), |
| 48 | + AstExpression.ComputedField("Value", valueTranslation.Ast) |
| 49 | + ]); |
| 50 | + |
| 51 | + var serializer = CreateResultSerializer(expression.Type, keyTranslation.Serializer, valueTranslation.Serializer); |
| 52 | + |
| 53 | + return new TranslatedExpression(expression, ast, serializer); |
| 54 | + } |
| 55 | + |
| 56 | + private static IBsonSerializer CreateResultSerializer(Type resultType, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) |
| 57 | + { |
| 58 | + var constructorInfo = resultType.GetConstructor([keySerializer.ValueType, valueSerializer.ValueType]); |
| 59 | + var classMap = new BsonClassMap(resultType); |
| 60 | + classMap.MapConstructor(constructorInfo); |
| 61 | + classMap.AutoMap(); |
| 62 | + classMap.GetMemberMap("Key").SetSerializer(keySerializer); |
| 63 | + classMap.GetMemberMap("Value").SetSerializer(valueSerializer); |
| 64 | + classMap.Freeze(); |
| 65 | + |
| 66 | + // have to use BsonClassMapSerializer here to mimic the MemberInitExpressionToAggregationExpressionTranslator to avoid risking a behavioral breaking change |
| 67 | + var serializerType = typeof(BsonClassMapSerializer<>).MakeGenericType(resultType); |
| 68 | + return (IBsonSerializer)Activator.CreateInstance(serializerType, classMap); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments