Skip to content

Commit a09e9c5

Browse files
authored
CSHARP-5551: Support KeyValuePair.Create<TKey,TValue>method in LINQ (#1661)
1 parent cc9b265 commit a09e9c5

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.Reflection;
18+
19+
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
20+
21+
namespace MongoDB.Driver.Linq.Linq3Implementation.Reflection
22+
{
23+
internal static class KeyValuePairMethod
24+
{
25+
// private static fields
26+
private static readonly MethodInfo __create;
27+
28+
// static constructor
29+
static KeyValuePairMethod()
30+
{
31+
__create = ReflectionInfo.Method((object key, object value) => KeyValuePair.Create(key, value));
32+
}
33+
34+
// public properties
35+
public static MethodInfo Create => __create;
36+
}
37+
}
38+
39+
#endif

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

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Reflection;
2020
using MongoDB.Bson.Serialization;
2121
using MongoDB.Bson.Serialization.Serializers;
22+
using MongoDB.Driver.Linq.Linq3Implementation.Ast;
2223
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
2324
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2425
using MongoDB.Driver.Linq.Linq3Implementation.Reflection;
@@ -87,6 +88,14 @@ public static TranslatedExpression Translate(TranslationContext context, MethodC
8788
return new TranslatedExpression(expression, ast, tupleSerializer);
8889
}
8990

91+
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
92+
if (method.Is(KeyValuePairMethod.Create))
93+
{
94+
var keyExpression = arguments[0];
95+
var valueExpression = arguments[1];
96+
return NewKeyValuePairExpressionToAggregationExpressionTranslator.Translate(context, expression, keyExpression, valueExpression);
97+
}
98+
#endif
9099
throw new ExpressionNotSupportedException(expression);
91100
}
92101

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

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static TranslatedExpression Translate(TranslationContext context, NewExpr
4646
{
4747
return NewTupleExpressionToAggregationExpressionTranslator.Translate(context, expression);
4848
}
49+
if (NewKeyValuePairExpressionToAggregationExpressionTranslator.CanTranslate(expression))
50+
{
51+
return NewKeyValuePairExpressionToAggregationExpressionTranslator.Translate(context, expression);
52+
}
4953
return MemberInitExpressionToAggregationExpressionTranslator.Translate(context, expression, expression, Array.Empty<MemberBinding>());
5054
}
5155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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;
18+
using FluentAssertions;
19+
using MongoDB.Driver.TestHelpers;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators;
23+
24+
#if NET6_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER
25+
public class NewKeyValuePairExpressionToAggregationExpressionTranslatorTests : LinqIntegrationTest<NewKeyValuePairExpressionToAggregationExpressionTranslatorTests.ClassFixture>
26+
{
27+
public NewKeyValuePairExpressionToAggregationExpressionTranslatorTests(ClassFixture fixture)
28+
: base(fixture)
29+
{
30+
}
31+
32+
[Fact]
33+
public void NewKeyValuePair_should_translate()
34+
{
35+
var collection = Fixture.Collection;
36+
37+
var queryable = collection.AsQueryable()
38+
.Select(d => new KeyValuePair<string,int>("X", d.X));
39+
40+
var stages = Translate(collection, queryable);
41+
AssertStages(stages, "{ $project : { Key : 'X', Value : '$X', _id : 0 } }");
42+
43+
var result = queryable.Single();
44+
result.Key.Should().Be("X");
45+
result.Value.Should().Be(42);
46+
}
47+
48+
[Fact]
49+
public void KeyValuePair_Create_should_translate()
50+
{
51+
var collection = Fixture.Collection;
52+
53+
var queryable = collection.AsQueryable()
54+
.Select(d => KeyValuePair.Create("X", d.X));
55+
56+
var stages = Translate(collection, queryable);
57+
AssertStages(stages, "{ $project : { Key : 'X', Value : '$X', _id : 0 } }");
58+
59+
var result = queryable.Single();
60+
result.Key.Should().Be("X");
61+
result.Value.Should().Be(42);
62+
}
63+
64+
public class C
65+
{
66+
public int X { get; set; }
67+
}
68+
69+
public sealed class ClassFixture : MongoCollectionFixture<C>
70+
{
71+
protected override IEnumerable<C> InitialData =>
72+
[
73+
new C { X = 42 }
74+
];
75+
}
76+
}
77+
78+
#endif

0 commit comments

Comments
 (0)