Skip to content

Commit 1d51d7b

Browse files
committed
CSHARP-5375: Remove KnownSerializerRegistry.
1 parent 2349b02 commit 1d51d7b

File tree

43 files changed

+717
-1032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+717
-1032
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Optimizers/AstSimplifier.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ public static TNode SimplifyAndConvert<TNode>(TNode node)
3636
}
3737
#endregion
3838

39+
public override AstNode VisitBinaryExpression(AstBinaryExpression node)
40+
{
41+
var arg1 = VisitAndConvert(node.Arg1);
42+
var arg2 = VisitAndConvert(node.Arg2);
43+
44+
if (node.Operator == AstBinaryOperator.IfNull)
45+
{
46+
if (arg1 is AstConstantExpression arg1ConstantExpression)
47+
{
48+
// { $ifNull : [expr1, expr2] } => expr2 when expr1 == null
49+
// { $ifNull : [expr1, expr2] } => expr1 when expr1 != null
50+
return arg1ConstantExpression.Value == BsonNull.Value ? arg2 : arg1;
51+
}
52+
53+
if (arg2 is AstConstantExpression arg2ConstantExpression &&
54+
arg2ConstantExpression.Value == BsonNull.Value)
55+
{
56+
// { $ifNull : [expr1, expr2] } => expr1 when expr2 == null
57+
return arg1;
58+
}
59+
}
60+
61+
return node.Update(arg1, arg2);
62+
}
63+
3964
public override AstNode VisitCondExpression(AstCondExpression node)
4065
{
4166
// { $cond : [{ $eq : [expr1, null] }, null, expr2] }

src/MongoDB.Driver/Linq/Linq3Implementation/GroupingWithOutputExpressionStageDefinitions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private AstStage RenderProjectStage(
6464
out IBsonSerializer<TOutput> outputSerializer)
6565
{
6666
var partiallyEvaluatedOutput = (Expression<Func<TGrouping, TOutput>>)PartialEvaluator.EvaluatePartially(_output);
67-
var context = TranslationContext.Create(partiallyEvaluatedOutput, inputSerializer, translationOptions);
67+
var context = TranslationContext.Create(partiallyEvaluatedOutput, translationOptions);
6868
var outputTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, partiallyEvaluatedOutput, inputSerializer, asRoot: true);
6969
var (projectStage, projectSerializer) = ProjectionHelper.CreateProjectStage(outputTranslation);
7070
outputSerializer = (IBsonSerializer<TOutput>)projectSerializer;
@@ -106,7 +106,7 @@ protected override AstStage RenderGroupingStage(
106106
out IBsonSerializer<IGrouping<TValue, TInput>> groupingOutputSerializer)
107107
{
108108
var partiallyEvaluatedGroupBy = (Expression<Func<TInput, TValue>>)PartialEvaluator.EvaluatePartially(_groupBy);
109-
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, inputSerializer, translationOptions);
109+
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, translationOptions);
110110
var groupByTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, partiallyEvaluatedGroupBy, inputSerializer, asRoot: true);
111111

112112
var valueSerializer = (IBsonSerializer<TValue>)groupByTranslation.Serializer;
@@ -150,7 +150,7 @@ protected override AstStage RenderGroupingStage(
150150
out IBsonSerializer<IGrouping<AggregateBucketAutoResultId<TValue>, TInput>> groupingOutputSerializer)
151151
{
152152
var partiallyEvaluatedGroupBy = (Expression<Func<TInput, TValue>>)PartialEvaluator.EvaluatePartially(_groupBy);
153-
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, inputSerializer, translationOptions);
153+
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, translationOptions);
154154
var groupByTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, partiallyEvaluatedGroupBy, inputSerializer, asRoot: true);
155155

