@@ -20,100 +20,88 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
20
20
internal static class AstExpressionExtensions
21
21
{
22
22
public static bool IsBooleanConstant ( this AstExpression expression )
23
- =>
24
- expression is AstConstantExpression constantExpression &&
25
- constantExpression . Value . IsBoolean ;
23
+ => expression . IsConstant < BsonBoolean > ( out _ ) ;
26
24
27
- public static bool IsBooleanConstant ( this AstExpression expression , out bool value )
25
+ public static bool IsBooleanConstant ( this AstExpression expression , out bool booleanConstant )
28
26
{
29
- if ( expression is AstConstantExpression constantExpression && constantExpression . Value is BsonBoolean bsonBoolean )
27
+ if ( expression . IsConstant < BsonBoolean > ( out var bsonBooleanConstant ) )
30
28
{
31
- value = bsonBoolean . Value ;
29
+ booleanConstant = bsonBooleanConstant . Value ;
32
30
return true ;
33
31
}
34
32
35
- value = default ;
33
+ booleanConstant = default ;
36
34
return false ;
37
35
}
38
36
39
37
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 ;
43
39
44
- public static bool IsConstant ( this AstExpression expression , out BsonValue value )
40
+ public static bool IsConstant ( this AstExpression expression , out BsonValue constant )
45
41
{
46
42
if ( expression is AstConstantExpression constantExpression )
47
43
{
48
- value = constantExpression . Value ;
44
+ constant = constantExpression . Value ;
49
45
return true ;
50
46
}
51
47
52
- value = null ;
48
+ constant = null ;
53
49
return false ;
54
50
}
55
51
56
- public static bool IsConstant < TBsonValue > ( this AstExpression expression , out TBsonValue value )
52
+ public static bool IsConstant < TBsonValue > ( this AstExpression expression , out TBsonValue constant )
57
53
where TBsonValue : BsonValue
58
54
{
59
- if ( expression is AstConstantExpression constantExpression && constantExpression . Value is TBsonValue bsonValue )
55
+ if ( expression . IsConstant ( out var bsonValueConstant ) && bsonValueConstant is TBsonValue derivedBsonValueConstant )
60
56
{
61
- value = bsonValue ;
57
+ constant = derivedBsonValueConstant ;
62
58
return true ;
63
59
}
64
60
65
- value = null ;
61
+ constant = null ;
66
62
return false ;
67
63
}
68
64
69
65
public static bool IsInt32Constant ( this AstExpression expression )
70
- =>
71
- expression is AstConstantExpression constantExpression &&
72
- constantExpression . Value . IsInt32 ;
66
+ => expression . IsConstant < BsonInt32 > ( out _ ) ;
73
67
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 ;
79
70
80
- public static bool IsInt32Constant ( this AstExpression expression , out int value )
71
+ public static bool IsInt32Constant ( this AstExpression expression , out int int32Constant )
81
72
{
82
- if ( expression is AstConstantExpression constantExpression && constantExpression . Value is BsonInt32 bsonInt32 )
73
+ if ( expression . IsConstant < BsonInt32 > ( out var bsonInt32Constant ) )
83
74
{
84
- value = bsonInt32 . Value ;
75
+ int32Constant = bsonInt32Constant . Value ;
85
76
return true ;
86
77
}
87
78
88
- value = default ;
79
+ int32Constant = default ;
89
80
return false ;
90
81
}
91
82
92
83
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 ;
94
85
95
86
public static bool IsRootVar ( this AstExpression expression )
96
87
=> expression is AstVarExpression varExpression && varExpression . Name == "ROOT" && varExpression . IsCurrent ;
97
88
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 ;
103
91
104
- public static bool IsStringConstant ( this AstExpression expression , out string value )
92
+ public static bool IsStringConstant ( this AstExpression expression , out string stringConstant )
105
93
{
106
- if ( expression is AstConstantExpression constantExpression && constantExpression . Value is BsonString bsonString )
94
+ if ( expression . IsConstant < BsonString > ( out var bsonStringConstant ) )
107
95
{
108
- value = bsonString . Value ;
96
+ stringConstant = bsonStringConstant . Value ;
109
97
return true ;
110
98
}
111
99
112
- value = default ;
100
+ stringConstant = default ;
113
101
return false ;
114
102
}
115
103
116
104
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
118
106
}
119
107
}
0 commit comments