|
| 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.Conventions; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; |
| 24 | + |
| 25 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators |
| 26 | +{ |
| 27 | + internal static class OfTypeMethodToAggregationExpressionTranslator |
| 28 | + { |
| 29 | + private static readonly MethodInfo[] __ofTypeMethods = |
| 30 | + { |
| 31 | + EnumerableMethod.OfType, |
| 32 | + QueryableMethod.OfType |
| 33 | + }; |
| 34 | + |
| 35 | + public static AggregationExpression Translate(TranslationContext context, MethodCallExpression expression) |
| 36 | + { |
| 37 | + var method = expression.Method; |
| 38 | + var arguments = expression.Arguments; |
| 39 | + |
| 40 | + if (method.IsOneOf(__ofTypeMethods)) |
| 41 | + { |
| 42 | + var sourceExpression = arguments[0]; |
| 43 | + var sourceTranslation = ExpressionToAggregationExpressionTranslator.TranslateEnumerable(context, sourceExpression); |
| 44 | + NestedAsQueryableHelper.EnsureQueryableMethodHasNestedAsQueryableSource(expression, sourceTranslation); |
| 45 | + var itemSerializer = ArraySerializerHelper.GetItemSerializer(sourceTranslation.Serializer); |
| 46 | + |
| 47 | + var nominalType = sourceTranslation.Serializer.ValueType; |
| 48 | + var actualType = method.GetGenericArguments()[0]; |
| 49 | + |
| 50 | + if (nominalType == actualType) |
| 51 | + { |
| 52 | + return sourceTranslation; |
| 53 | + } |
| 54 | + |
| 55 | + var discriminatorConvention = itemSerializer.GetDiscriminatorConvention(); |
| 56 | + var discriminatorElementName = discriminatorConvention.ElementName; |
| 57 | + |
| 58 | + var itemVar = AstExpression.Var("this"); |
| 59 | + var discriminatorField = AstExpression.GetField(itemVar, discriminatorElementName); |
| 60 | + var ofTypePredicate = discriminatorConvention switch |
| 61 | + { |
| 62 | + IHierarchicalDiscriminatorConvention hierarchicalDiscriminatorConvention => DiscriminatorAstExpression.TypeIs(discriminatorField, hierarchicalDiscriminatorConvention, nominalType, actualType), |
| 63 | + IScalarDiscriminatorConvention scalarDiscriminatorConvention => DiscriminatorAstExpression.TypeIs(discriminatorField, scalarDiscriminatorConvention, nominalType, actualType), |
| 64 | + _ => throw new ExpressionNotSupportedException(expression, because: "is operator is not supported with the configured discriminator convention") |
| 65 | + }; |
| 66 | + |
| 67 | + var ast = AstExpression.Filter( |
| 68 | + input: sourceTranslation.Ast, |
| 69 | + @as: itemVar.Name, |
| 70 | + cond: ofTypePredicate); |
| 71 | + var actualTypeSerializer = BsonSerializer.LookupSerializer(actualType); |
| 72 | + var resultSerializer = NestedAsQueryableSerializer.CreateIEnumerableOrNestedAsQueryableSerializer(expression.Type, actualTypeSerializer); |
| 73 | + |
| 74 | + return new AggregationExpression(expression, ast, resultSerializer); |
| 75 | + } |
| 76 | + |
| 77 | + throw new ExpressionNotSupportedException(expression); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments