Skip to content

CSHARP-5551: Support KeyValuePair.Create<TKey,TValue>method in LINQ #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2025

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner April 2, 2025 21:29
@sanych-sun sanych-sun requested review from JamesKovacs, rstam and adelinowona and removed request for a team and JamesKovacs April 2, 2025 21:29
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
if (method.Is(KeyValuePairMethod.Create))
{
return NewKeyValuePairExpressionToAggregationExpressionTranslator.TranslateKeyValuePairExpression(context, expression, expression.Arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break apart the arguments and pass them individually for perfect clarity:

var keyExpression = arguments[0];
var valueExpression = arguments[1];
return NewKeyValuePairExpressionToAggregationExpressionTranslator.Translate(context, expression, keyExpression, valueExpression);

=> expression.Type.IsConstructedGenericType && expression.Type.GetGenericTypeDefinition() == typeof(KeyValuePair<,>);

public static TranslatedExpression Translate(TranslationContext context, NewExpression expression)
=> TranslateKeyValuePairExpression(context, expression, expression.Arguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break apart the arguments and pass them individually for perfect clarity:

public static TranslatedExpression Translate(TranslationContext context, NewExpression expression)
{                                                                                                 
    var arguments = expression.Arguments;                                                         
    var keyExpression = arguments[0];                                                             
    var valueExpression = arguments[1];                                                           
    return Translate(context, expression, keyExpression, valueExpression);                        
}                           

I realize there is a little bit of duplication but I think the clarity of giving the keyExpression and valueExpression meaningful variable names is worth it.


public static TranslatedExpression TranslateKeyValuePairExpression(TranslationContext context, Expression expression, IReadOnlyList<Expression> arguments)
{
var translatedKeyExpression = ExpressionToAggregationExpressionTranslator.Translate(context, arguments[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name we usually use for this variable is keyTranslation.

public static TranslatedExpression Translate(TranslationContext context, NewExpression expression)
=> TranslateKeyValuePairExpression(context, expression, expression.Arguments);

public static TranslatedExpression TranslateKeyValuePairExpression(TranslationContext context, Expression expression, IReadOnlyList<Expression> arguments)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having an arguments parameter use explicit keyExpression and valueExpression parameters:

public static TranslatedExpression Translate(
    TranslationContext context,              
    Expression expression,                   
    Expression keyExpression,                
    Expression valueExpression)              

The name can be shortened to Translate. There is no ambiguity with this signature.

classMap.GetMemberMap("Value").SetSerializer(valueSerializer);
classMap.Freeze();

// have to use BsonClassMapSerializer here to mimic the MemberInitExpressionToAggregationExpressionTranslator to avoid behavioral breaking change
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe reword "avoid behavioral breaking change" to "avoid risking a behavioral breaking change"

We don't think there is a behavioral change, we just want to play it safe.

public static TranslatedExpression TranslateKeyValuePairExpression(TranslationContext context, Expression expression, IReadOnlyList<Expression> arguments)
{
var translatedKeyExpression = ExpressionToAggregationExpressionTranslator.Translate(context, arguments[0]);
var translatedValueExpression = ExpressionToAggregationExpressionTranslator.Translate(context, arguments[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name we usually use for this variable is valueTranslation.

@sanych-sun sanych-sun requested a review from rstam April 2, 2025 23:06
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return Translate(context, expression, keyExpression, valueExpression);
}

public static TranslatedExpression Translate(TranslationContext context, Expression expression, Expression keyExpression, Expression valueExpression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time the line gets this long I start putting the parameters on separate lines:

public static TranslatedExpression Translate(
    TranslationContext context,              
    Expression expression,                   
    Expression keyExpression,                
    Expression valueExpression) 

@sanych-sun sanych-sun merged commit a09e9c5 into mongodb:main Apr 3, 2025
27 of 30 checks passed
@sanych-sun sanych-sun deleted the csharp5551 branch April 3, 2025 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants