|
| 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.Collections.Generic; |
| 17 | +using System.Linq.Expressions; |
| 18 | +using System.Reflection; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 24 | +using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToSetStageTranslators |
| 27 | +{ |
| 28 | + internal static class ExpressionToSetStageTranslator |
| 29 | + { |
| 30 | + public static AstStage Translate(TranslationContext context, IBsonSerializer inputSerializer, LambdaExpression expression) |
| 31 | + { |
| 32 | + if (inputSerializer is not IBsonDocumentSerializer documentSerializer) |
| 33 | + { |
| 34 | + throw new ExpressionNotSupportedException(expression, because: $"serializer {inputSerializer.GetType()} does not implement IBsonDocumentSerializer"); |
| 35 | + } |
| 36 | + |
| 37 | + if (IsNewAnonymousClass(expression, out var newExpression)) |
| 38 | + { |
| 39 | + return TranslateNewAnonymousClass(context, documentSerializer, newExpression); |
| 40 | + } |
| 41 | + |
| 42 | + if (IsNewWithOptionalMemberInitializers(expression, out var memberInitExpression)) |
| 43 | + { |
| 44 | + return TranslateNewWithOptionalMemberInitializers(context, documentSerializer, memberInitExpression); |
| 45 | + } |
| 46 | + |
| 47 | + throw new ExpressionNotSupportedException(expression, because: "expression is not valid for Set"); |
| 48 | + } |
| 49 | + |
| 50 | + private static bool IsNewAnonymousClass(LambdaExpression expression, out NewExpression newExpression) |
| 51 | + { |
| 52 | + if (expression.Body is NewExpression tempNewExpression && |
| 53 | + tempNewExpression.Type.IsAnonymous()) |
| 54 | + { |
| 55 | + newExpression = tempNewExpression; |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + newExpression = null; |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + private static bool IsNewWithOptionalMemberInitializers(LambdaExpression expression, out MemberInitExpression memberInitExpression) |
| 64 | + { |
| 65 | + if (expression.Body.NodeType == ExpressionType.New) |
| 66 | + { |
| 67 | + memberInitExpression = null; |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + if (expression.Body is MemberInitExpression tempMemberInitExpression) |
| 72 | + { |
| 73 | + var constructor = tempMemberInitExpression.NewExpression.Constructor; // will be null for default constructor of struct |
| 74 | + if (constructor == null || IsDefaultConstructor(constructor) || IsCopyConstructor(constructor)) |
| 75 | + { |
| 76 | + memberInitExpression = tempMemberInitExpression; |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + memberInitExpression = null; |
| 82 | + return false; |
| 83 | + |
| 84 | + static bool IsDefaultConstructor(ConstructorInfo constructor) |
| 85 | + => constructor.GetParameters().Length == 0; |
| 86 | + |
| 87 | + static bool IsCopyConstructor(ConstructorInfo constructor) |
| 88 | + => |
| 89 | + constructor.GetParameters() is var parameters && |
| 90 | + parameters.Length == 1 && |
| 91 | + parameters[0].ParameterType == constructor.DeclaringType; |
| 92 | + } |
| 93 | + |
| 94 | + private static AstStage TranslateNewAnonymousClass(TranslationContext context, IBsonDocumentSerializer documentSerializer, NewExpression newExpression) |
| 95 | + { |
| 96 | + var members = newExpression.Members; // will be null in the case of "new { }" |
| 97 | + var arguments = newExpression.Arguments; |
| 98 | + |
| 99 | + var fields = new List<AstComputedField>(); |
| 100 | + if (members != null) |
| 101 | + { |
| 102 | + for (var i = 0; i < members.Count; i++) |
| 103 | + { |
| 104 | + var member = members[i]; |
| 105 | + var valueExpression = PartialEvaluator.EvaluatePartially(arguments[i]); |
| 106 | + var computedField = CreateComputedField(context, documentSerializer, member, valueExpression); |
| 107 | + fields.Add(computedField); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return AstStage.Set(fields); |
| 112 | + } |
| 113 | + |
| 114 | + private static AstStage TranslateNewWithOptionalMemberInitializers(TranslationContext context, IBsonDocumentSerializer documentSerializer, MemberInitExpression memberInitExpression) |
| 115 | + { |
| 116 | + var fields = new List<AstComputedField>(); |
| 117 | + if (memberInitExpression != null) |
| 118 | + { |
| 119 | + var bindings = memberInitExpression.Bindings; |
| 120 | + |
| 121 | + for (var i = 0; i < bindings.Count; i++) |
| 122 | + { |
| 123 | + var binding = bindings[i]; |
| 124 | + if (binding is not MemberAssignment assignment) |
| 125 | + { |
| 126 | + throw new ExpressionNotSupportedException(memberInitExpression, because: $"the member initializer for {binding.Member.Name} is not a simple assignment"); |
| 127 | + } |
| 128 | + |
| 129 | + var member = binding.Member; |
| 130 | + var valueExpression = PartialEvaluator.EvaluatePartially(assignment.Expression); |
| 131 | + var computedField = CreateComputedField(context, documentSerializer, member, valueExpression); |
| 132 | + fields.Add(computedField); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return AstStage.Set(fields); |
| 137 | + } |
| 138 | + |
| 139 | + private static AstComputedField CreateComputedField(TranslationContext context, IBsonDocumentSerializer documentSerializer, MemberInfo member, Expression valueExpression) |
| 140 | + { |
| 141 | + string elementName; |
| 142 | + AstExpression valueAst; |
| 143 | + if (documentSerializer.TryGetMemberSerializationInfo(member.Name, out var serializationInfo)) |
| 144 | + { |
| 145 | + elementName = serializationInfo.ElementName; |
| 146 | + var memberSerializer = serializationInfo.Serializer; |
| 147 | + |
| 148 | + if (valueExpression is ConstantExpression constantValueExpression) |
| 149 | + { |
| 150 | + var value = constantValueExpression.Value; |
| 151 | + var serializedValue = SerializationHelper.SerializeValue(memberSerializer, value); |
| 152 | + valueAst = AstExpression.Constant(serializedValue); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); |
| 157 | + ThrowIfMemberAndValueSerializersAreNotCompatible(valueExpression, memberSerializer, valueTranslation.Serializer); |
| 158 | + valueAst = valueTranslation.Ast; |
| 159 | + } |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + elementName = member.Name; |
| 164 | + var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression); |
| 165 | + valueAst = valueTranslation.Ast; |
| 166 | + } |
| 167 | + |
| 168 | + return AstExpression.ComputedField(elementName, valueAst); |
| 169 | + } |
| 170 | + |
| 171 | + private static void ThrowIfMemberAndValueSerializersAreNotCompatible(Expression expression, IBsonSerializer memberSerializer, IBsonSerializer valueSerializer) |
| 172 | + { |
| 173 | + // TODO: depends on CSHARP-3315 |
| 174 | + if (!memberSerializer.Equals(valueSerializer)) |
| 175 | + { |
| 176 | + throw new ExpressionNotSupportedException(expression, because: "member and value serializers are not compatible"); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments