Skip to content

Commit 8970838

Browse files
committed
Fix issues caused by rebasing on main.
1 parent 9a6b84e commit 8970838

File tree

6 files changed

+122
-67
lines changed

6 files changed

+122
-67
lines changed

src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,12 @@ namespace MongoDB.Bson.Serialization.Serializers
2121
/// <summary>
2222
/// An interface implemented by KeyValuePairSerializer.
2323
/// </summary>
24-
public interface IKeyValuePairSerializer : IBsonSerializer
24+
public interface IKeyValuePairSerializer
2525
{
26-
/// <summary>
27-
/// Gets the key element name.
28-
/// </summary>
29-
string KeyElementName { get; }
30-
31-
/// <summary>
32-
/// Gets the key serializer.
33-
/// </summary>
34-
IBsonSerializer KeySerializer { get; }
35-
3626
/// <summary>
3727
/// Gets the representation.
3828
/// </summary>
3929
BsonType Representation { get; }
40-
41-
/// <summary>
42-
/// Gets the value element name.
43-
/// </summary>
44-
string ValueElementName { get; }
45-
46-
/// <summary>
47-
/// Gets the value serializer.
48-
/// </summary>
49-
IBsonSerializer ValueSerializer { get; }
5030
}
5131

5232
/// <summary>
@@ -61,15 +41,15 @@ public static class KeyValuePairSerializer
6141
/// <param name="keySerializer">The key serializer.</param>
6242
/// <param name="valueSerializer">The value Serializer.</param>
6343
/// <returns>A KeyValuePairSerializer.</returns>
64-
public static IKeyValuePairSerializer Create(
44+
public static IBsonSerializer Create(
6545
BsonType representation,
6646
IBsonSerializer keySerializer,
6747
IBsonSerializer valueSerializer)
6848
{
6949
var keyType = keySerializer.ValueType;
7050
var valueType = valueSerializer.ValueType;
7151
var keyValuePairSerializerType = typeof(KeyValuePairSerializer<,>).MakeGenericType(keyType, valueType);
72-
return (IKeyValuePairSerializer)Activator.CreateInstance(keyValuePairSerializerType, [representation, keySerializer, valueSerializer]);
52+
return (IBsonSerializer)Activator.CreateInstance(keyValuePairSerializerType, [representation, keySerializer, valueSerializer]);
7353
}
7454
}
7555

@@ -172,15 +152,12 @@ private KeyValuePairSerializer(BsonType representation, Lazy<IBsonSerializer<TKe
172152

173153
_helper = new SerializerHelper
174154
(
175-
new SerializerHelper.Member(KeyElementName, Flags.Key),
176-
new SerializerHelper.Member(ValueElementName, Flags.Value)
155+
new SerializerHelper.Member("k", Flags.Key),
156+
new SerializerHelper.Member("v", Flags.Value)
177157
);
178158
}
179159

180160
// public properties
181-
/// <inheritdoc/>
182-
public string KeyElementName => "k"; // might be configurable some day
183-
184161
/// <summary>
185162
/// Gets the key serializer.
186163
/// </summary>
@@ -192,17 +169,17 @@ public IBsonSerializer<TKey> KeySerializer
192169
get { return _lazyKeySerializer.Value; }
193170
}
194171

195-
IBsonSerializer IKeyValuePairSerializer.KeySerializer => KeySerializer;
196-
197-
/// <inheritdoc/>
172+
/// <summary>
173+
/// Gets the representation.
174+
/// </summary>
175+
/// <value>
176+
/// The representation.
177+
/// </value>
198178
public BsonType Representation
199179
{
200180
get { return _representation; }
201181
}
202182

203-
/// <inheritdoc/>
204-
public string ValueElementName => "v"; // might be configurable some day
205-
206183
/// <summary>
207184
/// Gets the value serializer.
208185
/// </summary>
@@ -214,8 +191,6 @@ public IBsonSerializer<TValue> ValueSerializer
214191
get { return _lazyValueSerializer.Value; }
215192
}
216193

217-
IBsonSerializer IKeyValuePairSerializer.ValueSerializer => ValueSerializer;
218-
219194
// public methods
220195
/// <summary>
221196
/// Deserializes a value.
@@ -293,10 +268,10 @@ public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializati
293268
switch (memberName)
294269
{
295270
case "Key":
296-
serializationInfo = new BsonSerializationInfo(KeyElementName, _lazyKeySerializer.Value, _lazyKeySerializer.Value.ValueType);
271+
serializationInfo = new BsonSerializationInfo("k", _lazyKeySerializer.Value, _lazyKeySerializer.Value.ValueType);
297272
return true;
298273
case "Value":
299-
serializationInfo = new BsonSerializationInfo(ValueElementName, _lazyValueSerializer.Value, _lazyValueSerializer.Value.ValueType);
274+
serializationInfo = new BsonSerializationInfo("v", _lazyValueSerializer.Value, _lazyValueSerializer.Value.ValueType);
300275
return true;
301276
}
302277

@@ -343,9 +318,9 @@ private void SerializeDocumentRepresentation(BsonSerializationContext context, K
343318
{
344319
var bsonWriter = context.Writer;
345320
bsonWriter.WriteStartDocument();
346-
bsonWriter.WriteName(KeyElementName);
321+
bsonWriter.WriteName("k");
347322
_lazyKeySerializer.Value.Serialize(context, value.Key);
348-
bsonWriter.WriteName(ValueElementName);
323+
bsonWriter.WriteName("v");
349324
_lazyValueSerializer.Value.Serialize(context, value.Value);
350325
bsonWriter.WriteEndDocument();
351326
}

src/MongoDB.Driver/Linq/Linq3Implementation/KnownSerializerFinders/KnownSerializerFinderVisitMethodCall.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,17 @@ internal partial class KnownSerializerFinderVisitor
176176
MongoEnumerableMethod.PercentileNullableSingle,
177177
MongoEnumerableMethod.PercentileNullableSingleWithSelector,
178178
MongoEnumerableMethod.PercentileSingle,
179-
MongoEnumerableMethod.PercentileSingleWithSelector
179+
MongoEnumerableMethod.PercentileSingleWithSelector,
180+
WindowMethod.PercentileWithDecimal,
181+
WindowMethod.PercentileWithDouble,
182+
WindowMethod.PercentileWithInt32,
183+
WindowMethod.PercentileWithInt64,
184+
WindowMethod.PercentileWithNullableDecimal,
185+
WindowMethod.PercentileWithNullableDouble,
186+
WindowMethod.PercentileWithNullableInt32,
187+
WindowMethod.PercentileWithNullableInt64,
188+
WindowMethod.PercentileWithNullableSingle,
189+
WindowMethod.PercentileWithSingle
180190
];
181191

182192
private static readonly HashSet<MethodInfo> __averageOrMedianOrPercentileWithSelectorMethods =
@@ -220,7 +230,17 @@ internal partial class KnownSerializerFinderVisitor
220230
MongoEnumerableMethod.PercentileNullableInt32WithSelector,
221231
MongoEnumerableMethod.PercentileNullableInt64WithSelector,
222232
MongoEnumerableMethod.PercentileNullableSingleWithSelector,
223-
MongoEnumerableMethod.PercentileSingleWithSelector
233+
MongoEnumerableMethod.PercentileSingleWithSelector,
234+
WindowMethod.PercentileWithDecimal,
235+
WindowMethod.PercentileWithDouble,
236+
WindowMethod.PercentileWithInt32,
237+
WindowMethod.PercentileWithInt64,
238+
WindowMethod.PercentileWithNullableDecimal,
239+
WindowMethod.PercentileWithNullableDouble,
240+
WindowMethod.PercentileWithNullableInt32,
241+
WindowMethod.PercentileWithNullableInt64,
242+
WindowMethod.PercentileWithNullableSingle,
243+
WindowMethod.PercentileWithSingle
224244
];
225245

226246
private static readonly HashSet<MethodInfo> __countMethods =
@@ -814,6 +834,8 @@ void DeduceMethodCallSerializers()
814834
case "SelectMany": DeduceSelectManySerializers(); break;
815835
case "SequenceEqual": DeduceSequenceEqualMethodSerializers(); break;
816836
case "SetEquals": DeduceSetEqualsMethodSerializers(); break;
837+
case "SetWindowFields": DeduceSetWindowFieldsMethodSerializers(); break;
838+
case "Shift": DeduceShiftMethodSerializers(); break;
817839
case "Sin": DeduceSinMethodSerializers(); break;
818840
case "Sinh": DeduceSinhMethodSerializers(); break;
819841
case "Split": DeduceSplitMethodSerializers(); break;
@@ -2965,6 +2987,43 @@ parameters[0] is var otherParameter &&
29652987
}
29662988
}
29672989

