Skip to content

Commit ae1635c

Browse files
committed
CSHARP-5563: Code review changes.
1 parent 6dbe1e1 commit ae1635c

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpressionExtensions.cs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,100 +20,88 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
2020
internal static class AstExpressionExtensions
2121
{
2222
public static bool IsBooleanConstant(this AstExpression expression)
23-
=>
24-
expression is AstConstantExpression constantExpression &&
25-
constantExpression.Value.IsBoolean;
23+
=> expression.IsConstant<BsonBoolean>(out _);
2624

27-
public static bool IsBooleanConstant(this AstExpression expression, out bool value)
25+
public static bool IsBooleanConstant(this AstExpression expression, out bool booleanConstant)
2826
{
29-
if (expression is AstConstantExpression constantExpression && constantExpression.Value is BsonBoolean bsonBoolean)
27+
if (expression.IsConstant<BsonBoolean>(out var bsonBooleanConstant))
3028
{
31-
value = bsonBoolean.Value;
29+
booleanConstant = bsonBooleanConstant.Value;
3230
return true;
3331
}
3432

35-
value = default;
33+
booleanConstant = default;
3634
return false;
3735
}
3836

3937
public static bool IsBsonNull(this AstExpression expression)
40-
=>
41-
expression is AstConstantExpression constantExpression &&
42-
constantExpression.Value.IsBsonNull;
38+
=> expression.IsConstant(out var constant) && constant.IsBsonNull;
4339

44-
public static bool IsConstant(this AstExpression expression, out BsonValue value)
40+
public static bool IsConstant(this AstExpression expression, out BsonValue constant)
4541
{
4642
if (expression is AstConstantExpression constantExpression)
4743
{
48-
value = constantExpression.Value;
44+
constant = constantExpression.Value;
4945
return true;
5046
}
5147

52-
value = null;
48+
constant = null;
5349
return false;
5450
}
5551

56-
public static bool IsConstant<TBsonValue>(this AstExpression expression, out TBsonValue value)
52+
public static bool IsConstant<TBsonValue>(this AstExpression expression, out TBsonValue constant)
5753
where TBsonValue : BsonValue
5854
{
59-
if (expression is AstConstantExpression constantExpression && constantExpression.Value is TBsonValue bsonValue)
55+
if (expression.IsConstant(out var bsonValueConstant) && bsonValueConstant is TBsonValue derivedBsonValueConstant)
6056
{
61-
value = bsonValue;
57+
constant = derivedBsonValueConstant;
6258
return true;
6359
}
6460

65-
value = null;
61+
constant = null;
6662
return false;
6763
}
6864

6965
public static bool IsInt32Constant(this AstExpression expression)
70-
=>
71-
expression is AstConstantExpression constantExpression &&
72-
constantExpression.Value.IsInt32;
66+
=> expression.IsConstant<BsonInt32>(out _);
7367

74-
public static bool IsInt32Constant(this AstExpression expression, int value)
75-
=>
76-
expression is AstConstantExpression constantExpression &&
77-
constantExpression.Value is BsonInt32 bsonInt32 &&
78-
bsonInt32.Value == value;
68+
public static bool IsInt32Constant(this AstExpression expression, int comparand)
69+
=> expression.IsInt32Constant(out var int32Constant) && int32Constant == comparand;
7970

80-
public static bool IsInt32Constant(this AstExpression expression, out int value)
71+
public static bool IsInt32Constant(this AstExpression expression, out int int32Constant)
8172
{
82-
if (expression is AstConstantExpression constantExpression && constantExpression.Value is BsonInt32 bsonInt32)
73+
if (expression.IsConstant<BsonInt32>(out var bsonInt32Constant))
8374
{
84-
value = bsonInt32.Value;
75+
int32Constant = bsonInt32Constant.Value;
8576
return true;
8677
}
8778

88-
value = default;
79+
int32Constant = default;
8980
return false;
9081
}
9182

9283
public static bool IsMaxInt32(this AstExpression expression)
93-
=> expression.IsInt32Constant(out var value) && value == int.MaxValue;
84+
=> expression.IsInt32Constant(out var int32Constant) && int32Constant == int.MaxValue;
9485

9586
public static bool IsRootVar(this AstExpression expression)
9687
=> expression is AstVarExpression varExpression && varExpression.Name == "ROOT" && varExpression.IsCurrent;
9788

98-
public static bool IsStringConstant(this AstExpression expression, string value)
99-
=>
100-
expression is AstConstantExpression constantExpression &&
101-
constantExpression.Value is BsonString bsonString &&
102-
bsonString.Value == value;
89+
public static bool IsStringConstant(this AstExpression expression, string comparand)
90+
=> expression.IsStringConstant(out var stringConstant) && stringConstant == comparand;
10391

104-
public static bool IsStringConstant(this AstExpression expression, out string value)
92+
public static bool IsStringConstant(this AstExpression expression, out string stringConstant)
10593
{
106-
if (expression is AstConstantExpression constantExpression && constantExpression.Value is BsonString bsonString)
94+
if (expression.IsConstant<BsonString>(out var bsonStringConstant))
10795
{
108-
value = bsonString.Value;
96+
stringConstant = bsonStringConstant.Value;
10997
return true;
11098
}
11199

112-
value = default;
100+
stringConstant = default;
113101
return false;
114102
}
115103

116104
public static bool IsZero(this AstExpression expression)
117-
=> expression is AstConstantExpression constantExpression && constantExpression.Value == 0;
105+
=> expression.IsConstant(out var constant) && constant == 0; // works for all numeric BSON types
118106
}
119107
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override AstNode VisitBinaryExpression(AstBinaryExpression node)
5151
{
5252
// { $ifNull : [expr1, expr2] } => expr2 when expr1 == null
5353
// { $ifNull : [expr1, expr2] } => expr1 when expr1 != null
54-
return arg1Constant == BsonNull.Value ? arg2 : arg1;
54+
return arg1Constant.IsBsonNull ? arg2 : arg1;
5555
}
5656

5757
if (arg2.IsBsonNull())

0 commit comments

Comments
 (0)