156156
var valueSerializer = (IBsonSerializer<TValue>)groupByTranslation.Serializer;
@@ -188,7 +188,7 @@ protected override AstStage RenderGroupingStage(
188188
out IBsonSerializer<IGrouping<TValue, TInput>> groupingOutputSerializer)
189189
{
190190
var partiallyEvaluatedGroupBy = (Expression<Func<TInput, TValue>>)PartialEvaluator.EvaluatePartially(_groupBy);
191-
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, inputSerializer, translationOptions);
191+
var context = TranslationContext.Create(partiallyEvaluatedGroupBy, translationOptions);
192192
var groupByTranslation = ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(context, partiallyEvaluatedGroupBy, inputSerializer, asRoot: true);
193193
var pushElements = AstExpression.AccumulatorField("_elements", AstUnaryAccumulatorOperator.Push, AstExpression.Var("ROOT", isCurrent: true));
194194
var groupBySerializer = (IBsonSerializer<TValue>)groupByTranslation.Serializer;

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/ArraySerializerHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
using System;
1717
using System.Linq.Expressions;
1818
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Serializers;
1920

2021
namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
2122
{
2223
internal static class ArraySerializerHelper
2324
{
25+
public static IBsonSerializer CreateSerializer(IBsonSerializer itemSerializer)
26+
{
27+
var arraySerializerType = typeof(ArraySerializer<>).MakeGenericType(itemSerializer.ValueType);
28+
return (IBsonSerializer)Activator.CreateInstance(arraySerializerType, [itemSerializer]);
29+
}
30+
2431
public static IBsonSerializer GetItemSerializer(IBsonSerializer serializer)
2532
{
2633
if (serializer is IBsonArraySerializer arraySerializer)

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/ProjectionHelper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using MongoDB.Bson.Serialization;
2020
using MongoDB.Driver.Linq.Linq3Implementation.Ast;
2121
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
22+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Optimizers;
2223
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages;
2324
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
2425
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators;
@@ -138,17 +139,16 @@ private static bool IsGetFieldChainWithSafeFieldNames(AstGetFieldExpression getF
138139

139140
private static AstExpression QuoteIfNecessary(AstExpression expression)
140141
{
141-
if (expression is AstConstantExpression constantExpression)
142+
var simplifiedExpression = AstSimplifier.SimplifyAndConvert(expression);
143+
if (simplifiedExpression is AstConstantExpression constantExpression &&
144+
ValueNeedsToBeQuoted(constantExpression.Value))
142145
{
143-
if (ValueNeedsToBeQuoted(constantExpression.Value))
144-
{
145-
return AstExpression.Literal(constantExpression);
146-
}
146+
return AstExpression.Literal(constantExpression);
147147
}
148148

149-
return expression;
149+
return expression; // not the simplified expression
150150

151-
bool ValueNeedsToBeQuoted(BsonValue value)
151+
static bool ValueNeedsToBeQuoted(BsonValue value)
152152
{
153153
switch (value.BsonType)
154154
{

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/SerializationHelper.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
2727
{
2828
internal static class SerializationHelper
2929
{
30+
public static void EnsureArgumentSerializersAreEqual(Expression expression, AggregationExpression firstArgumentTranslation, AggregationExpression secondArgumentTranslation)
31+
{
32+
EnsureArgumentSerializersAreEqual(expression, firstArgumentTranslation.Serializer, secondArgumentTranslation.Serializer);
33+
}
34+
35+
public static void EnsureArgumentSerializersAreEqual(Expression expression, IBsonSerializer firstSerializer, IBsonSerializer secondSerializer)
36+
{
37+
if (!firstSerializer.Equals(secondSerializer))
38+
{
39+
throw new ExpressionNotSupportedException(expression, because: $"the two arguments are serialized differently");
40+
}
41+
}
42+
3043
public static void EnsureRepresentationIsArray(Expression expression, IBsonSerializer serializer)
3144
{
3245
var representation = GetRepresentation(serializer);
@@ -36,6 +49,20 @@ public static void EnsureRepresentationIsArray(Expression expression, IBsonSeria
3649
}
3750
}
3851

52+
public static void EnsureRepresentationIsBoolean(Expression expression, Expression argumentExpression, AggregationExpression argumentTranslation)
53+
{
54+
EnsureRepresentationIsBoolean(expression, argumentExpression, argumentTranslation.Serializer);
55+
}
56+
57+
public static void EnsureRepresentationIsBoolean(Expression expression, Expression argumentExpression, IBsonSerializer argumentSerializer)
58+
{
59+
var argumentRepresentation = GetRepresentation(argumentSerializer);
60+
if (argumentRepresentation != BsonType.Boolean)
61+
{
62+
throw new ExpressionNotSupportedException(expression, because: $"{argumentExpression} uses a non-boolean representation: {argumentRepresentation}");
63+
}
64+
}
65+
3966
public static void EnsureRepresentationIsNumeric(Expression expression, Expression argumentExpression, AggregationExpression argumentTranslation)
4067
{
4168
EnsureRepresentationIsNumeric(expression, argumentExpression, argumentTranslation.Serializer);

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/TypeExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,18 @@ public static bool IsAnonymous(this Type type)
163163
type.Name.Contains("Anon"); // don't check for more than "Anon" so it works in mono also
164164
}
165165

166+
public static bool IsArray(this Type type, out Type itemType)
167+
{
168+
if (type.IsArray)
169+
{
170+
itemType = type.GetElementType();
171+
return true;
172+
}
173+
174+
itemType = null;
175+
return false;
176+
}
177+
166178
public static bool IsEnum(this Type type, out Type underlyingType)
167179
{
168180
if (type.IsEnum)

src/MongoDB.Driver/Linq/Linq3Implementation/Reflection/MqlMethod.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515

1616
using System;
1717
using System.Reflection;
18+
using MongoDB.Bson;
1819
using MongoDB.Bson.Serialization;
1920

2021
namespace MongoDB.Driver.Linq.Linq3Implementation.Reflection
2122
{
2223
internal static class MqlMethod
2324
{
2425
// private static fields
26+
private static readonly MethodInfo __constantWithRepresentation;
27+
private static readonly MethodInfo __constantWithSerializer;
2528
private static readonly MethodInfo __dateFromString;
2629
private static readonly MethodInfo __dateFromStringWithFormat;
2730
private static readonly MethodInfo __dateFromStringWithFormatAndTimezone;
@@ -34,6 +37,8 @@ internal static class MqlMethod
3437
// static constructor
3538
static MqlMethod()
3639
{
40+
__constantWithRepresentation = ReflectionInfo.Method((object value, BsonType representation) => Mql.Constant(value, representation));
41+
__constantWithSerializer = ReflectionInfo.Method((object value, IBsonSerializer<object> serializer) => Mql.Constant(value, serializer));
3742
__dateFromString = ReflectionInfo.Method((string dateStringl) => Mql.DateFromString(dateStringl));
3843
__dateFromStringWithFormat = ReflectionInfo.Method((string dateString, string format) => Mql.DateFromString(dateString, format));
3944
__dateFromStringWithFormatAndTimezone = ReflectionInfo.Method((string dateString, string format, string timezone) => Mql.DateFromString(dateString, format, timezone));
@@ -45,6 +50,8 @@ static MqlMethod()
4550
}
4651

4752
// public properties
53+
public static MethodInfo ConstantWithRepresentation => __constantWithRepresentation;
54+
public static MethodInfo ConstantWithSerializer => __constantWithSerializer;
4855
public static MethodInfo DateFromString => __dateFromString;
4956
public static MethodInfo DateFromStringWithFormat => __dateFromStringWithFormat;
5057
public static MethodInfo DateFromStringWithFormatAndTimezone => __dateFromStringWithFormatAndTimezone;

0 commit comments

Comments
 (0)