2990+
void DeduceSetWindowFieldsMethodSerializers()
2991+
{
2992+
if (method.Is(EnumerableMethod.First))
2993+
{
2994+
var objectExpression = node.Object;
2995+
var otherExpression = arguments[0];
2996+
2997+
DeduceCollectionAndCollectionSerializers(objectExpression, otherExpression);
2998+
DeduceReturnsBooleanSerializer();
2999+
}
3000+
else
3001+
{
3002+
DeduceUnknownMethodSerializer();
3003+
}
3004+
}
3005+
3006+
void DeduceShiftMethodSerializers()
3007+
{
3008+
if (method.IsOneOf(WindowMethod.Shift, WindowMethod.ShiftWithDefaultValue))
3009+
{
3010+
var sourceExpression = arguments[0];
3011+
var selectorLambda = ExpressionHelper.UnquoteLambdaIfQueryableMethod(method, arguments[1]);
3012+
var selectorSourceItemParameter = selectorLambda.Parameters[0];
3013+
3014+
DeduceItemAndCollectionSerializers(selectorSourceItemParameter, sourceExpression);
3015+
3016+
if (IsNotKnown(node) && IsKnown(selectorLambda.Body, out var resultSerializer))
3017+
{
3018+
AddKnownSerializer(node, resultSerializer);
3019+
}
3020+
}
3021+
else
3022+
{
3023+
DeduceUnknownMethodSerializer();
3024+
}
3025+
}
3026+
29683027
void DeduceSinMethodSerializers()
29693028
{
29703029
if (method.Is(MathMethod.Sin))

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,8 @@ public static bool IsKeyValuePairSerializer(
9090
out IBsonSerializer keySerializer,
9191
out IBsonSerializer valueSerializer)
9292
{
93-
if (serializer is IKeyValuePairSerializer keyValuePairSerializer)
94-
{
95-
keyElementName = keyValuePairSerializer.KeyElementName;
96-
valueElementName = keyValuePairSerializer.ValueElementName;
97-
keySerializer = keyValuePairSerializer.KeySerializer;
98-
valueSerializer = keyValuePairSerializer.ValueSerializer;
99-
return true;
100-
}
101-
102-
// support user written custom KeyValuePair serializers that might not implement IKeyValuePairSerializer
93+
// TODO: add properties to IKeyValuePairSerializer to let us extract the needed information
94+
// note: we can only verify the existence of "Key" and "Value" properties, but can't verify there are no others
10395
if (serializer.ValueType is var valueType &&
10496
valueType.IsConstructedGenericType &&
10597
valueType.GetGenericTypeDefinition() == typeof(KeyValuePair<,>) &&

src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal interface ISetWindowFieldsPartitionSerializer
2424
IBsonSerializer InputSerializer { get; }
2525
}
2626

27-
internal class ISetWindowFieldsPartitionSerializer<TInput> : IBsonSerializer<ISetWindowFieldsPartition<TInput>>, ISetWindowFieldsPartitionSerializer
27+
internal class ISetWindowFieldsPartitionSerializer<TInput> : IBsonSerializer<ISetWindowFieldsPartition<TInput>>, ISetWindowFieldsPartitionSerializer, IBsonArraySerializer
2828
{
2929
private readonly IBsonSerializer<TInput> _inputSerializer;
3030

@@ -61,16 +61,20 @@ public void Serialize(BsonSerializationContext context, BsonSerializationArgs ar
6161
throw new InvalidOperationException("This serializer is not intended to be used.");
6262
}
6363

64-
6564
public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
6665
{
6766
throw new InvalidOperationException("This serializer is not intended to be used.");
6867
}
6968

70-
7169
object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
7270
{
7371
throw new InvalidOperationException("This serializer is not intended to be used.");
7472
}
73+
74+
public bool TryGetItemSerializationInfo(out BsonSerializationInfo itemSerializationInfo)
75+
{
76+
itemSerializationInfo = new BsonSerializationInfo(null, _inputSerializer, _inputSerializer.ValueType);
77+
return true;
78+
}
7579
}
7680
}

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/ConvertMethodToAggregationExpressionTranslator.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
4242
var valueExpression = arguments[0];
4343
var optionsExpression = arguments[1];
4444

45-
var toSerializer = context.KnownSerializers.GetSerializer(expression);
46-
var toBsonType = GetResultRepresentation(expression, toSerializer);
45+
var toBsonType = GetResultRepresentation(expression, toType);
4746
var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression);
47+
var toSerializer = context.KnownSerializers.GetSerializer(expression);
4848
var (subType, byteOrder, format, onErrorAst, onNullAst) = TranslateOptions(context, expression, optionsExpression, toSerializer);
4949

5050
var ast = AstExpression.Convert(valueTranslation.Ast, toBsonType.Render(), subType, byteOrder, format, onErrorAst, onNullAst);
@@ -144,14 +144,39 @@ IBsonSerializer toSerializer
144144
return (subType, byteOrder, format, onErrorTranslation?.Ast, onNullTranslation?.Ast);
145145
}
146146

147-
private static BsonType GetResultRepresentation(Expression expression, IBsonSerializer resultSerializer)
147+
private static BsonType GetResultRepresentation(Expression expression, Type toType)
148148
{
149-
if (resultSerializer is not IHasRepresentationSerializer hasRepresentationSerializer)
149+
var isNullable = toType.IsNullable();
150+
var valueType = isNullable ? Nullable.GetUnderlyingType(toType) : toType;
151+
152+
var representation = Type.GetTypeCode(valueType) switch
150153
{
151-
throw new ExpressionNotSupportedException(expression, because: $"{resultSerializer.GetType().Name} does not implement {nameof(IHasRepresentationSerializer)}");
152-
}
154+
TypeCode.Boolean => BsonType.Boolean,
155+
TypeCode.Byte => BsonType.Int32,
156+
TypeCode.Char => BsonType.String,
157+
TypeCode.DateTime => BsonType.DateTime,
158+
TypeCode.Decimal => BsonType.Decimal128,
159+
TypeCode.Double => BsonType.Double,
160+
TypeCode.Int16 => BsonType.Int32,
161+
TypeCode.Int32 => BsonType.Int32,
162+
TypeCode.Int64 => BsonType.Int64,
163+
TypeCode.SByte => BsonType.Int32,
164+
TypeCode.Single => BsonType.Double,
165+
TypeCode.String => BsonType.String,
166+
TypeCode.UInt16 => BsonType.Int32,
167+
TypeCode.UInt32 => BsonType.Int64,
168+
TypeCode.UInt64 => BsonType.Decimal128,
169+
170+
_ when valueType == typeof(byte[]) => BsonType.Binary,
171+
_ when valueType == typeof(BsonBinaryData) => BsonType.Binary,
172+
_ when valueType == typeof(Decimal128) => BsonType.Decimal128,
173+
_ when valueType == typeof(Guid) => BsonType.Binary,
174+
_ when valueType == typeof(ObjectId) => BsonType.ObjectId,
175+
176+
_ => throw new ExpressionNotSupportedException(expression, because: $"{toType} is not a valid TTo for Convert")
177+
};
153178

154-
return hasRepresentationSerializer.Representation;
179+
return representation;
155180
}
156181
}
157182
}

src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/NewKeyValuePairExpressionToAggregationExpressionTranslator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public static TranslatedExpression Translate(
4444
var keyTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, keyExpression);
4545
var valueTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, valueExpression);
4646

47+
var ast = AstExpression.ComputedDocument([
48+
AstExpression.ComputedField("k", keyTranslation.Ast),
49+
AstExpression.ComputedField("v", valueTranslation.Ast)
50+
]);
51+
4752
var keySerializer = keyTranslation.Serializer;
4853
var valueSerializer = valueTranslation.Serializer;
4954
var keyValuePairSerializer = KeyValuePairSerializer.Create(BsonType.Document, keySerializer, valueSerializer);
5055

51-
var ast = AstExpression.ComputedDocument([
52-
AstExpression.ComputedField(keyValuePairSerializer.KeyElementName, keyTranslation.Ast),
53-
AstExpression.ComputedField(keyValuePairSerializer.ValueElementName, valueTranslation.Ast)
54-
]);
55-
5656
return new TranslatedExpression(expression, ast, keyValuePairSerializer);
5757
}
5858
}

0 commit comments

Comments
 (0)